From 1749d90faf74c8b2f1e22b6ba9b8e74abb8b4c28 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Mon, 22 Aug 2016 18:03:56 -0400 Subject: [PATCH] Fix issues with repeated labels in the category scale and added a test to cover this case. --- src/scales/scale.category.js | 2 +- test/scale.category.tests.js | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index bc1582202..35d5475e8 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -59,7 +59,7 @@ module.exports = function(Chart) { // 1 is added because we need the length but we have the indexes var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - ((me.options.gridLines.offsetGridLines) ? 0 : 1)), 1); - if (value !== undefined) { + if (value !== undefined && isNaN(index)) { var labels = me.getLabels(); var idx = labels.indexOf(value); index = idx !== -1 ? idx : index; diff --git a/test/scale.category.tests.js b/test/scale.category.tests.js index b7fab63eb..5307ba652 100644 --- a/test/scale.category.tests.js +++ b/test/scale.category.tests.js @@ -225,6 +225,54 @@ describe('Category scale tests', function() { expect(scale.getValueForPixel(557)).toBe(4); }); + it ('Should get the correct pixel for a value when there are repeated labels', function() { + var scaleID = 'myScale'; + + var mockData = { + datasets: [{ + yAxisID: scaleID, + data: [10, 5, 0, 25, 78] + }], + labels: ['tick1', 'tick1', 'tick3', 'tick3', 'tick_last'] + }; + + var mockContext = window.createMockContext(); + var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category')); + config.gridLines.offsetGridLines = true; + var Constructor = Chart.scaleService.getScaleConstructor('category'); + var scale = new Constructor({ + ctx: mockContext, + options: config, + chart: { + data: mockData + }, + id: scaleID + }); + + var minSize = scale.update(600, 100); + + expect(scale.width).toBe(600); + expect(scale.height).toBe(28); + expect(scale.paddingTop).toBe(0); + expect(scale.paddingBottom).toBe(0); + expect(scale.paddingLeft).toBe(28); + expect(scale.paddingRight).toBe(48); + expect(scale.labelRotation).toBe(0); + + expect(minSize).toEqual({ + width: 600, + height: 28, + }); + + scale.left = 5; + scale.top = 5; + scale.right = 605; + scale.bottom = 33; + + expect(scale.getPixelForValue('tick_1', 1, 0, false)).toBe(138); + expect(scale.getPixelForValue('tick_1', 1, 0, true)).toBe(190); + }); + it ('Should get the correct pixel for a value when horizontal and zoomed', function() { var scaleID = 'myScale';