2016-09-14 20:05:19 +02:00
|
|
|
'use strict';
|
2016-02-12 04:30:53 +01:00
|
|
|
|
|
|
|
module.exports = function(Chart) {
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
var helpers = Chart.helpers;
|
|
|
|
|
|
|
|
var defaultConfig = {
|
2016-09-14 20:05:19 +02:00
|
|
|
position: 'left',
|
2016-02-14 23:06:00 +01:00
|
|
|
ticks: {
|
|
|
|
callback: function(tickValue, index, ticks) {
|
2016-04-17 16:33:38 +02:00
|
|
|
// If we have lots of ticks, don't use the ones
|
|
|
|
var delta = ticks.length > 3 ? ticks[2] - ticks[1] : ticks[1] - ticks[0];
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// If we have a number like 2.5 as the delta, figure out how many decimal places we need
|
|
|
|
if (Math.abs(delta) > 1) {
|
|
|
|
if (tickValue !== Math.floor(tickValue)) {
|
|
|
|
// not an integer
|
|
|
|
delta = tickValue - Math.floor(tickValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var logDelta = helpers.log10(Math.abs(delta));
|
|
|
|
var tickString = '';
|
|
|
|
|
|
|
|
if (tickValue !== 0) {
|
|
|
|
var numDecimal = -1 * Math.floor(logDelta);
|
|
|
|
numDecimal = Math.max(Math.min(numDecimal, 20), 0); // toFixed has a max of 20 decimal places
|
|
|
|
tickString = tickValue.toFixed(numDecimal);
|
|
|
|
} else {
|
|
|
|
tickString = '0'; // never show decimal places for 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return tickString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-29 16:16:47 +02:00
|
|
|
var LinearScale = Chart.LinearScaleBase.extend({
|
2016-02-14 23:06:00 +01:00
|
|
|
determineDataLimits: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
var opts = me.options;
|
|
|
|
var chart = me.chart;
|
2016-05-08 14:32:48 +02:00
|
|
|
var data = chart.data;
|
|
|
|
var datasets = data.datasets;
|
2016-06-04 20:14:16 +02:00
|
|
|
var isHorizontal = me.isHorizontal();
|
2016-05-08 14:32:48 +02:00
|
|
|
|
|
|
|
function IDMatches(meta) {
|
2016-06-04 20:14:16 +02:00
|
|
|
return isHorizontal ? meta.xAxisID === me.id : meta.yAxisID === me.id;
|
2016-05-08 14:32:48 +02:00
|
|
|
}
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
// First Calculate the range
|
2016-06-04 20:14:16 +02:00
|
|
|
me.min = null;
|
|
|
|
me.max = null;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-08 14:32:48 +02:00
|
|
|
if (opts.stacked) {
|
2016-02-14 23:06:00 +01:00
|
|
|
var valuesPerType = {};
|
|
|
|
var hasPositiveValues = false;
|
|
|
|
var hasNegativeValues = false;
|
|
|
|
|
2016-05-08 14:32:48 +02:00
|
|
|
helpers.each(datasets, function(dataset, datasetIndex) {
|
|
|
|
var meta = chart.getDatasetMeta(datasetIndex);
|
2016-04-21 23:43:47 +02:00
|
|
|
if (valuesPerType[meta.type] === undefined) {
|
|
|
|
valuesPerType[meta.type] = {
|
2016-02-14 23:06:00 +01:00
|
|
|
positiveValues: [],
|
|
|
|
negativeValues: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store these per type
|
2016-04-21 23:43:47 +02:00
|
|
|
var positiveValues = valuesPerType[meta.type].positiveValues;
|
|
|
|
var negativeValues = valuesPerType[meta.type].negativeValues;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-08 14:32:48 +02:00
|
|
|
if (chart.isDatasetVisible(datasetIndex) && IDMatches(meta)) {
|
2016-02-14 23:06:00 +01:00
|
|
|
helpers.each(dataset.data, function(rawValue, index) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var value = +me.getRightValue(rawValue);
|
2016-04-23 10:57:29 +02:00
|
|
|
if (isNaN(value) || meta.data[index].hidden) {
|
2016-02-14 23:06:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
positiveValues[index] = positiveValues[index] || 0;
|
|
|
|
negativeValues[index] = negativeValues[index] || 0;
|
|
|
|
|
2016-05-08 14:32:48 +02:00
|
|
|
if (opts.relativePoints) {
|
2016-02-14 23:06:00 +01:00
|
|
|
positiveValues[index] = 100;
|
|
|
|
} else {
|
|
|
|
if (value < 0) {
|
|
|
|
hasNegativeValues = true;
|
|
|
|
negativeValues[index] += value;
|
|
|
|
} else {
|
|
|
|
hasPositiveValues = true;
|
|
|
|
positiveValues[index] += value;
|
|
|
|
}
|
|
|
|
}
|
2016-05-08 14:32:48 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
2016-05-08 14:32:48 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
helpers.each(valuesPerType, function(valuesForType) {
|
|
|
|
var values = valuesForType.positiveValues.concat(valuesForType.negativeValues);
|
|
|
|
var minVal = helpers.min(values);
|
|
|
|
var maxVal = helpers.max(values);
|
2016-06-04 20:14:16 +02:00
|
|
|
me.min = me.min === null ? minVal : Math.min(me.min, minVal);
|
|
|
|
me.max = me.max === null ? maxVal : Math.max(me.max, maxVal);
|
2016-05-08 14:32:48 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
} else {
|
2016-05-08 14:32:48 +02:00
|
|
|
helpers.each(datasets, function(dataset, datasetIndex) {
|
|
|
|
var meta = chart.getDatasetMeta(datasetIndex);
|
|
|
|
if (chart.isDatasetVisible(datasetIndex) && IDMatches(meta)) {
|
2016-02-14 23:06:00 +01:00
|
|
|
helpers.each(dataset.data, function(rawValue, index) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var value = +me.getRightValue(rawValue);
|
2016-04-23 10:57:29 +02:00
|
|
|
if (isNaN(value) || meta.data[index].hidden) {
|
2016-02-14 23:06:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.min === null) {
|
|
|
|
me.min = value;
|
|
|
|
} else if (value < me.min) {
|
|
|
|
me.min = value;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.max === null) {
|
|
|
|
me.max = value;
|
|
|
|
} else if (value > me.max) {
|
|
|
|
me.max = value;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
2016-05-08 14:32:48 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
2016-05-08 14:32:48 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
2016-05-29 16:16:47 +02:00
|
|
|
// Common base implementation to handle ticks.min, ticks.max, ticks.beginAtZero
|
|
|
|
this.handleTickRangeOptions();
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
2016-05-29 16:16:47 +02:00
|
|
|
getTickLimit: function() {
|
2016-02-14 23:06:00 +01:00
|
|
|
var maxTicks;
|
2016-05-29 16:16:47 +02:00
|
|
|
var me = this;
|
|
|
|
var tickOpts = me.options.ticks;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-29 16:16:47 +02:00
|
|
|
if (me.isHorizontal()) {
|
|
|
|
maxTicks = Math.min(tickOpts.maxTicksLimit ? tickOpts.maxTicksLimit : 11, Math.ceil(me.width / 50));
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
|
|
|
// The factor of 2 used to scale the font size has been experimentally determined.
|
2016-05-29 16:16:47 +02:00
|
|
|
var tickFontSize = helpers.getValueOrDefault(tickOpts.fontSize, Chart.defaults.global.defaultFontSize);
|
|
|
|
maxTicks = Math.min(tickOpts.maxTicksLimit ? tickOpts.maxTicksLimit : 11, Math.ceil(me.height / (2 * tickFontSize)));
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
2016-05-29 16:16:47 +02:00
|
|
|
return maxTicks;
|
|
|
|
},
|
2016-06-18 11:00:11 +02:00
|
|
|
// Called after the ticks are built. We need
|
2016-05-29 16:16:47 +02:00
|
|
|
handleDirectionalChanges: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
if (!this.isHorizontal()) {
|
2016-02-14 23:06:00 +01:00
|
|
|
// We are in a vertical orientation. The top value is the highest. So reverse the array
|
2016-06-04 20:14:16 +02:00
|
|
|
this.ticks.reverse();
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
getLabelForIndex: function(index, datasetIndex) {
|
|
|
|
return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]);
|
|
|
|
},
|
|
|
|
// Utils
|
2016-06-18 11:00:11 +02:00
|
|
|
getPixelForValue: function(value) {
|
2016-02-14 23:06:00 +01:00
|
|
|
// This must be called after fit has been run so that
|
2016-06-18 11:00:11 +02:00
|
|
|
// this.left, this.top, this.right, and this.bottom have been defined
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
var paddingLeft = me.paddingLeft;
|
|
|
|
var paddingBottom = me.paddingBottom;
|
|
|
|
var start = me.start;
|
2016-05-08 13:55:29 +02:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
var rightValue = +me.getRightValue(value);
|
2016-02-14 23:06:00 +01:00
|
|
|
var pixel;
|
2016-05-08 13:55:29 +02:00
|
|
|
var innerDimension;
|
2016-06-04 20:14:16 +02:00
|
|
|
var range = me.end - start;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.isHorizontal()) {
|
|
|
|
innerDimension = me.width - (paddingLeft + me.paddingRight);
|
|
|
|
pixel = me.left + (innerDimension / range * (rightValue - start));
|
2016-05-08 13:55:29 +02:00
|
|
|
return Math.round(pixel + paddingLeft);
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-06-04 20:14:16 +02:00
|
|
|
innerDimension = me.height - (me.paddingTop + paddingBottom);
|
|
|
|
pixel = (me.bottom - paddingBottom) - (innerDimension / range * (rightValue - start));
|
2016-02-14 23:06:00 +01:00
|
|
|
return Math.round(pixel);
|
|
|
|
}
|
|
|
|
},
|
2016-04-21 13:48:47 +02:00
|
|
|
getValueForPixel: function(pixel) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
var isHorizontal = me.isHorizontal();
|
|
|
|
var paddingLeft = me.paddingLeft;
|
|
|
|
var paddingBottom = me.paddingBottom;
|
|
|
|
var innerDimension = isHorizontal ? me.width - (paddingLeft + me.paddingRight) : me.height - (me.paddingTop + paddingBottom);
|
|
|
|
var offset = (isHorizontal ? pixel - me.left - paddingLeft : me.bottom - paddingBottom - pixel) / innerDimension;
|
|
|
|
return me.start + ((me.end - me.start) * offset);
|
2016-04-21 13:48:47 +02:00
|
|
|
},
|
2016-06-18 11:00:11 +02:00
|
|
|
getPixelForTick: function(index) {
|
|
|
|
return this.getPixelForValue(this.ticksAsNumbers[index]);
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
});
|
2016-09-14 20:05:19 +02:00
|
|
|
Chart.scaleService.registerScaleType('linear', LinearScale, defaultConfig);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-09-14 20:05:19 +02:00
|
|
|
};
|