Skip to content

Commit f070925

Browse files
committed
Format the label in the time scale tooltip
1 parent 92d033b commit f070925

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/scales/scale.time.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,27 @@ function ticksFromTimestamps(values, majorUnit) {
403403
return ticks;
404404
}
405405

406+
function determineLabelFormat(data, timeOpts) {
407+
var i, momentDate, hasTime;
408+
var ilen = data.length;
409+
410+
// find the label with the most parts (milliseconds, minutes, etc.)
411+
// format all labels with the same level of detail as the most specific label
412+
for (i = 0; i < ilen; i++) {
413+
momentDate = momentify(data[i], timeOpts);
414+
if (momentDate.millisecond() !== 0) {
415+
return 'MMM D, YYYY h:mm:ss.SSS a';
416+
}
417+
if (momentDate.second() !== 0 || momentDate.minute() !== 0 || momentDate.hour() !== 0) {
418+
hasTime = true;
419+
}
420+
}
421+
if (hasTime) {
422+
return 'MMM D, YYYY h:mm:ss a';
423+
}
424+
return 'MMM D, YYYY';
425+
}
426+
406427
module.exports = function(Chart) {
407428

408429
var defaultConfig = {
@@ -621,6 +642,7 @@ module.exports = function(Chart) {
621642
me._majorUnit = determineMajorUnit(me._unit);
622643
me._table = buildLookupTable(me._timestamps.data, min, max, options.distribution);
623644
me._offsets = computeOffsets(me._table, ticks, min, max, options);
645+
me._labelFormat = determineLabelFormat(me._timestamps.data, timeOpts);
624646

625647
return ticksFromTimestamps(ticks, me._majorUnit);
626648
},
@@ -636,10 +658,13 @@ module.exports = function(Chart) {
636658
label = me.getRightValue(value);
637659
}
638660
if (timeOpts.tooltipFormat) {
639-
label = momentify(label, timeOpts).format(timeOpts.tooltipFormat);
661+
return momentify(label, timeOpts).format(timeOpts.tooltipFormat);
662+
}
663+
if (typeof label === 'string') {
664+
return label;
640665
}
641666

642-
return label;
667+
return momentify(label, timeOpts).format(me._labelFormat);
643668
},
644669

645670
/**

0 commit comments

Comments
 (0)