Skip to content

Commit

Permalink
Format the label in the time scale tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jan 9, 2018
1 parent 92d033b commit 52f9331
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/scales/scale.time.js
Expand Up @@ -403,6 +403,27 @@ function ticksFromTimestamps(values, majorUnit) {
return ticks;
}

function determineLabelFormat(data, timeOpts) {
var i, momentDate, hasTime;
var ilen = data.length;

// find the label with the most parts (milliseconds, minutes, etc.)
// format all labels with the same level of detail as the most specific label
for (i = 0; i < ilen; i++) {
momentDate = momentify(data[i], timeOpts);
if (momentDate.millisecond() !== 0) {
return 'MMM D, YYYY h:mm:ss.SSS a';
}
if (momentDate.second() !== 0 || momentDate.minute() !== 0 || momentDate.hour() !== 0) {
hasTime = true;
}
}
if (hasTime) {
return 'MMM D, YYYY h:mm:ss a';
}
return 'MMM D, YYYY';
}

module.exports = function(Chart) {

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

return ticksFromTimestamps(ticks, me._majorUnit);
},
Expand All @@ -636,10 +658,13 @@ module.exports = function(Chart) {
label = me.getRightValue(value);
}
if (timeOpts.tooltipFormat) {
label = momentify(label, timeOpts).format(timeOpts.tooltipFormat);
return momentify(label, timeOpts).format(timeOpts.tooltipFormat);
}
if (typeof label === 'string') {
return label;
}

return label;
return momentify(label, timeOpts).format(me._labelFormat);
},

/**
Expand Down
113 changes: 113 additions & 0 deletions test/specs/scale.time.tests.js
Expand Up @@ -584,6 +584,119 @@ describe('Time scale tests', function() {
expect(xScale.getLabelForIndex(6, 0)).toBe('2015-01-10T12:00');
});

it('should get the correct label when time is specified as a string', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
xAxisID: 'xScale0',
data: [{t: '2015-01-01T20:00:00', y: 10}, {t: '2015-01-02T21:00:00', y: 3}]
}],
},
options: {
scales: {
xAxes: [{
id: 'xScale0',
type: 'time',
position: 'bottom'
}],
}
}
});

var xScale = chart.scales.xScale0;
expect(xScale.getLabelForIndex(0, 0)).toBeTruthy();
expect(xScale.getLabelForIndex(0, 0)).toBe('2015-01-01T20:00:00');
});

it('should get the correct label for a timestamp with milliseconds', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
xAxisID: 'xScale0',
data: [{t: 1515469457180, y: 10}, {t: 1515469458180, y: 3}]
}],
},
options: {
scales: {
xAxes: [{
id: 'xScale0',
type: 'time',
position: 'bottom'
}],
}
}
});

var xScale = chart.scales.xScale0;
var label0 = xScale.getLabelForIndex(0, 0);
// check things that won't differ by timezone until we have timezone support
expect(label0).toBeTruthy();
expect(label0).toContain('Jan');
expect(label0).toContain('2018');
expect(label0).toContain('44:17.180');
expect(label0).toContain('m');
});

it('should get the correct label for a timestamp with time', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
xAxisID: 'xScale0',
data: [{t: 1515469457000, y: 10}, {t: 1515469458000, y: 3}]
}],
},
options: {
scales: {
xAxes: [{
id: 'xScale0',
type: 'time',
position: 'bottom'
}],
}
}
});

var xScale = chart.scales.xScale0;
var label0 = xScale.getLabelForIndex(0, 0);
// check things that won't differ by timezone until we have timezone support
expect(label0).toBeTruthy();
expect(label0).toContain('Jan');
expect(label0).toContain('2018');
expect(label0).not.toContain('.000');
});

it('should get the correct label for a timestamp representing a date', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
xAxisID: 'xScale0',
data: [{t: 1515484800000, y: 10}, {t: 1515571200000, y: 3}]
}],
},
options: {
scales: {
xAxes: [{
id: 'xScale0',
type: 'time',
position: 'bottom'
}],
}
}
});

var xScale = chart.scales.xScale0;
var label0 = xScale.getLabelForIndex(0, 0);
// check things that won't differ by timezone until we have timezone support
expect(label0).toBeTruthy();
expect(label0).toContain('Jan');
expect(label0).toContain('2018');
expect(label0).not.toContain('.000');
});

it('should get the correct pixel for only one data in the dataset', function() {
var chart = window.acquireChart({
type: 'line',
Expand Down

0 comments on commit 52f9331

Please sign in to comment.