Skip to content

Commit 090b5ae

Browse files
benmccannetimberg
authored andcommitted
Improve autoSkip performance (#6783)
* Improve autoSkip performance * Maintain tick ordering
1 parent 6d8bde4 commit 090b5ae

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

src/core/core.scale.js

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,6 @@ function parseTickFontOptions(options) {
212212
return {minor: minor, major: major};
213213
}
214214

215-
function nonSkipped(ticksToFilter) {
216-
var filtered = [];
217-
var item, index, len;
218-
for (index = 0, len = ticksToFilter.length; index < len; ++index) {
219-
item = ticksToFilter[index];
220-
if (typeof item._index !== 'undefined') {
221-
filtered.push(item);
222-
}
223-
}
224-
return filtered;
225-
}
226-
227215
function getEvenSpacing(arr) {
228216
var len = arr.length;
229217
var i, diff;
@@ -272,29 +260,26 @@ function getMajorIndices(ticks) {
272260
return result;
273261
}
274262

275-
function skipMajors(ticks, majorIndices, spacing) {
276-
var count = 0;
277-
var next = majorIndices[0];
278-
var i, tick;
263+
function skipMajors(ticks, newTicks, majorIndices, spacing) {
264+
let count = 0;
265+
let next = majorIndices[0];
266+
let i;
279267

280268
spacing = Math.ceil(spacing);
281269
for (i = 0; i < ticks.length; i++) {
282-
tick = ticks[i];
283270
if (i === next) {
284-
tick._index = i;
271+
newTicks.push(ticks[i]);
285272
count++;
286273
next = majorIndices[count * spacing];
287-
} else {
288-
delete tick.label;
289274
}
290275
}
291276
}
292277

293-
function skip(ticks, spacing, majorStart, majorEnd) {
294-
var start = valueOrDefault(majorStart, 0);
295-
var end = Math.min(valueOrDefault(majorEnd, ticks.length), ticks.length);
296-
var count = 0;
297-
var length, i, tick, next;
278+
function skip(ticks, newTicks, spacing, majorStart, majorEnd) {
279+
const start = valueOrDefault(majorStart, 0);
280+
const end = Math.min(valueOrDefault(majorEnd, ticks.length), ticks.length);
281+
let count = 0;
282+
let length, i, next;
298283

299284
spacing = Math.ceil(spacing);
300285
if (majorEnd) {
@@ -310,13 +295,10 @@ function skip(ticks, spacing, majorStart, majorEnd) {
310295
}
311296

312297
for (i = Math.max(start, 0); i < end; i++) {
313-
tick = ticks[i];
314298
if (i === next) {
315-
tick._index = i;
299+
newTicks.push(ticks[i]);
316300
count++;
317301
next = Math.round(start + count * spacing);
318-
} else {
319-
delete tick.label;
320302
}
321303
}
322304
}
@@ -910,35 +892,36 @@ class Scale extends Element {
910892
* @private
911893
*/
912894
_autoSkip(ticks) {
913-
var me = this;
914-
var tickOpts = me.options.ticks;
915-
var axisLength = me._length;
916-
var ticksLimit = tickOpts.maxTicksLimit || axisLength / me._tickSize() + 1;
917-
var majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : [];
918-
var numMajorIndices = majorIndices.length;
919-
var first = majorIndices[0];
920-
var last = majorIndices[numMajorIndices - 1];
921-
var i, ilen, spacing, avgMajorSpacing;
895+
const me = this;
896+
const tickOpts = me.options.ticks;
897+
const axisLength = me._length;
898+
const ticksLimit = tickOpts.maxTicksLimit || axisLength / me._tickSize() + 1;
899+
const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : [];
900+
const numMajorIndices = majorIndices.length;
901+
const first = majorIndices[0];
902+
const last = majorIndices[numMajorIndices - 1];
903+
const newTicks = [];
922904

923905
// If there are too many major ticks to display them all
924906
if (numMajorIndices > ticksLimit) {
925-
skipMajors(ticks, majorIndices, numMajorIndices / ticksLimit);
926-
return nonSkipped(ticks);
907+
skipMajors(ticks, newTicks, majorIndices, numMajorIndices / ticksLimit);
908+
return newTicks;
927909
}
928910

929-
spacing = calculateSpacing(majorIndices, ticks, axisLength, ticksLimit);
911+
const spacing = calculateSpacing(majorIndices, ticks, axisLength, ticksLimit);
930912

931913
if (numMajorIndices > 0) {
914+
let i, ilen;
915+
const avgMajorSpacing = numMajorIndices > 1 ? (last - first) / (numMajorIndices - 1) : null;
916+
skip(ticks, newTicks, spacing, helpers.isNullOrUndef(avgMajorSpacing) ? 0 : first - avgMajorSpacing, first);
932917
for (i = 0, ilen = numMajorIndices - 1; i < ilen; i++) {
933-
skip(ticks, spacing, majorIndices[i], majorIndices[i + 1]);
918+
skip(ticks, newTicks, spacing, majorIndices[i], majorIndices[i + 1]);
934919
}
935-
avgMajorSpacing = numMajorIndices > 1 ? (last - first) / (numMajorIndices - 1) : null;
936-
skip(ticks, spacing, helpers.isNullOrUndef(avgMajorSpacing) ? 0 : first - avgMajorSpacing, first);
937-
skip(ticks, spacing, last, helpers.isNullOrUndef(avgMajorSpacing) ? ticks.length : last + avgMajorSpacing);
938-
return nonSkipped(ticks);
920+
skip(ticks, newTicks, spacing, last, helpers.isNullOrUndef(avgMajorSpacing) ? ticks.length : last + avgMajorSpacing);
921+
return newTicks;
939922
}
940-
skip(ticks, spacing);
941-
return nonSkipped(ticks);
923+
skip(ticks, newTicks, spacing);
924+
return newTicks;
942925
}
943926

944927
/**

0 commit comments

Comments
 (0)