Skip to content

Commit e21e78f

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

File tree

2 files changed

+136
-2
lines changed

2 files changed

+136
-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
/**

test/specs/scale.time.tests.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,115 @@ describe('Time scale tests', function() {
584584
expect(xScale.getLabelForIndex(6, 0)).toBe('2015-01-10T12:00');
585585
});
586586

587+
it('should get the correct label when time is specified as a string', function() {
588+
var chart = window.acquireChart({
589+
type: 'line',
590+
data: {
591+
datasets: [{
592+
xAxisID: 'xScale0',
593+
data: [{t: '2015-01-01T20:00:00', y: 10}, {t: '2015-01-02T21:00:00', y: 3}]
594+
}],
595+
},
596+
options: {
597+
scales: {
598+
xAxes: [{
599+
id: 'xScale0',
600+
type: 'time',
601+
position: 'bottom'
602+
}],
603+
}
604+
}
605+
});
606+
607+
var xScale = chart.scales.xScale0;
608+
expect(xScale.getLabelForIndex(0, 0)).toBeTruthy();
609+
expect(xScale.getLabelForIndex(0, 0)).toBe('2015-01-01T20:00:00');
610+
});
611+
612+
it('should get the correct label for a timestamp with milliseconds', function() {
613+
var chart = window.acquireChart({
614+
type: 'line',
615+
data: {
616+
datasets: [{
617+
xAxisID: 'xScale0',
618+
data: [{t: 1515469457180, y: 10}, {t: 1515469458180, y: 3}]
619+
}],
620+
},
621+
options: {
622+
scales: {
623+
xAxes: [{
624+
id: 'xScale0',
625+
type: 'time',
626+
position: 'bottom'
627+
}],
628+
}
629+
}
630+
});
631+
632+
var xScale = chart.scales.xScale0;
633+
var label = xScale.getLabelForIndex(0, 0);
634+
// check things that won't differ by timezone until we have timezone support
635+
expect(label).toMatch(/^Jan \d+, 2018 \d{1,2}:\d{2}:\d{2}.\d{3} (am|pm)$/);
636+
});
637+
638+
it('should get the correct label for a timestamp with time', function() {
639+
var chart = window.acquireChart({
640+
type: 'line',
641+
data: {
642+
datasets: [{
643+
xAxisID: 'xScale0',
644+
data: [{t: 1515469457000, y: 10}, {t: 1515469458000, y: 3}]
645+
}],
646+
},
647+
options: {
648+
scales: {
649+
xAxes: [{
650+
id: 'xScale0',
651+
type: 'time',
652+
position: 'bottom'
653+
}],
654+
}
655+
}
656+
});
657+
658+
var xScale = chart.scales.xScale0;
659+
var label0 = xScale.getLabelForIndex(0, 0);
660+
// check things that won't differ by timezone until we have timezone support
661+
expect(label0).toBeTruthy();
662+
expect(label0).toContain('Jan');
663+
expect(label0).toContain('2018');
664+
expect(label0).not.toContain('.000');
665+
});
666+
667+
it('should get the correct label for a timestamp representing a date', function() {
668+
var chart = window.acquireChart({
669+
type: 'line',
670+
data: {
671+
datasets: [{
672+
xAxisID: 'xScale0',
673+
data: [{t: 1515484800000, y: 10}, {t: 1515571200000, y: 3}]
674+
}],
675+
},
676+
options: {
677+
scales: {
678+
xAxes: [{
679+
id: 'xScale0',
680+
type: 'time',
681+
position: 'bottom'
682+
}],
683+
}
684+
}
685+
});
686+
687+
var xScale = chart.scales.xScale0;
688+
var label0 = xScale.getLabelForIndex(0, 0);
689+
// check things that won't differ by timezone until we have timezone support
690+
expect(label0).toBeTruthy();
691+
expect(label0).toContain('Jan');
692+
expect(label0).toContain('2018');
693+
expect(label0).not.toContain('.000');
694+
});
695+
587696
it('should get the correct pixel for only one data in the dataset', function() {
588697
var chart = window.acquireChart({
589698
type: 'line',

0 commit comments

Comments
 (0)