Skip to content

Commit

Permalink
Fix getValueForPixel in time scale
Browse files Browse the repository at this point in the history
  • Loading branch information
nagix committed Jun 10, 2019
1 parent 70b32ff commit 497464e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/scales/scale.time.js
Expand Up @@ -704,13 +704,14 @@ module.exports = Scale.extend({
*/
getPixelForOffset: function(time) {
var me = this;
var isReverse = me.options.ticks.reverse;
var offsets = me._offsets;
var size = me._horizontal ? me.width : me.height;
var start = me._horizontal ? isReverse ? me.right : me.left : isReverse ? me.bottom : me.top;
var pos = interpolate(me._table, 'time', time, 'pos');
var offset = size * (me._offsets.start + pos) / (me._offsets.start + 1 + me._offsets.end);
var offset = size * (offsets.start + pos) / (offsets.start + 1 + offsets.end);

return isReverse ? start - offset : start + offset;
return me.options.ticks.reverse ?
(me._horizontal ? me.right : me.bottom) - offset :
(me._horizontal ? me.left : me.top) + offset;
},

getPixelForValue: function(value, index, datasetIndex) {
Expand Down Expand Up @@ -739,9 +740,12 @@ module.exports = Scale.extend({

getValueForPixel: function(pixel) {
var me = this;
var offsets = me._offsets;
var size = me._horizontal ? me.width : me.height;
var start = me._horizontal ? me.left : me.top;
var pos = (size ? (pixel - start) / size : 0) * (me._offsets.start + 1 + me._offsets.start) - me._offsets.end;
var offset = me.options.ticks.reverse ?
(me._horizontal ? me.right : me.bottom) - pixel :
pixel - (me._horizontal ? me.left : me.top);
var pos = (size ? offset / size : 0) * (offsets.start + 1 + offsets.end) - offsets.start;
var time = interpolate(me._table, 'pos', pos, 'time');

// DEPRECATION, we should return time directly
Expand Down
34 changes: 34 additions & 0 deletions test/specs/scale.time.tests.js
Expand Up @@ -1512,6 +1512,18 @@ describe('Time scale tests', function() {
expect(scale.getPixelForValue('2042')).toBeCloseToPixel(scale.left);
});

it ('should reverse the values for pixels', function() {
var scale = this.chart.chart.scales.x;
expect(scale.getValueForPixel(scale.left)).toBeCloseToTime({
value: moment('2042-01-01T00:00:00'),
unit: 'hour',
});
expect(scale.getValueForPixel(scale.left + scale.width)).toBeCloseToTime({
value: moment('2017-01-01T00:00:00'),
unit: 'hour',
});
});

it ('should reverse the bars and add offsets if offset is true', function() {
var chart = this.chart;
var scale = chart.scales.x;
Expand All @@ -1527,6 +1539,28 @@ describe('Time scale tests', function() {
expect(scale.getPixelForValue('2017')).toBeCloseToPixel(scale.left + scale.width - lastTickInterval / 2);
expect(scale.getPixelForValue('2042')).toBeCloseToPixel(scale.left + firstTickInterval / 2);
});

it ('should reverse the values for pixels if offset is true', function() {
var chart = this.chart;
var scale = chart.scales.x;
var options = chart.options.scales.xAxes[0];

options.offset = true;
chart.update();

var numTicks = scale.ticks.length;
var firstTickInterval = scale.getPixelForTick(1) - scale.getPixelForTick(0);
var lastTickInterval = scale.getPixelForTick(numTicks - 1) - scale.getPixelForTick(numTicks - 2);

expect(scale.getValueForPixel(scale.left + firstTickInterval / 2)).toBeCloseToTime({
value: moment('2042-01-01T00:00:00'),
unit: 'hour',
});
expect(scale.getValueForPixel(scale.left + scale.width - lastTickInterval / 2)).toBeCloseToTime({
value: moment('2017-01-01T00:00:00'),
unit: 'hour',
});
});
});
});

Expand Down

0 comments on commit 497464e

Please sign in to comment.