Skip to content

Commit

Permalink
Improve autoSkip performance
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jun 28, 2019
1 parent d0d2bfc commit 6e3e4a1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
29 changes: 12 additions & 17 deletions src/core/core.scale.js
Expand Up @@ -304,11 +304,13 @@ var Scale = Element.extend({
ticks = [];
for (i = 0, ilen = me.ticks.length; i < ilen; ++i) {
ticks.push({
index: i,
value: me.ticks[i],
major: false
});
}
}
me._numTicks = ticks.length;

me.beforeTickToLabelConversion();

Expand Down Expand Up @@ -422,13 +424,13 @@ var Scale = Element.extend({
var me = this;
var options = me.options;
var tickOpts = options.ticks;
var ticks = me.getTicks();
var minRotation = tickOpts.minRotation || 0;
var maxRotation = tickOpts.maxRotation;
var labelRotation = minRotation;
var numTicks = me._numTicks;
var labelSizes, maxLabelWidth, maxLabelHeight, maxWidth, tickWidth, maxHeight, maxLabelDiagonal;

if (!me._isVisible() || !tickOpts.display || minRotation >= maxRotation || ticks.length <= 1 || !me.isHorizontal()) {
if (!me._isVisible() || !tickOpts.display || minRotation >= maxRotation || numTicks <= 1 || !me.isHorizontal()) {
me.labelRotation = minRotation;
return;
}
Expand All @@ -440,11 +442,11 @@ var Scale = Element.extend({
// Estimate the width of each grid based on the canvas width, the maximum
// label width and the number of tick intervals
maxWidth = Math.min(me.maxWidth, me.chart.width - maxLabelWidth);
tickWidth = options.offset ? me.maxWidth / ticks.length : maxWidth / (ticks.length - 1);
tickWidth = options.offset ? me.maxWidth / numTicks : maxWidth / (numTicks - 1);

// Allow 3 pixels x2 padding either side for label readability
if (maxLabelWidth + 6 > tickWidth) {
tickWidth = maxWidth / (ticks.length - (options.offset ? 0.5 : 1));
tickWidth = maxWidth / (numTicks - (options.offset ? 0.5 : 1));
maxHeight = me.maxHeight - getTickMarkLength(options.gridLines)
- tickOpts.padding - getScaleLabelHeight(options.scaleLabel);
maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight);
Expand Down Expand Up @@ -475,7 +477,6 @@ var Scale = Element.extend({
};

var chart = me.chart;
var ticks = me.getTicks();
var opts = me.options;
var tickOpts = opts.ticks;
var scaleLabelOpts = opts.scaleLabel;
Expand Down Expand Up @@ -523,7 +524,7 @@ var Scale = Element.extend({
minSize.height = Math.min(me.maxHeight, minSize.height + labelHeight + tickPadding);

var offsetLeft = me.getPixelForTick(0) - me.left;
var offsetRight = me.right - me.getPixelForTick(ticks.length - 1);
var offsetRight = me.right - me.getPixelForTick(me._numTicks - 1);
var paddingLeft, paddingRight;

// Ensure that our ticks are always inside the canvas. When rotated, ticks are right aligned
Expand Down Expand Up @@ -704,7 +705,7 @@ var Scale = Element.extend({
getPixelForTick: function(index) {
var me = this;
var offset = me.options.offset;
var numTicks = me._ticks.length;
var numTicks = me._numTicks;
if (index < 0 || index > numTicks - 1) {
return null;
}
Expand Down Expand Up @@ -786,11 +787,9 @@ var Scale = Element.extend({
for (i = 0; i < tickCount; i++) {
tick = ticks[i];

if (skipRatio > 1 && i % skipRatio > 0) {
// leave tick in place but make sure it's not displayed (#4635)
delete tick.label;
if (skipRatio <= 1 || i % skipRatio === 0) {
result.push(tick);
}
result.push(tick);
}
return result;
},
Expand Down Expand Up @@ -895,12 +894,8 @@ var Scale = Element.extend({
tickEnd = me.left + tl;
}

helpers.each(ticks, function(tick, index) {
// autoskipper skipped this tick (#4635)
if (helpers.isNullOrUndef(tick.label)) {
return;
}

helpers.each(ticks, function(tick) {
var index = tick.index;
var label = tick.label;
var tickFont = tick.major ? tickFonts.major : tickFonts.minor;
var lineHeight = tickFont.lineHeight;
Expand Down
2 changes: 1 addition & 1 deletion src/scales/scale.category.js
Expand Up @@ -60,7 +60,7 @@ module.exports = Scale.extend({
return me.getRightValue(chart.data.datasets[datasetIndex].data[index]);
}

return me.ticks[index - me.minIndex];
return me._getLabels()[index];
},

// Used to get data value locations. Value can either be an index or a numerical value
Expand Down
1 change: 1 addition & 0 deletions src/scales/scale.time.js
Expand Up @@ -430,6 +430,7 @@ function ticksFromTimestamps(scale, values, majorUnit) {
map[value] = i;

ticks.push({
index: i,
value: value,
major: false
});
Expand Down
10 changes: 2 additions & 8 deletions test/specs/core.scale.tests.js
Expand Up @@ -34,12 +34,6 @@ describe('Core.scale', function() {
});
}

function lastTick(chart) {
var xAxis = chart.scales['x-axis-0'];
var ticks = xAxis.getTicks();
return ticks[ticks.length - 1];
}

it('should display the last tick if it fits evenly with other ticks', function() {
var chart = getChart({
labels: [
Expand All @@ -52,7 +46,7 @@ describe('Core.scale', function() {
}]
});

expect(lastTick(chart).label).toEqual('September 2018');
expect(chart.scales['x-axis-0']._ticks.length).toEqual(9);
});

it('should not display the last tick if it does not fit evenly', function() {
Expand All @@ -71,7 +65,7 @@ describe('Core.scale', function() {
}]
});

expect(lastTick(chart).label).toBeUndefined();
expect(chart.scales['x-axis-0']._ticks.length).toEqual(13);
});
});

Expand Down

0 comments on commit 6e3e4a1

Please sign in to comment.