Fix category scale autoSkip (#6847)

This commit is contained in:
Ben McCann 2019-12-18 08:01:53 -08:00 committed by Evert Timberg
parent 1127ff561b
commit 39f27f68b5

View File

@ -7,36 +7,42 @@ const defaultConfig = {
class CategoryScale extends Scale { class CategoryScale extends Scale {
_parse(raw, index) { _parse(raw, index) {
var labels = this._getLabels(); const labels = this._getLabels();
var first = labels.indexOf(raw); const first = labels.indexOf(raw);
var last = labels.lastIndexOf(raw); const last = labels.lastIndexOf(raw);
return first === -1 || first !== last ? index : first; return first === -1 || first !== last ? index : first;
} }
determineDataLimits() { determineDataLimits() {
var me = this; const me = this;
var max = me._getLabels().length - 1; const max = me._getLabels().length - 1;
me.min = Math.max(me._userMin || 0, 0); me.min = Math.max(me._userMin || 0, 0);
me.max = Math.min(me._userMax || max, max); me.max = Math.min(me._userMax || max, max);
} }
buildTicks() { buildTicks() {
var me = this; const me = this;
var labels = me._getLabels(); const min = me.min;
var min = me.min; const max = me.max;
var max = me.max; const offset = me.options.offset;
let labels = me._getLabels();
// If we are viewing some subset of labels, slice the original array // If we are viewing some subset of labels, slice the original array
labels = (min === 0 && max === labels.length - 1) ? labels : labels.slice(min, max + 1); labels = (min === 0 && max === labels.length - 1) ? labels : labels.slice(min, max + 1);
me._numLabels = labels.length;
me._valueRange = Math.max(labels.length - (offset ? 0 : 1), 1);
me._startValue = me.min - (offset ? 0.5 : 0);
return labels.map(function(l) { return labels.map(function(l) {
return {value: l}; return {value: l};
}); });
} }
getLabelForValue(value) { getLabelForValue(value) {
var me = this; const me = this;
var labels = me._getLabels(); const labels = me._getLabels();
if (value >= 0 && value < labels.length) { if (value >= 0 && value < labels.length) {
return labels[value]; return labels[value];
@ -45,9 +51,7 @@ class CategoryScale extends Scale {
} }
_configure() { _configure() {
var me = this; const me = this;
var offset = me.options.offset;
var ticks = me.ticks;
Scale.prototype._configure.call(me); Scale.prototype._configure.call(me);
@ -55,16 +59,9 @@ class CategoryScale extends Scale {
// For backward compatibility, vertical category scale reverse is inverted. // For backward compatibility, vertical category scale reverse is inverted.
me._reversePixels = !me._reversePixels; me._reversePixels = !me._reversePixels;
} }
if (!ticks) {
return;
}
me._startValue = me.min - (offset ? 0.5 : 0);
me._valueRange = Math.max(ticks.length - (offset ? 0 : 1), 1);
} }
// Used to get data value locations. Value can either be an index or a numerical value // Used to get data value locations. Value can either be an index or a numerical value
getPixelForValue(value) { getPixelForValue(value) {
var me = this; var me = this;
@ -76,15 +73,17 @@ class CategoryScale extends Scale {
} }
getPixelForTick(index) { getPixelForTick(index) {
var ticks = this.ticks; const me = this;
return index < 0 || index > ticks.length - 1 const ticks = me.ticks;
? null if (index < 0 || index > ticks.length - 1) {
: this.getPixelForValue(index + this.min); return null;
}
return this.getPixelForValue(index * me._numLabels / ticks.length + this.min);
} }
getValueForPixel(pixel) { getValueForPixel(pixel) {
var me = this; const me = this;
var value = Math.round(me._startValue + me.getDecimalForPixel(pixel) * me._valueRange); const value = Math.round(me._startValue + me.getDecimalForPixel(pixel) * me._valueRange);
return Math.min(Math.max(value, 0), me.ticks.length - 1); return Math.min(Math.max(value, 0), me.ticks.length - 1);
} }