]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Implement getValueForPixel for category scale
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 30 Apr 2016 12:39:18 +0000 (08:39 -0400)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sat, 30 Apr 2016 12:39:18 +0000 (08:39 -0400)
src/scales/scale.category.js
test/scale.category.tests.js

index 5b096fef18ceb071956db006fa99828c22b5ca84..6cde7200fea10d83da1d32530e00809af94d2b36 100644 (file)
@@ -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;
                }
        });
 
index 3303448b6cbcfff2dbed5bc9be378731d09192f2..c26a875c24f662e0af5c130525463b248c930c28 100644 (file)
@@ -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() {