mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-09 01:36:51 +01:00
* Fixed different calculation of scale min and max when dataset contains no values * Removed trailing spaces * Added test for correct min/max calculation * Removed trailing spaces
107 lines
2.8 KiB
JavaScript
107 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = function(Chart) {
|
|
|
|
var helpers = Chart.helpers,
|
|
noop = helpers.noop;
|
|
|
|
Chart.LinearScaleBase = Chart.Scale.extend({
|
|
handleTickRangeOptions: function() {
|
|
var me = this;
|
|
var opts = me.options;
|
|
var tickOpts = opts.ticks;
|
|
|
|
// If we are forcing it to begin at 0, but 0 will already be rendered on the chart,
|
|
// do nothing since that would make the chart weird. If the user really wants a weird chart
|
|
// axis, they can manually override it
|
|
if (tickOpts.beginAtZero) {
|
|
var minSign = helpers.sign(me.min);
|
|
var maxSign = helpers.sign(me.max);
|
|
|
|
if (minSign < 0 && maxSign < 0) {
|
|
// move the top up to 0
|
|
me.max = 0;
|
|
} else if (minSign > 0 && maxSign > 0) {
|
|
// move the bottom down to 0
|
|
me.min = 0;
|
|
}
|
|
}
|
|
|
|
if (tickOpts.min !== undefined) {
|
|
me.min = tickOpts.min;
|
|
} else if (tickOpts.suggestedMin !== undefined) {
|
|
if (me.min === null) {
|
|
me.min = tickOpts.suggestedMin;
|
|
} else {
|
|
me.min = Math.min(me.min, tickOpts.suggestedMin);
|
|
}
|
|
}
|
|
|
|
if (tickOpts.max !== undefined) {
|
|
me.max = tickOpts.max;
|
|
} else if (tickOpts.suggestedMax !== undefined) {
|
|
if (me.max === null) {
|
|
me.max = tickOpts.suggestedMax;
|
|
} else {
|
|
me.max = Math.max(me.max, tickOpts.suggestedMax);
|
|
}
|
|
}
|
|
|
|
if (me.min === me.max) {
|
|
me.max++;
|
|
|
|
if (!tickOpts.beginAtZero) {
|
|
me.min--;
|
|
}
|
|
}
|
|
},
|
|
getTickLimit: noop,
|
|
handleDirectionalChanges: noop,
|
|
|
|
buildTicks: function() {
|
|
var me = this;
|
|
var opts = me.options;
|
|
var tickOpts = opts.ticks;
|
|
|
|
// Figure out what the max number of ticks we can support it is based on the size of
|
|
// the axis area. For now, we say that the minimum tick spacing in pixels must be 50
|
|
// We also limit the maximum number of ticks to 11 which gives a nice 10 squares on
|
|
// the graph. Make sure we always have at least 2 ticks
|
|
var maxTicks = me.getTickLimit();
|
|
maxTicks = Math.max(2, maxTicks);
|
|
|
|
var numericGeneratorOptions = {
|
|
maxTicks: maxTicks,
|
|
min: tickOpts.min,
|
|
max: tickOpts.max,
|
|
stepSize: helpers.getValueOrDefault(tickOpts.fixedStepSize, tickOpts.stepSize)
|
|
};
|
|
var ticks = me.ticks = Chart.Ticks.generators.linear(numericGeneratorOptions, me);
|
|
|
|
me.handleDirectionalChanges();
|
|
|
|
// At this point, we need to update our max and min given the tick values since we have expanded the
|
|
// range of the scale
|
|
me.max = helpers.max(ticks);
|
|
me.min = helpers.min(ticks);
|
|
|
|
if (tickOpts.reverse) {
|
|
ticks.reverse();
|
|
|
|
me.start = me.max;
|
|
me.end = me.min;
|
|
} else {
|
|
me.start = me.min;
|
|
me.end = me.max;
|
|
}
|
|
},
|
|
convertTicksToLabels: function() {
|
|
var me = this;
|
|
me.ticksAsNumbers = me.ticks.slice();
|
|
me.zeroLineIndex = me.ticks.indexOf(0);
|
|
|
|
Chart.Scale.prototype.convertTicksToLabels.call(me);
|
|
}
|
|
});
|
|
};
|