mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Better conversion to ticks to make it easier to use callbacks
This commit is contained in:
parent
1157b19626
commit
21a33f0e41
@ -221,13 +221,16 @@ module.exports = function(Chart) {
|
|||||||
this.start = this.min;
|
this.start = this.min;
|
||||||
this.end = this.max;
|
this.end = this.max;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ticksAsNumbers = this.ticks.slice(); // do after we potentially reverse the ticks
|
|
||||||
this.zeroLineIndex = this.ticks.indexOf(0);
|
|
||||||
},
|
},
|
||||||
getLabelForIndex: function(index, datasetIndex) {
|
getLabelForIndex: function(index, datasetIndex) {
|
||||||
return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]);
|
return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]);
|
||||||
},
|
},
|
||||||
|
convertTicksToLabels: function() {
|
||||||
|
this.ticksAsNumbers = this.ticks.slice();
|
||||||
|
this.zeroLineIndex = this.ticks.indexOf(0);
|
||||||
|
|
||||||
|
Chart.Scale.prototype.convertTicksToLabels.call(this);
|
||||||
|
},
|
||||||
// Utils
|
// Utils
|
||||||
getPixelForValue: function(value, index, datasetIndex, includeOffset) {
|
getPixelForValue: function(value, index, datasetIndex, includeOffset) {
|
||||||
// This must be called after fit has been run so that
|
// This must be called after fit has been run so that
|
||||||
|
|||||||
@ -103,7 +103,7 @@ module.exports = function(Chart) {
|
|||||||
buildTicks: function() {
|
buildTicks: function() {
|
||||||
// Reset the ticks array. Later on, we will draw a grid line at these positions
|
// Reset the ticks array. Later on, we will draw a grid line at these positions
|
||||||
// The array simply contains the numerical value of the spots where ticks will be
|
// The array simply contains the numerical value of the spots where ticks will be
|
||||||
this.tickValues = [];
|
this.ticks = [];
|
||||||
|
|
||||||
// Figure out what the max number of ticks we can support it is based on the size of
|
// 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
|
// the axis area. For now, we say that the minimum tick spacing in pixels must be 50
|
||||||
@ -113,7 +113,7 @@ module.exports = function(Chart) {
|
|||||||
var tickVal = this.options.ticks.min !== undefined ? this.options.ticks.min : Math.pow(10, Math.floor(helpers.log10(this.min)));
|
var tickVal = this.options.ticks.min !== undefined ? this.options.ticks.min : Math.pow(10, Math.floor(helpers.log10(this.min)));
|
||||||
|
|
||||||
while (tickVal < this.max) {
|
while (tickVal < this.max) {
|
||||||
this.tickValues.push(tickVal);
|
this.ticks.push(tickVal);
|
||||||
|
|
||||||
var exp = Math.floor(helpers.log10(tickVal));
|
var exp = Math.floor(helpers.log10(tickVal));
|
||||||
var significand = Math.floor(tickVal / Math.pow(10, exp)) + 1;
|
var significand = Math.floor(tickVal / Math.pow(10, exp)) + 1;
|
||||||
@ -127,20 +127,20 @@ module.exports = function(Chart) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var lastTick = this.options.ticks.max !== undefined ? this.options.ticks.max : tickVal;
|
var lastTick = this.options.ticks.max !== undefined ? this.options.ticks.max : tickVal;
|
||||||
this.tickValues.push(lastTick);
|
this.ticks.push(lastTick);
|
||||||
|
|
||||||
if (this.options.position === "left" || this.options.position === "right") {
|
if (this.options.position === "left" || this.options.position === "right") {
|
||||||
// We are in a vertical orientation. The top value is the highest. So reverse the array
|
// We are in a vertical orientation. The top value is the highest. So reverse the array
|
||||||
this.tickValues.reverse();
|
this.ticks.reverse();
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point, we need to update our max and min given the tick values since we have expanded the
|
// At this point, we need to update our max and min given the tick values since we have expanded the
|
||||||
// range of the scale
|
// range of the scale
|
||||||
this.max = helpers.max(this.tickValues);
|
this.max = helpers.max(this.ticks);
|
||||||
this.min = helpers.min(this.tickValues);
|
this.min = helpers.min(this.ticks);
|
||||||
|
|
||||||
if (this.options.ticks.reverse) {
|
if (this.options.ticks.reverse) {
|
||||||
this.tickValues.reverse();
|
this.ticks.reverse();
|
||||||
|
|
||||||
this.start = this.max;
|
this.start = this.max;
|
||||||
this.end = this.min;
|
this.end = this.min;
|
||||||
@ -148,8 +148,11 @@ module.exports = function(Chart) {
|
|||||||
this.start = this.min;
|
this.start = this.min;
|
||||||
this.end = this.max;
|
this.end = this.max;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
convertTicksToLabels: function() {
|
||||||
|
this.tickValues = this.ticks.slice();
|
||||||
|
|
||||||
this.ticks = this.tickValues.slice();
|
Chart.Scale.prototype.convertTicksToLabels.call(this);
|
||||||
},
|
},
|
||||||
// Get the correct tooltip label
|
// Get the correct tooltip label
|
||||||
getLabelForIndex: function(index, datasetIndex) {
|
getLabelForIndex: function(index, datasetIndex) {
|
||||||
|
|||||||
@ -650,7 +650,6 @@ describe('Linear Scale', function() {
|
|||||||
|
|
||||||
// Reverse mode makes this count up
|
// Reverse mode makes this count up
|
||||||
expect(scale.ticks).toEqual([0, 10, 20, 30, 40, 50, 60, 70, 80]);
|
expect(scale.ticks).toEqual([0, 10, 20, 30, 40, 50, 60, 70, 80]);
|
||||||
expect(scale.ticksAsNumbers).toEqual([0, 10, 20, 30, 40, 50, 60, 70, 80]);
|
|
||||||
expect(scale.start).toBe(80);
|
expect(scale.start).toBe(80);
|
||||||
expect(scale.end).toBe(0);
|
expect(scale.end).toBe(0);
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user