Skip to content

Commit 675a031

Browse files
committed
Fix value passed to getPixelForTick
1 parent a3392e0 commit 675a031

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

docs/getting-started/v3-migration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Chart.js is no longer providing the `Chart.bundle.js` and `Chart.bundle.min.js`.
9898

9999
##### Ticks
100100

101+
* When `autoSkip` is enabled, `scale.ticks` now contains only the non-skipped ticks instead of all ticks.
101102
* `scale.ticks` now contains objects instead of strings
102103
* `buildTicks` is now expected to return tick objects
103104
* `afterBuildTicks` now has no parameters like the other callbacks

src/core/core.scale.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,11 @@ class Scale extends Element {
484484
me.afterFit();
485485

486486
// Auto-skip
487-
me._ticksToDraw = tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto') ? me._autoSkip(me.ticks) : me.ticks;
487+
me.ticks = tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto') ? me._autoSkip(me.ticks) : me.ticks;
488488

489489
if (samplingEnabled) {
490490
// Generate labels using all non-skipped ticks
491-
me._convertTicksToLabels(me._ticksToDraw);
491+
me._convertTicksToLabels(me.ticks);
492492
}
493493

494494
// IMPORTANT: after this point, we consider that `this.ticks` will NEVER change!
@@ -963,8 +963,7 @@ class Scale extends Element {
963963
var position = options.position;
964964
var offsetGridLines = gridLines.offsetGridLines;
965965
var isHorizontal = me.isHorizontal();
966-
var ticks = me._ticksToDraw;
967-
var ticksLength = ticks.length + (offsetGridLines ? 1 : 0);
966+
var ticksLength = me.ticks.length + (offsetGridLines ? 1 : 0);
968967

969968
var tl = getTickMarkLength(gridLines);
970969
var items = [];
@@ -974,7 +973,7 @@ class Scale extends Element {
974973
var alignBorderValue = function(pixel) {
975974
return alignPixel(chart, pixel, axisWidth);
976975
};
977-
var borderValue, i, tick, lineValue, alignedLineValue;
976+
var borderValue, i, lineValue, alignedLineValue;
978977
var tx1, ty1, tx2, ty2, x1, y1, x2, y2;
979978

980979
if (position === 'top') {
@@ -1004,14 +1003,12 @@ class Scale extends Element {
10041003
}
10051004

10061005
for (i = 0; i < ticksLength; ++i) {
1007-
tick = ticks[i] || {};
1008-
10091006
const lineWidth = valueAtIndexOrDefault(gridLines.lineWidth, i, 1);
10101007
const lineColor = valueAtIndexOrDefault(gridLines.color, i, 'rgba(0,0,0,0.1)');
10111008
const borderDash = gridLines.borderDash || [];
10121009
const borderDashOffset = gridLines.borderDashOffset || 0.0;
10131010

1014-
lineValue = getPixelForGridLine(me, tick._index || i, offsetGridLines);
1011+
lineValue = getPixelForGridLine(me, i, offsetGridLines);
10151012

10161013
// Skip if the pixel is out of the range
10171014
if (lineValue === undefined) {
@@ -1058,7 +1055,7 @@ class Scale extends Element {
10581055
var position = options.position;
10591056
var isMirrored = optionTicks.mirror;
10601057
var isHorizontal = me.isHorizontal();
1061-
var ticks = me._ticksToDraw;
1058+
var ticks = me.ticks;
10621059
var fonts = parseTickFontOptions(optionTicks);
10631060
var tickPadding = optionTicks.padding;
10641061
var tl = getTickMarkLength(options.gridLines);
@@ -1084,7 +1081,7 @@ class Scale extends Element {
10841081
tick = ticks[i];
10851082
label = tick.label;
10861083

1087-
pixel = me.getPixelForTick(tick._index || i) + optionTicks.labelOffset;
1084+
pixel = me.getPixelForTick(i) + optionTicks.labelOffset;
10881085
font = tick.major ? fonts.major : fonts.minor;
10891086
lineHeight = font.lineHeight;
10901087
lineCount = isArray(label) ? label.length : 1;

test/specs/core.scale.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('Core.scale', function() {
7575
}]
7676
});
7777

78-
expect(lastTick(chart).label).toBeUndefined();
78+
expect(lastTick(chart).label).toEqual('January 2020');
7979
});
8080
});
8181

test/specs/scale.time.tests.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('Time scale tests', function() {
126126
var ticks = getLabels(scale);
127127

128128
// `bounds === 'data'`: first and last ticks removed since outside the data range
129-
expect(ticks.length).toEqual(217);
129+
expect(ticks.length).toEqual(44);
130130
});
131131

132132
it('should accept labels as date objects', function() {
@@ -137,7 +137,7 @@ describe('Time scale tests', function() {
137137
var ticks = getLabels(scale);
138138

139139
// `bounds === 'data'`: first and last ticks removed since outside the data range
140-
expect(ticks.length).toEqual(217);
140+
expect(ticks.length).toEqual(44);
141141
});
142142

143143
it('should accept data as xy points', function() {
@@ -185,7 +185,7 @@ describe('Time scale tests', function() {
185185
var ticks = getLabels(xScale);
186186

187187
// `bounds === 'data'`: first and last ticks removed since outside the data range
188-
expect(ticks.length).toEqual(217);
188+
expect(ticks.length).toEqual(37);
189189
});
190190

191191
it('should accept data as ty points', function() {
@@ -233,7 +233,7 @@ describe('Time scale tests', function() {
233233
var ticks = getLabels(tScale);
234234

235235
// `bounds === 'data'`: first and last ticks removed since outside the data range
236-
expect(ticks.length).toEqual(217);
236+
expect(ticks.length).toEqual(37);
237237
});
238238
});
239239

@@ -704,7 +704,7 @@ describe('Time scale tests', function() {
704704
it('should get the correct labels for ticks', function() {
705705
var labels = getLabels(this.scale);
706706

707-
expect(labels.length).toEqual(61);
707+
expect(labels.length).toEqual(21);
708708
expect(labels[0]).toEqual('<8:00:00>');
709709
expect(labels[labels.length - 1]).toEqual('<8:01:00>');
710710
});
@@ -717,7 +717,7 @@ describe('Time scale tests', function() {
717717
chart.update();
718718

719719
var labels = getLabels(this.scale);
720-
expect(labels.length).toEqual(61);
720+
expect(labels.length).toEqual(21);
721721
expect(labels[0]).toEqual('{8:00:00}');
722722
expect(labels[labels.length - 1]).toEqual('{8:01:00}');
723723
});
@@ -765,7 +765,7 @@ describe('Time scale tests', function() {
765765
it('should get the correct labels for major and minor ticks', function() {
766766
var labels = getLabels(this.scale);
767767

768-
expect(labels.length).toEqual(61);
768+
expect(labels.length).toEqual(21);
769769
expect(labels[0]).toEqual('[[8:00 pm]]');
770770
expect(labels[Math.floor(labels.length / 2)]).toEqual('(8:00:30 pm)');
771771
expect(labels[labels.length - 1]).toEqual('[[8:01 pm]]');
@@ -777,7 +777,7 @@ describe('Time scale tests', function() {
777777
chart.update();
778778

779779
var labels = getLabels(this.scale);
780-
expect(labels.length).toEqual(61);
780+
expect(labels.length).toEqual(21);
781781
expect(labels[0]).toEqual('(8:00:00 pm)');
782782
expect(labels[labels.length - 1]).toEqual('(8:01:00 pm)');
783783
});
@@ -788,7 +788,7 @@ describe('Time scale tests', function() {
788788
chart.update();
789789

790790
var labels = getLabels(this.scale);
791-
expect(labels.length).toEqual(61);
791+
expect(labels.length).toEqual(21);
792792
expect(labels[0]).toEqual('<8:00 pm>');
793793
expect(labels[labels.length - 1]).toEqual('<8:01 pm>');
794794
});
@@ -799,7 +799,7 @@ describe('Time scale tests', function() {
799799
chart.update();
800800

801801
var labels = getLabels(this.scale);
802-
expect(labels.length).toEqual(61);
802+
expect(labels.length).toEqual(21);
803803
expect(labels[0]).toEqual('[[8:00 pm]]');
804804
expect(labels[Math.floor(labels.length / 2)]).toEqual('<8:00:30 pm>');
805805
expect(labels[labels.length - 1]).toEqual('[[8:01 pm]]');
@@ -1469,6 +1469,37 @@ describe('Time scale tests', function() {
14691469
expect(scale.getPixelForValue('2012')).toBeCloseToPixel(scale.left);
14701470
expect(scale.getPixelForValue('2051')).toBeCloseToPixel(scale.left + scale.width);
14711471
});
1472+
});
1473+
});
1474+
});
1475+
1476+
['data', 'labels'].forEach(function(source) {
1477+
['series', 'linear'].forEach(function(distribution) {
1478+
describe('when ticks.source is "' + source + '" and distribution is "' + distribution + '"', function() {
1479+
beforeEach(function() {
1480+
this.chart = window.acquireChart({
1481+
type: 'line',
1482+
data: {
1483+
labels: ['2017', '2019', '2020', '2025', '2042'],
1484+
datasets: [{data: [0, 1, 2, 3, 4, 5]}]
1485+
},
1486+
options: {
1487+
scales: {
1488+
xAxes: [{
1489+
id: 'x',
1490+
type: 'time',
1491+
time: {
1492+
parser: 'YYYY'
1493+
},
1494+
ticks: {
1495+
source: source
1496+
},
1497+
distribution: distribution
1498+
}]
1499+
}
1500+
}
1501+
});
1502+
});
14721503

14731504
it ('should add offset if min and max extend the labels range and offset is true', function() {
14741505
var chart = this.chart;
@@ -1816,7 +1847,7 @@ describe('Time scale tests', function() {
18161847

18171848
var scale = chart.scales.xScale0;
18181849

1819-
var labels = scale._ticksToDraw.map(function(t) {
1850+
var labels = scale.ticks.map(function(t) {
18201851
return t.label;
18211852
});
18221853
expect(labels).toEqual([

0 commit comments

Comments
 (0)