]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Backwards pixel to value API
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 23 Apr 2016 17:27:10 +0000 (13:27 -0400)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sat, 23 Apr 2016 17:27:10 +0000 (13:27 -0400)
src/scales/scale.logarithmic.js
src/scales/scale.time.js
test/scale.time.tests.js

index 7476765db63f95f37da766eb0787f4dad24f8636..8474abb510cdb5afce5243051d834f6837686cfc 100644 (file)
@@ -164,8 +164,8 @@ module.exports = function(Chart) {
                getPixelForValue: function(value, index, datasetIndex, includeOffset) {
                        var pixel;
 
-                       var newVal = +this.getRightValue(value);
-                       var range = helpers.log10(this.end) - helpers.log10(this.start);
+                       var newVal = +this.getRightValue(value)
+;                      var range = helpers.log10(this.end) - helpers.log10(this.start);
 
                        if (this.isHorizontal()) {
 
@@ -190,13 +190,18 @@ module.exports = function(Chart) {
                },
                getValueForPixel: function(pixel) {
                        var offset;
+                       var range = helpers.log10(this.end) - helpers.log10(this.start);
+                       var value;
 
-                       if (this.isHorizontal) {
+                       if (this.isHorizontal()) {
                                var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
+                               value = this.start * Math.pow(10, (pixel - this.left - this.paddingLeft) * range / innerWidth);
                        } else {
                                var innerHeight = this.height - (this.paddingTop + this.paddingBottom);
+                               value = Math.pow(10, (this.bottom - this.paddingBottom - pixel) * range / innerHeight) / this.start;
                        }
 
+                       return value;
                }
 
        });
index 47d1fa09c691ab95c4c98e812f7205ece2d99e82..1d31bf23389c28d07156b6e6d6b09c70578eb3e3 100644 (file)
@@ -317,7 +317,8 @@ module.exports = function(Chart) {
                        }
                },
                getValueForPixel: function(pixel) {
-                       var offset = pixel - (this.isHorizontal() ? this.left + this.paddingLeft : this.top + this.paddingTop);
+                       var innerDimension = this.isHorizontal() ? this.width - (this.paddingLeft + this.paddingRight) : this.height - (this.paddingTop + this.paddingBottom);
+                       var offset = (pixel - (this.isHorizontal() ? this.left + this.paddingLeft : this.top + this.paddingTop)) / innerDimension;
                        offset *= this.scaleSizeInUnits;
                        return this.firstTick.clone().add(offset, this.tickUnit);
                },
index 722354d59465298f70b1f9508140a8ef9f62965a..64a83fdf4ed81efaa2eaf82a95db7de8c732f482 100644 (file)
@@ -18,6 +18,20 @@ describe('Time scale tests', function() {
                                                };
                                        }
                                };
+                       },
+                       toBeCloseToTime: function() {
+                               return {
+                                       compare: function(actual, expected) {
+                                               var result = false;
+
+                                               var diff = actual.diff(expected.value, expected.unit, true);
+                                               result = Math.abs(diff) < 0.25;
+
+                                               return {
+                                                       pass: result
+                                               };
+                                       }
+                               }
                        }
                });
        });
@@ -337,10 +351,16 @@ describe('Time scale tests', function() {
                scale.bottom = 38;
 
                expect(scale.getPixelForValue('', 0, 0)).toBe(81);
-               expect(scale.getValueForPixel(81)).toEqual(scale.firstTick);
+               expect(scale.getValueForPixel(81)).toBeCloseToTime({
+                       value: moment(mockData.labels[0]),
+                       unit: 'hour'
+               });
 
                expect(scale.getPixelForValue('', 6, 0)).toBe(323);
-               expect(scale.getValueForPixel(323)).toEqual(scale.lastTick);
+               expect(scale.getValueForPixel(323)).toBeCloseToTime({
+                       value: moment(mockData.labels[6]),
+                       unit: 'hour'
+               });
 
                var verticalScaleConfig = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time'));
                verticalScaleConfig.position = "left";
@@ -362,10 +382,16 @@ describe('Time scale tests', function() {
                verticalScale.bottom = 400;
 
                expect(verticalScale.getPixelForValue('', 0, 0)).toBe(38);
-               expect(verticalScale.getValueForPixel(38)).toEqual(verticalScale.firstTick);
+               expect(verticalScale.getValueForPixel(38)).toBeCloseToTime({
+                       value: moment(mockData.labels[0]),
+                       unit: 'hour'
+               });
 
                expect(verticalScale.getPixelForValue('', 6, 0)).toBe(375);
-               expect(verticalScale.getValueForPixel(375)).toEqual(verticalScale.lastTick);
+               expect(verticalScale.getValueForPixel(375)).toBeCloseToTime({
+                       value: moment(mockData.labels[6]),
+                       unit: 'hour'
+               });
        });
 
        it('should get the correct label for a data value', function() {