Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getValueForPixel in time scale #6328

Merged
merged 2 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
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 ticksFromTimestamps(scale, values, majorUnit) {
Expand Down 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.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 @@ -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 = 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
Original file line number Diff line number Diff line change
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