Skip to content

Commit

Permalink
Improve offset calculation for scale.offset option
Browse files Browse the repository at this point in the history
  • Loading branch information
nagix committed Aug 15, 2017
1 parent b9afeaf commit bc4bc9e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
52 changes: 32 additions & 20 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,27 +334,39 @@ function generate(min, max, minor, major, capacity, options) {
* Returns the right and left offsets from edges in the form of {left, right}.
* Offsets are added when the `offset` option is true.
*/
function computeOffsets(table, ticks, min, max, options) {
function computeOffsets(table, ts, min, max, options) {
var left = 0;
var right = 0;
var upper, lower;

if (options.offset && ticks.length) {
if (!options.time.min) {
upper = ticks.length > 1 ? ticks[1] : max;
lower = ticks[0];
left = (
interpolate(table, 'time', upper, 'pos') -
interpolate(table, 'time', lower, 'pos')
) / 2;
}
if (!options.time.max) {
upper = ticks[ticks.length - 1];
lower = ticks.length > 1 ? ticks[ticks.length - 2] : min;
right = (
interpolate(table, 'time', upper, 'pos') -
interpolate(table, 'time', lower, 'pos')
) / 2;
var timestamps = [];
var i, ilen, timestamp, length, upper, lower, divisor;

if (options.offset) {
[ts.labels, ts.data].forEach(function(data) {
// Remove labels outside the min/max range
for (i = 0, ilen = data.length; i < ilen; ++i) {
timestamp = data[i];
if (timestamp >= min && timestamp <= max) {
timestamps.push(timestamp);
}
}
if (timestamps.length) {
return;
}
});

length = timestamps.length;
if (length) {
divisor = length > 1 ? 2 : 1;
if (!options.time.min) {
upper = length > 1 ? interpolate(table, 'time', timestamps[1], 'pos') : 1;
lower = interpolate(table, 'time', timestamps[0], 'pos');
left = Math.max((upper - lower) / divisor - lower, 0);
}
if (!options.time.max) {
upper = interpolate(table, 'time', timestamps[length - 1], 'pos');
lower = length > 1 ? interpolate(table, 'time', timestamps[length - 2], 'pos') : 0;
right = Math.max((upper - lower) / divisor - (1 - upper), 0);
}
}
}

Expand Down Expand Up @@ -593,7 +605,7 @@ module.exports = function(Chart) {
me._minorFormat = formats[unit];
me._majorFormat = formats[majorUnit];
me._table = buildLookupTable(me._timestamps.data, min, max, options.distribution);
me._offsets = computeOffsets(me._table, ticks, min, max, options);
me._offsets = computeOffsets(me._table, me._timestamps, min, max, options);

return ticksFromTimestamps(ticks, majorUnit);
},
Expand Down
9 changes: 4 additions & 5 deletions test/specs/scale.time.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1132,12 +1132,11 @@ describe('Time scale tests', function() {
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);
var firstInterval = scale.getPixelForValue('2019') - scale.getPixelForValue('2017');
var lastInterval = scale.getPixelForValue('2042') - scale.getPixelForValue('2025');

expect(scale.getPixelForValue('2017')).toBeCloseToPixel(scale.left + firstTickInterval / 2);
expect(scale.getPixelForValue('2042')).toBeCloseToPixel(scale.left + scale.width - lastTickInterval / 2);
expect(scale.getPixelForValue('2017')).toBeCloseToPixel(scale.left + firstInterval / 2);
expect(scale.getPixelForValue('2042')).toBeCloseToPixel(scale.left + scale.width - lastInterval / 2);
});

it ('should not add offset if min and max extend the labels range', function() {
Expand Down

0 comments on commit bc4bc9e

Please sign in to comment.