From 41d9be33a257058f398bc05d5b212bcdbae2bb80 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Wed, 19 Jun 2019 20:43:04 +0300 Subject: [PATCH] review comments --- src/core/core.scale.js | 14 ++++++-------- src/scales/scale.category.js | 12 +++++------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/core/core.scale.js b/src/core/core.scale.js index 534d3f2d63c..5e5547d18ce 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -70,9 +70,7 @@ function getPixelForGridLine(scale, index, offsetGridLines) { if (offsetGridLines) { if (scale.getTicks().length === 1) { - lineValue -= scale.isHorizontal() ? - Math.max(lineValue - scale.left, scale.right - lineValue) : - Math.max(lineValue - scale.top, scale.bottom - lineValue); + lineValue -= Math.max(lineValue - scale._start, scale._end - lineValue); } else if (index === 0) { lineValue -= (scale.getPixelForTick(1) - lineValue) / 2; } else { @@ -454,7 +452,7 @@ var Scale = Element.extend({ // 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 / (ticks.length - (options.offset ? 0.5 : 1)); maxHeight = me.maxHeight - getTickMarkLength(options.gridLines) - tickOpts.padding - getScaleLabelHeight(options.scaleLabel); maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight); @@ -700,8 +698,8 @@ var Scale = Element.extend({ getPixelForTick: function(index) { var me = this; var offset = me.options.offset; - var tickWidth = 1 / Math.max((me._ticks.length - offset ? 0 : 1), 1); - var decimal = index * tickWidth + offset ? tickWidth / 2 : 0; + var tickWidth = 1 / Math.max(me._ticks.length - (offset ? 0 : 1), 1); + var decimal = index * tickWidth + (offset ? tickWidth / 2 : 0); return me.getPixelForDecimal(decimal); }, @@ -1134,7 +1132,7 @@ var Scale = Element.extend({ var scaleLabelX, scaleLabelY; if (me.isHorizontal()) { - scaleLabelX = me.left + ((me.right - me.left) / 2); // midpoint of the width + scaleLabelX = me.left + me.width / 2; // midpoint of the width scaleLabelY = position === 'bottom' ? me.bottom - halfLineHeight - scaleLabelPadding.bottom : me.top + halfLineHeight + scaleLabelPadding.top; @@ -1143,7 +1141,7 @@ var Scale = Element.extend({ scaleLabelX = isLeft ? me.left + halfLineHeight + scaleLabelPadding.top : me.right - halfLineHeight - scaleLabelPadding.top; - scaleLabelY = me.top + ((me.bottom - me.top) / 2); + scaleLabelY = me.top + me.height / 2; rotation = isLeft ? -0.5 * Math.PI : 0.5 * Math.PI; } diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index f7bac9f2469..9cef04ff1bb 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -67,12 +67,9 @@ module.exports = Scale.extend({ var me = this; var offset = me.options.offset; - // 1 is added because we need the length but we have the indexes - var range = Math.max(me.maxIndex + 1 - me.minIndex - (offset ? 0 : 1), 1); - var start = me.minIndex - (offset ? 0.5 : 0); return { - start: start, - range: range + start: me.minIndex - (offset ? 0.5 : 0), + range: Math.max(me.ticks.length - (offset ? 0 : 1), 1) }; }, @@ -88,7 +85,7 @@ module.exports = Scale.extend({ // If value is a data object, then index is the index in the data array, // not the index of the scale. We need to change that. - if (!helpers.isNullOrUndef(value)) { + if (!isNullOrUndef(value)) { valueCategory = me.isHorizontal() ? value.x : value.y; } if (valueCategory !== undefined || (value !== undefined && isNaN(index))) { @@ -114,7 +111,8 @@ module.exports = Scale.extend({ getValueForPixel: function(pixel) { var me = this; var params = me._getParams(); - return Math.round(params.start + me.getDecimalForPixel(pixel) * params.range); + var value = Math.round(params.start + me.getDecimalForPixel(pixel) * params.range); + return Math.min(Math.max(value, 0), me.ticks.length - 1); }, getBasePixel: function() {