Skip to content

Commit

Permalink
{start, end} instead of {left, right} for internal offsets.
Browse files Browse the repository at this point in the history
actually reverse the ticks, to be combatible with other implementations
revert change to core.scale.js (not needed anymore)
  • Loading branch information
kurkle committed Dec 20, 2018
1 parent 0fad2b6 commit ef52acb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/core/core.scale.js
Expand Up @@ -352,7 +352,7 @@ module.exports = Element.extend({
var cosRotation, sinRotation;

// Allow 3 pixels x2 padding either side for label readability
var tickWidth = Math.abs(me.getPixelForTick(1) - me.getPixelForTick(0)) - 6;
var tickWidth = me.getPixelForTick(1) - me.getPixelForTick(0) - 6;

// Max label rotation can be set or default to 90 - also act as a loop counter
while (labelWidth > tickWidth && labelRotation < tickOpts.maxRotation) {
Expand Down
20 changes: 12 additions & 8 deletions src/scales/scale.time.js
Expand Up @@ -356,34 +356,34 @@ function generate(min, max, capacity, options) {
}

/**
* Returns the right and left offsets from edges in the form of {left, right}.
* Returns the end and start offsets from edges in the form of {start, end}.
* Offsets are added when the `offset` option is true.
*/
function computeOffsets(table, ticks, min, max, options) {
var left = 0;
var right = 0;
var start = 0;
var end = 0;
var upper, lower;

if (options.offset && ticks.length) {
if (!options.time.min) {
upper = ticks.length > 1 ? ticks[1] : max;
lower = ticks[0];
left = (
start = (
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 = (
end = (
interpolate(table, 'time', upper, 'pos') -
interpolate(table, 'time', lower, 'pos')
) / 2;
}
}

return options.ticks.reverse ? {left: right, right: left} : {left: left, right: right};
return options.ticks.reverse ? {start: -end, end: -start} : {start: start, end: end};
}

function ticksFromTimestamps(values, majorUnit) {
Expand Down Expand Up @@ -638,6 +638,10 @@ module.exports = function() {
me.min = min;
me.max = max;

if (options.ticks.reverse) {
ticks.reverse();
}

// PRIVATE
me._unit = timeOpts.unit || determineUnitForFormatting(ticks, timeOpts.minUnit, me.min, me.max);
me._majorUnit = determineMajorUnit(me._unit);
Expand Down Expand Up @@ -710,7 +714,7 @@ module.exports = function() {
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.left + pos) / (me._offsets.left + 1 + me._offsets.right);
var offset = size * (me._offsets.start + pos) / (me._offsets.start + 1 + me._offsets.end);

return isReverse ? start - offset : start + offset;
},
Expand Down Expand Up @@ -743,7 +747,7 @@ module.exports = function() {
var me = this;
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.left + 1 + me._offsets.left) - me._offsets.right;
var pos = (size ? (pixel - start) / size : 0) * (me._offsets.start + 1 + me._offsets.start) - me._offsets.end;
var time = interpolate(me._table, 'pos', pos, 'time');

return moment(time);
Expand Down

0 comments on commit ef52acb

Please sign in to comment.