Skip to content

Commit

Permalink
Allow tooltipFormat to be a function. Provide a default tooltipFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jul 29, 2017
1 parent 53b7a63 commit 3e1d8f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
11 changes: 8 additions & 3 deletions docs/axes/cartesian/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The following options are provided by the time scale. They are all located in th
| `min` | [Time](#date-formats) | | If defined, this will override the data minimum
| `parser` | `String` or `Function` | | Custom parser for dates. [more...](#parser)
| `round` | `String` | `false` | If defined, dates will be rounded to the start of this unit. See [Time Units](#time-units) below for the allowed units.
| `tooltipFormat` | `String` | | The moment js format string to use for the tooltip.
| `tooltipFormat` | `String` or `Function` | | The Moment.js format string to use for the tooltip. [more...](#tooltip-format)
| `unit` | `String` | `false` | If defined, will force the unit to be a certain type. See [Time Units](#time-units) section below for details.
| `stepSize` | `Number` | `1` | The number of units between grid lines.
| `minUnit` | `String` | `'millisecond'` | The minimum display format to be used for a time unit.
Expand Down Expand Up @@ -107,6 +107,11 @@ var chart = new Chart(ctx, {
```

### Parser
If this property is defined as a string, it is interpreted as a custom format to be used by moment to parse the date.
If this property is defined as a string, it is interpreted as a custom format to be used by Moment.js to parse the date.

If this is a function, it must return a moment.js object given the appropriate data value.
If this is a function, it must return a Moment.js object given the appropriate data value.

### Tooltip Format
If this property is defined as a string, it is interpreted as a custom format to be used by Moment.js to format the date.

If this is a function, it must return a custom format to be used by Moment.js to format the date given a Moment.js object, the index of the dataset, and index within the dataset.
34 changes: 28 additions & 6 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ function parse(input, scale) {
/**
* Returns the number of unit to skip to be able to display up to `capacity` number of ticks
* in `unit` for the given `min` / `max` range and respecting the interval steps constraints.
* @param min {number} minimum tick millis
* @param max {number} maximum tick millis
* @param unit {string} the time unit the ticks are being displayed as
* @param capacity {number} the number of labels we have room to display
* @return {string} the number of ticks to skip before displaying the next one
*/
function determineStepSize(min, max, unit, capacity) {
var range = max - min;
Expand Down Expand Up @@ -329,6 +334,19 @@ function ticksFromTimestamps(values, majorUnit) {
return ticks;
}

/**
* Show the most specific time format by default
*/
function defaultTooltipFormat(momentDate) {
if (momentDate.millisecond() !== 0) {
return 'MMM D, YYYY h:mm:ss.SSS a';
}
if (momentDate.second() !== 0 || momentDate.minute() !== 0 || momentDate.hour() !== 0) {
return 'MMM D, YYYY h:mm:ss a';
}
return 'MMM D, YYYY';
}

module.exports = function(Chart) {

var defaultConfig = {
Expand All @@ -342,6 +360,7 @@ module.exports = function(Chart) {
displayFormat: false, // DEPRECATED
isoWeekday: false, // override week start day - see http://momentjs.com/docs/#/get-set/iso-weekday/
minUnit: 'millisecond',
tooltipFormat: defaultTooltipFormat,

// defaults to unit's corresponding unitFormat below or override using pattern string from http://momentjs.com/docs/#/displaying/format/
displayFormats: {
Expand Down Expand Up @@ -549,17 +568,20 @@ module.exports = function(Chart) {
var me = this;
var data = me.chart.data;
var timeOpts = me.options.time;
var tooltipFormat = timeOpts.tooltipFormat;
var label = data.labels && index < data.labels.length ? data.labels[index] : '';
var value = data.datasets[datasetIndex].data[index];
var momentDate;

if (helpers.isObject(value)) {
label = me.getRightValue(value);
}
if (timeOpts.tooltipFormat) {
label = momentify(label, timeOpts).format(timeOpts.tooltipFormat);
if (!helpers.isObject(value)) {
return label;
}

return label;
momentDate = momentify(me.getRightValue(value), timeOpts);
tooltipFormat = typeof tooltipFormat === 'function'
? tooltipFormat.call(me, momentDate, datasetIndex, index)
: tooltipFormat;
return momentDate.format(tooltipFormat);
},

/**
Expand Down
1 change: 1 addition & 0 deletions test/specs/scale.time.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ describe('Time scale tests', function() {
isoWeekday: false,
displayFormat: false,
minUnit: 'millisecond',
tooltipFormat: defaultConfig.time.tooltipFormat,
displayFormats: {
millisecond: 'h:mm:ss.SSS a', // 11:20:01.123 AM
second: 'h:mm:ss a', // 11:20:01 AM
Expand Down

0 comments on commit 3e1d8f4

Please sign in to comment.