Skip to content

Commit cb95a36

Browse files
benmccannetimberg
authored andcommitted
Fix value passed to getPixelForTick (#6724)
1 parent 4feaea9 commit cb95a36

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

docs/getting-started/v3-migration.md

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

101101
##### Ticks
102102

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

src/core/core.scale.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,11 @@ class Scale extends Element {
508508
me.afterFit();
509509

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

513513
if (samplingEnabled) {
514514
// Generate labels using all non-skipped ticks
515-
me._convertTicksToLabels(me._ticksToDraw);
515+
me._convertTicksToLabels(me.ticks);
516516
}
517517

518518
// IMPORTANT: after this point, we consider that `this.ticks` will NEVER change!
@@ -987,14 +987,12 @@ class Scale extends Element {
987987
var position = options.position;
988988
var offsetGridLines = gridLines.offsetGridLines;
989989
var isHorizontal = me.isHorizontal();
990-
var ticks = me._ticksToDraw;
990+
var ticks = me.ticks;
991991
var ticksLength = ticks.length + (offsetGridLines ? 1 : 0);
992-
var context;
993-
994992
var tl = getTickMarkLength(gridLines);
995993
var items = [];
996994

997-
context = {
995+
var context = {
998996
scale: me,
999997
tick: ticks[0],
1000998
};
@@ -1046,7 +1044,7 @@ class Scale extends Element {
10461044
const borderDash = gridLines.borderDash || [];
10471045
const borderDashOffset = resolve([gridLines.borderDashOffset], context, i);
10481046

1049-
lineValue = getPixelForGridLine(me, tick._index || i, offsetGridLines);
1047+
lineValue = getPixelForGridLine(me, i, offsetGridLines);
10501048

10511049
// Skip if the pixel is out of the range
10521050
if (lineValue === undefined) {
@@ -1093,7 +1091,7 @@ class Scale extends Element {
10931091
var position = options.position;
10941092
var isMirrored = optionTicks.mirror;
10951093
var isHorizontal = me.isHorizontal();
1096-
var ticks = me._ticksToDraw;
1094+
var ticks = me.ticks;
10971095
var fonts = parseTickFontOptions(optionTicks);
10981096
var tickPadding = optionTicks.padding;
10991097
var tl = getTickMarkLength(options.gridLines);
@@ -1119,7 +1117,7 @@ class Scale extends Element {
11191117
tick = ticks[i];
11201118
label = tick.label;
11211119

1122-
pixel = me.getPixelForTick(tick._index || i) + optionTicks.labelOffset;
1120+
pixel = me.getPixelForTick(i) + optionTicks.labelOffset;
11231121
font = tick.major ? fonts.major : fonts.minor;
11241122
lineHeight = font.lineHeight;
11251123
lineCount = isArray(label) ? label.length : 1;
@@ -1164,7 +1162,7 @@ class Scale extends Element {
11641162
var alignPixel = helpers._alignPixel;
11651163
var context = {
11661164
scale: me,
1167-
tick: me._ticksToDraw[0],
1165+
tick: me.ticks[0],
11681166
};
11691167
var axisWidth = gridLines.drawBorder ? resolve([gridLines.lineWidth, 0], context, 0) : 0;
11701168
var items = me._gridLineItems || (me._gridLineItems = me._computeGridLineItems(chartArea));
@@ -1206,7 +1204,7 @@ class Scale extends Element {
12061204
var firstLineWidth = axisWidth;
12071205
context = {
12081206
scale: me,
1209-
tick: me._ticksToDraw[items.ticksLength - 1],
1207+
tick: me.ticks[items.ticksLength - 1],
12101208
};
12111209
var lastLineWidth = resolve([gridLines.lineWidth, 1], context, items.ticksLength - 1);
12121210
var borderValue = items.borderValue;

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)