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: {
|
2016-09-24 22:56:16 +02:00
|
|
|
callback: Chart.Ticks.formatters.linear
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2017-01-01 15:36:01 +01:00
|
|
|
var hasStacks = opts.stacked;
|
|
|
|
if (hasStacks === undefined) {
|
|
|
|
helpers.each(datasets, function(dataset, datasetIndex) {
|
|
|
|
if (hasStacks) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var meta = chart.getDatasetMeta(datasetIndex);
|
|
|
|
if (chart.isDatasetVisible(datasetIndex) && IDMatches(meta) &&
|
|
|
|
meta.stack !== undefined) {
|
|
|
|
hasStacks = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.stacked || hasStacks) {
|
|
|
|
var valuesPerStack = {};
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-08 14:32:48 +02:00
|
|
|
helpers.each(datasets, function(dataset, datasetIndex) {
|
|
|
|
var meta = chart.getDatasetMeta(datasetIndex);
|
2017-01-01 15:36:01 +01:00
|
|
|
var key = [
|
|
|
|
meta.type,
|
|
|
|
// we have a separate stack for stack=undefined datasets when the opts.stacked is undefined
|
|
|
|
((opts.stacked === undefined && meta.stack === undefined) ? datasetIndex : ''),
|
|
|
|
meta.stack
|
|
|
|
].join('.');
|
|
|
|
|
|
|
|
if (valuesPerStack[key] === undefined) {
|
|
|
|
valuesPerStack[key] = {
|
2016-02-14 23:06:00 +01:00
|
|
|
positiveValues: [],
|
|
|
|
negativeValues: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store these per type
|
2017-01-01 15:36:01 +01:00
|
|
|
var positiveValues = valuesPerStack[key].positiveValues;
|
|
|
|
var negativeValues = valuesPerStack[key].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;
|
2016-09-15 02:23:30 +02:00
|
|
|
} else if (value < 0) {
|
|
|
|
negativeValues[index] += value;
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-09-15 02:23:30 +02:00
|
|
|
positiveValues[index] += 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
|
|
|
|
2017-01-01 15:36:01 +01:00
|
|
|
helpers.each(valuesPerStack, function(valuesForType) {
|
2016-02-14 23:06:00 +01:00
|
|
|
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 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-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()) {
|
2016-10-30 18:14:59 +01:00
|
|
|
pixel = me.left + (me.width / range * (rightValue - start));
|
|
|
|
return Math.round(pixel);
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
2016-10-30 18:14:59 +01:00
|
|
|
|
|
|
|
pixel = me.bottom - (me.height / range * (rightValue - start));
|
2016-09-15 02:23:30 +02:00
|
|
|
return Math.round(pixel);
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
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();
|
2016-10-30 18:14:59 +01:00
|
|
|
var innerDimension = isHorizontal ? me.width : me.height;
|
|
|
|
var offset = (isHorizontal ? pixel - me.left : me.bottom - pixel) / innerDimension;
|
2016-06-04 20:14:16 +02:00
|
|
|
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
|
|
|
};
|