const me = this;
const tickOpts = me.options.ticks;
const axisLength = me._length;
- const ticksLimit = tickOpts.maxTicksLimit || axisLength / me._tickSize() + 1;
+ const ticksLimit = tickOpts.maxTicksLimit || axisLength / me._tickSize();
const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : [];
const numMajorIndices = majorIndices.length;
const first = majorIndices[0];
const format = displayFormats[timeOpts.unit] || displayFormats.millisecond;
const exampleLabel = me._tickFormatFunction(exampleTime, 0, ticksFromTimestamps(me, [exampleTime], me._majorUnit), format);
const size = me._getLabelSize(exampleLabel);
- let capacity = Math.floor(me.isHorizontal() ? me.width / size.w : me.height / size.h);
-
- if (me.options.offset) {
- capacity--;
- }
-
+ // subtract 1 - if offset then there's one less label than tick
+ // if not offset then one half label padding is added to each end leaving room for one less label
+ const capacity = Math.floor(me.isHorizontal() ? me.width / size.w : me.height / size.h) - 1;
return capacity > 0 ? capacity : 1;
}
}
this.chart = window.acquireChart({
type: 'line',
data: {
- labels: ['2017', '2019', '2020', '2025', '2042'],
- datasets: [{data: [0, 1, 2, 3, 4, 5]}]
+ labels: ['2017', '2019', '2020', '2025'],
+ datasets: [{data: [0, 1, 2, 3, 4]}]
},
options: {
scales: {
var scale = this.chart.scales.x;
expect(scale.getPixelForValue(moment('2017').valueOf())).toBeCloseToPixel(scale.left);
- expect(scale.getPixelForValue(moment('2042').valueOf())).toBeCloseToPixel(scale.left + scale.width);
+ expect(scale.getPixelForValue(moment('2025').valueOf())).toBeCloseToPixel(scale.left + scale.width);
});
it ('should add offset from the edges if offset is true', function() {
var lastTickInterval = scale.getPixelForTick(numTicks - 1) - scale.getPixelForTick(numTicks - 2);
expect(scale.getPixelForValue(moment('2017').valueOf())).toBeCloseToPixel(scale.left + firstTickInterval / 2);
- expect(scale.getPixelForValue(moment('2042').valueOf())).toBeCloseToPixel(scale.left + scale.width - lastTickInterval / 2);
+ expect(scale.getPixelForValue(moment('2025').valueOf())).toBeCloseToPixel(scale.left + scale.width - lastTickInterval / 2);
});
it ('should not add offset if min and max extend the labels range', function() {