From cb54f30c970672488a239a2d371d55dfe3fb8864 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sat, 30 Apr 2016 08:39:18 -0400 Subject: [PATCH] Implement getValueForPixel for category scale --- src/scales/scale.category.js | 21 +++++++++++++++++++++ test/scale.category.tests.js | 11 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index 5b096fef1..6cde7200f 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -69,6 +69,27 @@ module.exports = function(Chart) { }, getPixelForTick: function(index, includeOffset) { return this.getPixelForValue(this.ticks[index], index + this.minIndex, null, includeOffset); + }, + getValueForPixel: function(pixel) + { + var value +; var offsetAmt = Math.max((this.ticks.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1); + var horz = this.isHorizontal(); + var innerDimension = horz ? this.width - (this.paddingLeft + this.paddingRight) : this.height - (this.paddingTop + this.paddingBottom); + var valueDimension = innerDimension / offsetAmt; + + if (this.options.gridLines.offsetGridLines) { + pixel -= (valueDimension / 2); + } + pixel -= horz ? this.paddingLeft : this.paddingTop; + + if (pixel <= 0) { + value = 0; + } else { + value = Math.round(pixel / valueDimension); + } + + return value; } }); diff --git a/test/scale.category.tests.js b/test/scale.category.tests.js index 3303448b6..c26a875c2 100644 --- a/test/scale.category.tests.js +++ b/test/scale.category.tests.js @@ -146,17 +146,23 @@ describe('Category scale tests', function() { expect(scale.getPixelForValue(0, 0, 0, false)).toBe(33); expect(scale.getPixelForValue(0, 0, 0, true)).toBe(85); + expect(scale.getValueForPixel(33)).toBe(0); + expect(scale.getValueForPixel(85)).toBe(0); expect(scale.getPixelForValue(0, 4, 0, false)).toBe(452); expect(scale.getPixelForValue(0, 4, 0, true)).toBe(505); + expect(scale.getValueForPixel(452)).toBe(4); + expect(scale.getValueForPixel(505)).toBe(4); config.gridLines.offsetGridLines = false; expect(scale.getPixelForValue(0, 0, 0, false)).toBe(33); expect(scale.getPixelForValue(0, 0, 0, true)).toBe(33); + expect(scale.getValueForPixel(33)).toBe(0); expect(scale.getPixelForValue(0, 4, 0, false)).toBe(557); expect(scale.getPixelForValue(0, 4, 0, true)).toBe(557); + expect(scale.getValueForPixel(557)).toBe(4); }); it ('Should get the correct pixel for a value when horizontal and zoomed', function() { @@ -268,17 +274,22 @@ describe('Category scale tests', function() { expect(scale.getPixelForValue(0, 0, 0, false)).toBe(11); expect(scale.getPixelForValue(0, 0, 0, true)).toBe(30); + expect(scale.getValueForPixel(11)).toBe(0); + expect(scale.getValueForPixel(30)).toBe(0); expect(scale.getPixelForValue(0, 4, 0, false)).toBe(161); expect(scale.getPixelForValue(0, 4, 0, true)).toBe(180); + expect(scale.getValueForPixel(161)).toBe(4); config.gridLines.offsetGridLines = false; expect(scale.getPixelForValue(0, 0, 0, false)).toBe(11); expect(scale.getPixelForValue(0, 0, 0, true)).toBe(11); + expect(scale.getValueForPixel(11)).toBe(0); expect(scale.getPixelForValue(0, 4, 0, false)).toBe(199); expect(scale.getPixelForValue(0, 4, 0, true)).toBe(199); + expect(scale.getValueForPixel(199)).toBe(4); }); it ('should get the correct pixel for a value when vertical and zoomed', function() { -- 2.47.3