From 0e196fc514d231ad3a17ab3d4b640dab33244fb8 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Fri, 27 Mar 2020 16:48:04 -0700 Subject: [PATCH] getPixelForTick cleanup (#7225) --- src/core/core.scale.js | 13 +++++-------- src/scales/scale.category.js | 2 ++ src/scales/scale.linear.js | 8 -------- src/scales/scale.logarithmic.js | 8 -------- src/scales/scale.time.js | 12 ------------ 5 files changed, 7 insertions(+), 36 deletions(-) diff --git a/src/core/core.scale.js b/src/core/core.scale.js index 606bf0cd6..567a62973 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -960,14 +960,11 @@ export default class Scale extends Element { * @return {number} */ getPixelForTick(index) { - const me = this; - const offset = me.options.offset; - const numTicks = me.ticks.length; - const tickWidth = 1 / Math.max(numTicks - (offset ? 0 : 1), 1); - - return index < 0 || index > numTicks - 1 - ? null - : me.getPixelForDecimal(index * tickWidth + (offset ? tickWidth / 2 : 0)); + const ticks = this.ticks; + if (index < 0 || index > ticks.length - 1) { + return null; + } + return this.getPixelForValue(ticks[index].value); } /** diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index 9c998b00c..57aa5439b 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -88,6 +88,8 @@ export default class CategoryScale extends Scale { return me.getPixelForDecimal((value - me._startValue) / me._valueRange); } + // Must override base implementation becuase it calls getPixelForValue + // and category scale can have duplicate values getPixelForTick(index) { const me = this; const ticks = me.ticks; diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index a2a4adcca..7bff82311 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -66,12 +66,4 @@ export default class LinearScale extends LinearScaleBase { getValueForPixel(pixel) { return this._startValue + this.getDecimalForPixel(pixel) * this._valueRange; } - - getPixelForTick(index) { - const ticks = this.ticks; - if (index < 0 || index > ticks.length - 1) { - return null; - } - return this.getPixelForValue(ticks[index].value); - } } diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 2b0d5cba0..2c750c059 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -158,14 +158,6 @@ export default class LogarithmicScale extends Scale { return value === undefined ? '0' : new Intl.NumberFormat(this.options.locale).format(value); } - getPixelForTick(index) { - const ticks = this.ticks; - if (index < 0 || index > ticks.length - 1) { - return null; - } - return this.getPixelForValue(ticks[index].value); - } - /** * @protected */ diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 054d76d95..945bcc082 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -757,18 +757,6 @@ export default class TimeScale extends Scale { return me.getPixelForDecimal((offsets.start + pos) * offsets.factor); } - /** - * @param {number} index - * @return {number} - */ - getPixelForTick(index) { - const ticks = this.ticks; - if (index < 0 || index > ticks.length - 1) { - return null; - } - return this.getPixelForValue(ticks[index].value); - } - /** * @param {number} pixel * @return {number} -- 2.47.2