Improve autoSkip performance (#6783)

* Improve autoSkip performance

* Maintain tick ordering
This commit is contained in:
Ben McCann 2019-11-27 15:24:57 -08:00 committed by Evert Timberg
parent 6d8bde48bd
commit 090b5aee1f

View File

@ -212,18 +212,6 @@ function parseTickFontOptions(options) {
return {minor: minor, major: major}; return {minor: minor, major: major};
} }
function nonSkipped(ticksToFilter) {
var filtered = [];
var item, index, len;
for (index = 0, len = ticksToFilter.length; index < len; ++index) {
item = ticksToFilter[index];
if (typeof item._index !== 'undefined') {
filtered.push(item);
}
}
return filtered;
}
function getEvenSpacing(arr) { function getEvenSpacing(arr) {
var len = arr.length; var len = arr.length;
var i, diff; var i, diff;
@ -272,29 +260,26 @@ function getMajorIndices(ticks) {
return result; return result;
} }
function skipMajors(ticks, majorIndices, spacing) { function skipMajors(ticks, newTicks, majorIndices, spacing) {
var count = 0; let count = 0;
var next = majorIndices[0]; let next = majorIndices[0];
var i, tick; let i;
spacing = Math.ceil(spacing); spacing = Math.ceil(spacing);
for (i = 0; i < ticks.length; i++) { for (i = 0; i < ticks.length; i++) {
tick = ticks[i];
if (i === next) { if (i === next) {
tick._index = i; newTicks.push(ticks[i]);
count++; count++;
next = majorIndices[count * spacing]; next = majorIndices[count * spacing];
} else {
delete tick.label;
} }
} }
} }
function skip(ticks, spacing, majorStart, majorEnd) { function skip(ticks, newTicks, spacing, majorStart, majorEnd) {
var start = valueOrDefault(majorStart, 0); const start = valueOrDefault(majorStart, 0);
var end = Math.min(valueOrDefault(majorEnd, ticks.length), ticks.length); const end = Math.min(valueOrDefault(majorEnd, ticks.length), ticks.length);
var count = 0; let count = 0;
var length, i, tick, next; let length, i, next;
spacing = Math.ceil(spacing); spacing = Math.ceil(spacing);
if (majorEnd) { if (majorEnd) {
@ -310,13 +295,10 @@ function skip(ticks, spacing, majorStart, majorEnd) {
} }
for (i = Math.max(start, 0); i < end; i++) { for (i = Math.max(start, 0); i < end; i++) {
tick = ticks[i];
if (i === next) { if (i === next) {
tick._index = i; newTicks.push(ticks[i]);
count++; count++;
next = Math.round(start + count * spacing); next = Math.round(start + count * spacing);
} else {
delete tick.label;
} }
} }
} }
@ -910,35 +892,36 @@ class Scale extends Element {
* @private * @private
*/ */
_autoSkip(ticks) { _autoSkip(ticks) {
var me = this; const me = this;
var tickOpts = me.options.ticks; const tickOpts = me.options.ticks;
var axisLength = me._length; const axisLength = me._length;
var ticksLimit = tickOpts.maxTicksLimit || axisLength / me._tickSize() + 1; const ticksLimit = tickOpts.maxTicksLimit || axisLength / me._tickSize() + 1;
var majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : []; const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : [];
var numMajorIndices = majorIndices.length; const numMajorIndices = majorIndices.length;
var first = majorIndices[0]; const first = majorIndices[0];
var last = majorIndices[numMajorIndices - 1]; const last = majorIndices[numMajorIndices - 1];
var i, ilen, spacing, avgMajorSpacing; const newTicks = [];
// If there are too many major ticks to display them all // If there are too many major ticks to display them all
if (numMajorIndices > ticksLimit) { if (numMajorIndices > ticksLimit) {
skipMajors(ticks, majorIndices, numMajorIndices / ticksLimit); skipMajors(ticks, newTicks, majorIndices, numMajorIndices / ticksLimit);
return nonSkipped(ticks); return newTicks;
} }
spacing = calculateSpacing(majorIndices, ticks, axisLength, ticksLimit); const spacing = calculateSpacing(majorIndices, ticks, axisLength, ticksLimit);
if (numMajorIndices > 0) { if (numMajorIndices > 0) {
let i, ilen;
const avgMajorSpacing = numMajorIndices > 1 ? (last - first) / (numMajorIndices - 1) : null;
skip(ticks, newTicks, spacing, helpers.isNullOrUndef(avgMajorSpacing) ? 0 : first - avgMajorSpacing, first);
for (i = 0, ilen = numMajorIndices - 1; i < ilen; i++) { for (i = 0, ilen = numMajorIndices - 1; i < ilen; i++) {
skip(ticks, spacing, majorIndices[i], majorIndices[i + 1]); skip(ticks, newTicks, spacing, majorIndices[i], majorIndices[i + 1]);
} }
avgMajorSpacing = numMajorIndices > 1 ? (last - first) / (numMajorIndices - 1) : null; skip(ticks, newTicks, spacing, last, helpers.isNullOrUndef(avgMajorSpacing) ? ticks.length : last + avgMajorSpacing);
skip(ticks, spacing, helpers.isNullOrUndef(avgMajorSpacing) ? 0 : first - avgMajorSpacing, first); return newTicks;
skip(ticks, spacing, last, helpers.isNullOrUndef(avgMajorSpacing) ? ticks.length : last + avgMajorSpacing);
return nonSkipped(ticks);
} }
skip(ticks, spacing); skip(ticks, newTicks, spacing);
return nonSkipped(ticks); return newTicks;
} }
/** /**