Skip to content

Commit

Permalink
Fix getValueForPixel in time scale (#6328)
Browse files Browse the repository at this point in the history
* Fix getValueForPixel in time scale

* Minor refactoring
  • Loading branch information
nagix authored and etimberg committed Jun 22, 2019
1 parent 07109b6 commit 1c85700
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/scales/scale.time.js
Expand Up @@ -401,7 +401,7 @@ function computeOffsets(table, ticks, min, max, options) {
}
}

return {start: start, end: end};
return {start: start, end: end, factor: 1 / (start + 1 + end)};
}

function setMajorTicks(scale, ticks, map, majorUnit) {
Expand Down Expand Up @@ -723,13 +723,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.factor;

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 @@ -758,9 +759,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 = offset / size / offsets.factor - 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 @@ -1517,6 +1517,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 @@ -1532,6 +1544,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 1c85700

Please sign in to comment.