Skip to content

Commit 7cdd3a7

Browse files
benmccannsimonbrunel
authored andcommitted
Use datetime as default time scale tooltip format (chartjs#6019)
Remove the logic that computed an "optimal" tooltip format. Instead, always fallback to the `datetime` adapter format which is more efficient and stable. Additionally, remove the adapter `presets` API, which is not needed anymore.
1 parent 431e1da commit 7cdd3a7

File tree

4 files changed

+8
-104
lines changed

4 files changed

+8
-104
lines changed

src/adapters/adapter.moment.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var adapter = require('../core/core.adapters')._date;
77
var helpers = require('../helpers/helpers.core');
88

99
var FORMATS = {
10+
datetime: 'MMM D, YYYY, h:mm:ss a',
1011
millisecond: 'h:mm:ss.SSS a',
1112
second: 'h:mm:ss a',
1213
minute: 'h:mm a',
@@ -18,23 +19,13 @@ var FORMATS = {
1819
year: 'YYYY'
1920
};
2021

21-
var PRESETS = {
22-
full: 'MMM D, YYYY h:mm:ss.SSS a',
23-
time: 'MMM D, YYYY h:mm:ss a',
24-
date: 'MMM D, YYYY'
25-
};
26-
2722
helpers.merge(adapter, moment ? {
2823
_id: 'moment', // DEBUG ONLY
2924

3025
formats: function() {
3126
return FORMATS;
3227
},
3328

34-
presets: function() {
35-
return PRESETS;
36-
},
37-
3829
parse: function(value, format) {
3930
if (typeof value === 'string' && typeof format === 'string') {
4031
value = moment(value, format);

src/core/core.adapters.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,12 @@ function abstract() {
3030
/** @lends Chart._adapters._date */
3131
module.exports._date = {
3232
/**
33-
* Returns a map of time formats for the supported units.
33+
* Returns a map of time formats for the supported formatting units defined
34+
* in Unit as well as 'datetime' representing a detailed date/time string.
3435
* @returns {{string: string}}
3536
*/
3637
formats: abstract,
3738

38-
/**
39-
* Returns a map of date/time formats for the following presets:
40-
* 'full': date + time + millisecond
41-
* 'time': date + time
42-
* 'date': date
43-
* @returns {{string: string}}
44-
*/
45-
presets: abstract,
46-
4739
/**
4840
* Parses the given `value` and return the associated timestamp.
4941
* @param {any} value - the value to parse (usually comes from the data)

src/scales/scale.time.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -405,29 +405,6 @@ function ticksFromTimestamps(values, majorUnit) {
405405
return ticks;
406406
}
407407

408-
/**
409-
* Return the time format for the label with the most parts (milliseconds, second, etc.)
410-
*/
411-
function determineLabelFormat(timestamps) {
412-
var presets = adapter.presets();
413-
var ilen = timestamps.length;
414-
var i, ts, hasTime;
415-
416-
for (i = 0; i < ilen; i++) {
417-
ts = timestamps[i];
418-
if (ts % INTERVALS.second.size !== 0) {
419-
return presets.full;
420-
}
421-
if (!hasTime && adapter.startOf(ts, 'day') !== ts) {
422-
hasTime = true;
423-
}
424-
}
425-
if (hasTime) {
426-
return presets.time;
427-
}
428-
return presets.date;
429-
}
430-
431408
var defaultConfig = {
432409
position: 'bottom',
433410

@@ -637,7 +614,6 @@ module.exports = Scale.extend({
637614
me._majorUnit = determineMajorUnit(me._unit);
638615
me._table = buildLookupTable(me._timestamps.data, min, max, options.distribution);
639616
me._offsets = computeOffsets(me._table, ticks, min, max, options);
640-
me._labelFormat = determineLabelFormat(me._timestamps.data);
641617

642618
if (options.ticks.reverse) {
643619
ticks.reverse();
@@ -662,8 +638,7 @@ module.exports = Scale.extend({
662638
if (typeof label === 'string') {
663639
return label;
664640
}
665-
666-
return adapter.format(toTimestamp(label, timeOpts), me._labelFormat);
641+
return adapter.format(toTimestamp(label, timeOpts), timeOpts.displayFormats.datetime);
667642
},
668643

669644
/**

test/specs/scale.time.tests.js

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ describe('Time scale tests', function() {
599599
expect(xScale.getLabelForIndex(0, 0)).toBe('2015-01-01T20:00:00');
600600
});
601601

602-
it('should get the correct label for a timestamp with milliseconds', function() {
602+
it('should get the correct label for a timestamp', function() {
603603
var chart = window.acquireChart({
604604
type: 'line',
605605
data: {
@@ -624,63 +624,7 @@ describe('Time scale tests', function() {
624624

625625
var xScale = chart.scales.xScale0;
626626
var label = xScale.getLabelForIndex(0, 0);
627-
expect(label).toEqual('Jan 8, 2018 5:14:23.234 am');
628-
});
629-
630-
it('should get the correct label for a timestamp with time', function() {
631-
var chart = window.acquireChart({
632-
type: 'line',
633-
data: {
634-
datasets: [{
635-
xAxisID: 'xScale0',
636-
data: [
637-
{t: +new Date('2018-01-08 05:14:23'), y: 10},
638-
{t: +new Date('2018-01-09 06:17:43'), y: 3}
639-
]
640-
}],
641-
},
642-
options: {
643-
scales: {
644-
xAxes: [{
645-
id: 'xScale0',
646-
type: 'time',
647-
position: 'bottom'
648-
}],
649-
}
650-
}
651-
});
652-
653-
var xScale = chart.scales.xScale0;
654-
var label = xScale.getLabelForIndex(0, 0);
655-
expect(label).toEqual('Jan 8, 2018 5:14:23 am');
656-
});
657-
658-
it('should get the correct label for a timestamp representing a date', function() {
659-
var chart = window.acquireChart({
660-
type: 'line',
661-
data: {
662-
datasets: [{
663-
xAxisID: 'xScale0',
664-
data: [
665-
{t: +new Date('2018-01-08 00:00:00'), y: 10},
666-
{t: +new Date('2018-01-09 00:00:00'), y: 3}
667-
]
668-
}],
669-
},
670-
options: {
671-
scales: {
672-
xAxes: [{
673-
id: 'xScale0',
674-
type: 'time',
675-
position: 'bottom'
676-
}],
677-
}
678-
}
679-
});
680-
681-
var xScale = chart.scales.xScale0;
682-
var label = xScale.getLabelForIndex(0, 0);
683-
expect(label).toEqual('Jan 8, 2018');
627+
expect(label).toEqual('Jan 8, 2018, 5:14:23 am');
684628
});
685629

686630
it('should get the correct pixel for only one data in the dataset', function() {
@@ -1532,6 +1476,7 @@ describe('Time scale tests', function() {
15321476

15331477
// NOTE: built-in adapter uses moment
15341478
var expected = {
1479+
datetime: 'MMM D, YYYY, h:mm:ss a',
15351480
millisecond: 'h:mm:ss.SSS a',
15361481
second: 'h:mm:ss a',
15371482
minute: 'h:mm a',
@@ -1570,6 +1515,7 @@ describe('Time scale tests', function() {
15701515

15711516
// NOTE: built-in adapter uses moment
15721517
var expected = {
1518+
datetime: 'MMM D, YYYY, h:mm:ss a',
15731519
millisecond: 'foo',
15741520
second: 'h:mm:ss a',
15751521
minute: 'h:mm a',

0 commit comments

Comments
 (0)