Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ export default class TimeScale extends Scale {
*/
getValueForPixel(pixel) {
const offsets = this._offsets;
const pos = this.getDecimalForPixel(pixel) / offsets.factor - offsets.end;
const pos = this.getDecimalForPixel(pixel) / offsets.factor - offsets.start;
return this.min + pos * (this.max - this.min);
}

Expand Down
2 changes: 1 addition & 1 deletion src/scales/scale.timeseries.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class TimeSeriesScale extends TimeScale {
*/
getValueForPixel(pixel) {
const offsets = this._offsets;
const decimal = this.getDecimalForPixel(pixel) / offsets.factor - offsets.end;
const decimal = this.getDecimalForPixel(pixel) / offsets.factor - offsets.start;
return interpolate(this._table, decimal * this._tableRange + this._minPos, true);
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/specs/scale.time.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,45 @@ describe('Time scale tests', function() {
expect(scale.getPixelForDecimal(1.0)).toBeCloseToPixel(512);
});

it('should invert getPixelForValue when the axis has asymmetric offsets', function() {
// Irregularly spaced labels so the first and last gaps differ, giving
// offsets.start !== offsets.end once offset is enabled.
var chart = window.acquireChart({
type: 'line',
data: {
labels: [
'2015-01-01T00:00:00',
'2015-01-01T06:00:00',
'2015-01-02T00:00:00',
'2015-01-05T00:00:00'
],
datasets: [{data: [1, 2, 3, 4]}]
},
options: {
scales: {
x: {
type: 'time',
offset: true,
time: {unit: 'hour'},
ticks: {source: 'labels'}
}
}
}
}, {canvas: {width: 600, height: 200}});

var scale = chart.scales.x;
// Guard the premise of the test: the offsets really are asymmetric here.
expect(scale._offsets.start).not.toEqual(scale._offsets.end);

// getValueForPixel is the inverse of getPixelForValue, so a round trip
// through an interior value should return that value.
var value = moment('2015-01-02T00:00:00').valueOf();
expect(scale.getValueForPixel(scale.getPixelForValue(value))).toBeCloseToTime({
value: moment(value),
unit: 'hour'
});
});

['data', 'labels'].forEach(function(source) {
['timeseries', 'time'].forEach(function(type) {
describe('when ticks.source is "' + source + '" and scale type is "' + type + '"', function() {
Expand Down