Skip to content

Commit a82ef8a

Browse files
committed
Fix value passed to getPixelForTick
1 parent fef2a13 commit a82ef8a

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
@@ -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,14 +963,12 @@ class Scale extends Element {
963963
var position = options.position;
964964
var offsetGridLines = gridLines.offsetGridLines;
965965
var isHorizontal = me.isHorizontal();
966-
var ticks = me._ticksToDraw;
966+
var ticks = me.ticks;
967967
var ticksLength = ticks.length + (offsetGridLines ? 1 : 0);
968-
var context;
969-
970968
var tl = getTickMarkLength(gridLines);
971969
var items = [];
972970

973-
context = {
971+
var context = {
974972
scale: me,
975973
tick: ticks[0],
976974
};
@@ -1022,7 +1020,7 @@ class Scale extends Element {
10221020
const borderDash = gridLines.borderDash || [];
10231021
const borderDashOffset = resolve([gridLines.borderDashOffset], context, i);
10241022

1025-
lineValue = getPixelForGridLine(me, tick._index || i, offsetGridLines);
1023+
lineValue = getPixelForGridLine(me, i, offsetGridLines);
10261024

10271025
// Skip if the pixel is out of the range
10281026
if (lineValue === undefined) {
@@ -1069,7 +1067,7 @@ class Scale extends Element {
10691067
var position = options.position;
10701068
var isMirrored = optionTicks.mirror;
10711069
var isHorizontal = me.isHorizontal();
1072-
var ticks = me._ticksToDraw;
1070+
var ticks = me.ticks;
10731071
var fonts = parseTickFontOptions(optionTicks);
10741072
var tickPadding = optionTicks.padding;
10751073
var tl = getTickMarkLength(options.gridLines);
@@ -1095,7 +1093,7 @@ class Scale extends Element {
10951093
tick = ticks[i];
10961094
label = tick.label;
10971095

1098-
pixel = me.getPixelForTick(tick._index || i) + optionTicks.labelOffset;
1096+
pixel = me.getPixelForTick(i) + optionTicks.labelOffset;
10991097
font = tick.major ? fonts.major : fonts.minor;
11001098
lineHeight = font.lineHeight;
11011099
lineCount = isArray(label) ? label.length : 1;
@@ -1140,7 +1138,7 @@ class Scale extends Element {
11401138
var alignPixel = helpers._alignPixel;
11411139
var context = {
11421140
scale: me,
1143-
tick: me._ticksToDraw[0],
1141+
tick: me.ticks[0],
11441142
};
11451143
var axisWidth = gridLines.drawBorder ? resolve([gridLines.lineWidth, 0], context, 0) : 0;
11461144
var items = me._gridLineItems || (me._gridLineItems = me._computeGridLineItems(chartArea));
@@ -1182,7 +1180,7 @@ class Scale extends Element {
11821180
var firstLineWidth = axisWidth;
11831181
context = {
11841182
scale: me,
1185-
tick: me._ticksToDraw[items.ticksLength - 1],
1183+
tick: me.ticks[items.ticksLength - 1],
11861184
};
11871185
var lastLineWidth = resolve([gridLines.lineWidth, 1], context, items.ticksLength - 1);
11881186
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)