From: Evert Timberg Date: Sat, 23 Apr 2016 17:27:10 +0000 (-0400) Subject: Backwards pixel to value API X-Git-Tag: 2.1.0~7^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c31c90b2e4b535c0595df505336a0f4816fec6c8;p=thirdparty%2FChart.js.git Backwards pixel to value API --- diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 7476765db..8474abb51 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -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; } }); diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 47d1fa09c..1d31bf233 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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); }, diff --git a/test/scale.time.tests.js b/test/scale.time.tests.js index 722354d59..64a83fdf4 100644 --- a/test/scale.time.tests.js +++ b/test/scale.time.tests.js @@ -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() {