From 23b7b8715f38cb0284ebd2920e9b1b2528c17191 Mon Sep 17 00:00:00 2001 From: Ben McCann Date: Wed, 20 Sep 2017 10:35:58 -0700 Subject: [PATCH] Fix choosing of formatting unit --- src/scales/scale.time.js | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 82bfbd29d15..e2f06600c64 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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; @@ -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) { @@ -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; @@ -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; @@ -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) { @@ -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) { @@ -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);