Skip to content

Commit

Permalink
Fix choosing of formatting unit
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Sep 20, 2017
1 parent bcb4849 commit 23b7b87
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ function determineStepSize(min, max, unit, capacity) {
return factor;
}

function determineUnit(minUnit, min, max, capacity) {
/**
* Figures out what unit results in an appropriate number of auto-generated ticks
*/
function determineUnitForAutoTicks(minUnit, min, max, capacity) {
var ilen = UNITS.length;
var i, interval, factor;

Expand All @@ -269,6 +272,24 @@ function determineUnit(minUnit, min, max, capacity) {
return UNITS[ilen - 1];
}

/**
* Figures out what unit to format a set of ticks with
*/
function determineUnitForFormatting(ticks, minUnit, min, max) {
var duration = moment.duration(moment(max).diff(moment(min)));
var ilen = UNITS.length;
var i, unit;

for (i = ilen - 1; i >= UNITS.indexOf(minUnit); i--) {
unit = UNITS[i];
if (INTERVALS[unit].major && duration.as(unit) >= ticks.length) {
return unit;
}
}

return UNITS[minUnit ? UNITS.indexOf(minUnit) : 0];
}

function determineMajorUnit(unit) {
for (var i = UNITS.indexOf(unit) + 1, ilen = UNITS.length; i < ilen; ++i) {
if (INTERVALS[UNITS[i]].major) {
Expand All @@ -283,8 +304,10 @@ function determineMajorUnit(unit) {
* Important: this method can return ticks outside the min and max range, it's the
* responsibility of the calling code to clamp values if needed.
*/
function generate(min, max, minor, major, capacity, options) {
function generate(min, max, capacity, options) {
var timeOpts = options.time;
var minor = timeOpts.unit || determineUnitForAutoTicks(timeOpts.minUnit, min, max, capacity);
var major = determineMajorUnit(minor);
var stepSize = helpers.valueOrDefault(timeOpts.stepSize, timeOpts.unitStepSize);
var weekday = minor === 'week' ? timeOpts.isoWeekday : false;
var majorTicksEnabled = options.ticks.major.enabled;
Expand Down Expand Up @@ -553,9 +576,6 @@ module.exports = function(Chart) {
var max = me.max;
var options = me.options;
var timeOpts = options.time;
var capacity = me.getLabelCapacity(min);
var unit = timeOpts.unit || determineUnit(timeOpts.minUnit, min, max, capacity);
var majorUnit = determineMajorUnit(unit);
var timestamps = [];
var ticks = [];
var i, ilen, timestamp;
Expand All @@ -569,7 +589,7 @@ module.exports = function(Chart) {
break;
case 'auto':
default:
timestamps = generate(min, max, unit, majorUnit, capacity, options);
timestamps = generate(min, max, me.getLabelCapacity(min), options);
}

if (options.bounds === 'ticks' && timestamps.length) {
Expand All @@ -593,12 +613,12 @@ module.exports = function(Chart) {
me.max = max;

// PRIVATE
me._unit = unit;
me._majorUnit = majorUnit;
me._unit = determineUnitForFormatting(ticks, timeOpts.minUnit, me.min, me.max);
me._majorUnit = determineMajorUnit(me._unit);
me._table = buildLookupTable(me._timestamps.data, min, max, options.distribution);
me._offsets = computeOffsets(me._table, ticks, min, max, options);

return ticksFromTimestamps(ticks, majorUnit);
return ticksFromTimestamps(ticks, me._majorUnit);
},

getLabelForIndex: function(index, datasetIndex) {
Expand Down Expand Up @@ -630,7 +650,7 @@ module.exports = function(Chart) {
var minorFormat = formats[me._unit];
var majorUnit = me._majorUnit;
var majorFormat = formats[majorUnit];
var majorTime = tick.clone().startOf(me._majorUnit).valueOf();
var majorTime = tick.clone().startOf(majorUnit).valueOf();
var majorTickOpts = options.ticks.major;
var major = majorTickOpts.enabled && majorUnit && majorFormat && time === majorTime;
var label = tick.format(formatOverride ? formatOverride : major ? majorFormat : minorFormat);
Expand Down

0 comments on commit 23b7b87

Please sign in to comment.