Chart.js/src/scales/scale.linear.js

201 lines
6.4 KiB
JavaScript
Raw Normal View History

"use strict";
module.exports = function(Chart) {
var helpers = Chart.helpers;
var defaultConfig = {
position: "left",
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];
// 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;
}
}
};
var LinearScale = Chart.LinearScaleBase.extend({
determineDataLimits: function() {
2016-05-08 14:32:48 +02:00
var _this = this;
var opts = _this.options;
var tickOpts = opts.ticks;
var chart = _this.chart;
var data = chart.data;
var datasets = data.datasets;
var isHorizontal = _this.isHorizontal();
function IDMatches(meta) {
return isHorizontal ? meta.xAxisID === _this.id : meta.yAxisID === _this.id;
}
// First Calculate the range
2016-05-08 14:32:48 +02:00
_this.min = null;
_this.max = null;
2016-05-08 14:32:48 +02:00
if (opts.stacked) {
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);
if (valuesPerType[meta.type] === undefined) {
valuesPerType[meta.type] = {
positiveValues: [],
negativeValues: []
};
}
// Store these per type
var positiveValues = valuesPerType[meta.type].positiveValues;
var negativeValues = valuesPerType[meta.type].negativeValues;
2016-05-08 14:32:48 +02:00
if (chart.isDatasetVisible(datasetIndex) && IDMatches(meta)) {
helpers.each(dataset.data, function(rawValue, index) {
2016-05-08 14:32:48 +02:00
var value = +_this.getRightValue(rawValue);
if (isNaN(value) || meta.data[index].hidden) {
return;
}
positiveValues[index] = positiveValues[index] || 0;
negativeValues[index] = negativeValues[index] || 0;
2016-05-08 14:32:48 +02:00
if (opts.relativePoints) {
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-05-08 14:32:48 +02:00
});
helpers.each(valuesPerType, function(valuesForType) {
var values = valuesForType.positiveValues.concat(valuesForType.negativeValues);
var minVal = helpers.min(values);
var maxVal = helpers.max(values);
2016-05-08 14:32:48 +02:00
_this.min = _this.min === null ? minVal : Math.min(_this.min, minVal);
_this.max = _this.max === null ? maxVal : Math.max(_this.max, maxVal);
});
} 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)) {
helpers.each(dataset.data, function(rawValue, index) {
2016-05-08 14:32:48 +02:00
var value = +_this.getRightValue(rawValue);
if (isNaN(value) || meta.data[index].hidden) {
return;
}
2016-05-08 14:32:48 +02:00
if (_this.min === null) {
_this.min = value;
} else if (value < _this.min) {
_this.min = value;
}
2016-05-08 14:32:48 +02:00
if (_this.max === null) {
_this.max = value;
} else if (value > _this.max) {
_this.max = value;
}
2016-05-08 14:32:48 +02:00
});
}
2016-05-08 14:32:48 +02:00
});
}
// Common base implementation to handle ticks.min, ticks.max, ticks.beginAtZero
this.handleTickRangeOptions();
},
getTickLimit: function() {
var maxTicks;
var me = this;
var tickOpts = me.options.ticks;
if (me.isHorizontal()) {
maxTicks = Math.min(tickOpts.maxTicksLimit ? tickOpts.maxTicksLimit : 11, Math.ceil(me.width / 50));
} else {
// The factor of 2 used to scale the font size has been experimentally determined.
var tickFontSize = helpers.getValueOrDefault(tickOpts.fontSize, Chart.defaults.global.defaultFontSize);
maxTicks = Math.min(tickOpts.maxTicksLimit ? tickOpts.maxTicksLimit : 11, Math.ceil(me.height / (2 * tickFontSize)));
}
return maxTicks;
},
// Called after the ticks are built. We need
handleDirectionalChanges: function() {
var me = this;
if (!me.isHorizontal()) {
// We are in a vertical orientation. The top value is the highest. So reverse the array
me.ticks.reverse();
}
},
getLabelForIndex: function(index, datasetIndex) {
return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]);
},
// Utils
getPixelForValue: function(value, index, datasetIndex, includeOffset) {
// This must be called after fit has been run so that
// this.left, this.top, this.right, and this.bottom have been defined
2016-05-08 13:55:29 +02:00
var _this = this;
var paddingLeft = _this.paddingLeft;
var paddingBottom = _this.paddingBottom;
var start = _this.start;
var rightValue = +_this.getRightValue(value);
var pixel;
2016-05-08 13:55:29 +02:00
var innerDimension;
var range = _this.end - start;
2016-05-08 13:55:29 +02:00
if (_this.isHorizontal()) {
innerDimension = _this.width - (paddingLeft + _this.paddingRight);
pixel = _this.left + (innerDimension / range * (rightValue - start));
return Math.round(pixel + paddingLeft);
} else {
2016-05-08 13:55:29 +02:00
innerDimension = _this.height - (_this.paddingTop + paddingBottom);
pixel = (_this.bottom - paddingBottom) - (innerDimension / range * (rightValue - start));
return Math.round(pixel);
}
},
2016-04-21 13:48:47 +02:00
getValueForPixel: function(pixel) {
2016-05-08 13:55:29 +02:00
var _this = this;
var isHorizontal = _this.isHorizontal();
var paddingLeft = _this.paddingLeft;
var paddingBottom = _this.paddingBottom;
var innerDimension = isHorizontal ? _this.width - (paddingLeft + _this.paddingRight) : _this.height - (_this.paddingTop + paddingBottom);
var offset = (isHorizontal ? pixel - _this.left - paddingLeft : _this.bottom - paddingBottom - pixel) / innerDimension;
return _this.start + ((_this.end - _this.start) * offset);
2016-04-21 13:48:47 +02:00
},
getPixelForTick: function(index, includeOffset) {
return this.getPixelForValue(this.ticksAsNumbers[index], null, null, includeOffset);
}
});
Chart.scaleService.registerScaleType("linear", LinearScale, defaultConfig);
};