-
Notifications
You must be signed in to change notification settings - Fork 12k
Implement adapter to abstract date/time features #5960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7322d75
Implement adapter to abstract date/time features
simonbrunel f161049
Fix format usage in `toTimestamp`
simonbrunel 524f765
Fix `abstract` exception message
simonbrunel 81d71fb
Replace SHORT/LONG_PRESETS by formats() and presets()
simonbrunel 52a7734
Fix detection of tooltip fallback format
simonbrunel 3183b55
Make `format()` and `presets()` abstract
simonbrunel b76d6b4
Better argument naming and comments
simonbrunel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// TODO v3 - make this adapter external (chartjs-adapter-moment) | ||
|
||
'use strict'; | ||
|
||
var moment = require('moment'); | ||
var adapter = require('../core/core.adapters')._date; | ||
var helpers = require('../helpers/helpers.core'); | ||
|
||
var FORMATS = { | ||
millisecond: 'h:mm:ss.SSS a', | ||
second: 'h:mm:ss a', | ||
minute: 'h:mm a', | ||
hour: 'hA', | ||
day: 'MMM D', | ||
week: 'll', | ||
month: 'MMM YYYY', | ||
quarter: '[Q]Q - YYYY', | ||
year: 'YYYY' | ||
}; | ||
|
||
var PRESETS = { | ||
full: 'MMM D, YYYY h:mm:ss.SSS a', | ||
time: 'MMM D, YYYY h:mm:ss a', | ||
date: 'MMM D, YYYY' | ||
}; | ||
|
||
helpers.merge(adapter, moment ? { | ||
_id: 'moment', // DEBUG ONLY | ||
|
||
formats: function() { | ||
return FORMATS; | ||
}, | ||
|
||
presets: function() { | ||
return PRESETS; | ||
}, | ||
|
||
parse: function(value, format) { | ||
if (typeof value === 'string' && typeof format === 'string') { | ||
value = moment(value, format); | ||
} else if (!(value instanceof moment)) { | ||
value = moment(value); | ||
} | ||
return value.isValid() ? +value : null; | ||
}, | ||
|
||
format: function(time, format) { | ||
return moment(time).format(format); | ||
}, | ||
|
||
add: function(time, amount, unit) { | ||
return +moment(time).add(amount, unit); | ||
}, | ||
|
||
diff: function(max, min, unit) { | ||
return moment.duration(moment(max).diff(moment(min))).as(unit); | ||
}, | ||
|
||
startOf: function(time, unit, weekday) { | ||
time = moment(time); | ||
if (unit === 'isoWeek') { | ||
return +time.isoWeekday(weekday); | ||
} | ||
return +time.startOf(unit); | ||
}, | ||
|
||
endOf: function(time, unit) { | ||
return +moment(time).endOf(unit); | ||
}, | ||
|
||
// DEPRECATIONS | ||
|
||
/** | ||
* Provided for backward compatibility with scale.getValueForPixel(). | ||
* @deprecated since version 2.8.0 | ||
* @todo remove at version 3 | ||
* @private | ||
*/ | ||
_create: function(time) { | ||
return moment(time); | ||
}, | ||
} : {}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// IMPORTANT: do NOT submit new adapters to this repository, instead | ||
// create an external library named `chartjs-adapter-{lib-name}` | ||
// ----------------------------------------------------------------------------- | ||
|
||
// Built-in moment adapter that we need to keep for backward compatibility | ||
// https://github.com/chartjs/Chart.js/issues/5542 | ||
require('./adapter.moment'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* @namespace Chart._adapters | ||
* @since 2.8.0 | ||
* @private | ||
*/ | ||
|
||
'use strict'; | ||
|
||
function abstract() { | ||
throw new Error( | ||
'This method is not implemented: either no adapter can ' + | ||
'be found or an incomplete integration was provided.' | ||
); | ||
} | ||
|
||
/** | ||
* Date adapter (current used by the time scale) | ||
* @namespace Chart._adapters._date | ||
* @memberof Chart._adapters | ||
* @private | ||
*/ | ||
|
||
/** | ||
* Currently supported unit string values. | ||
* @typedef {('millisecond'|'second'|'minute'|'hour'|'day'|'week'|'month'|'quarter'|'year')} | ||
* @memberof Chart._adapters._date | ||
* @name Unit | ||
*/ | ||
|
||
/** @lends Chart._adapters._date */ | ||
module.exports._date = { | ||
/** | ||
* Returns a map of time formats for the supported units. | ||
* @returns {{string: string}} | ||
*/ | ||
formats: abstract, | ||
|
||
/** | ||
* Returns a map of date/time formats for the following presets: | ||
* 'full': date + time + millisecond | ||
* 'time': date + time | ||
* 'date': date | ||
* @returns {{string: string}} | ||
*/ | ||
presets: abstract, | ||
|
||
/** | ||
* Parses the given `value` and return the associated timestamp. | ||
* @param {any} value - the value to parse (usually comes from the data) | ||
* @param {string} [format] - the expected data format | ||
* @returns {(number|null)} | ||
* @function | ||
*/ | ||
parse: abstract, | ||
|
||
/** | ||
* Returns the formatted date in the specified `format` for a given `timestamp`. | ||
* @param {number} timestamp - the timestamp to format | ||
* @param {string} format - the date/time token | ||
* @return {string} | ||
* @function | ||
*/ | ||
format: abstract, | ||
|
||
/** | ||
* Adds the specified `amount` of `unit` to the given `timestamp`. | ||
* @param {number} timestamp - the input timestamp | ||
* @param {number} amount - the amount to add | ||
* @param {Unit} unit - the unit as string | ||
* @return {number} | ||
* @function | ||
*/ | ||
add: abstract, | ||
|
||
/** | ||
* Returns the number of `unit` between the given timestamps. | ||
* @param {number} max - the input timestamp (reference) | ||
* @param {number} min - the timestamp to substract | ||
* @param {Unit} unit - the unit as string | ||
* @return {number} | ||
* @function | ||
*/ | ||
diff: abstract, | ||
|
||
/** | ||
* Returns start of `unit` for the given `timestamp`. | ||
* @param {number} timestamp - the input timestamp | ||
* @param {Unit} unit - the unit as string | ||
* @param {number} [weekday] - the ISO day of the week with 1 being Monday | ||
* and 7 being Sunday (only needed if param *unit* is `isoWeek`). | ||
* @function | ||
*/ | ||
startOf: abstract, | ||
|
||
/** | ||
* Returns end of `unit` for the given `timestamp`. | ||
* @param {number} timestamp - the input timestamp | ||
* @param {Unit} unit - the unit as string | ||
* @function | ||
simonbrunel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
endOf: abstract, | ||
|
||
// DEPRECATIONS | ||
|
||
/** | ||
* Provided for backward compatibility for scale.getValueForPixel(), | ||
* this method should be overridden only by the moment adapter. | ||
* @deprecated since version 2.8.0 | ||
* @todo remove at version 3 | ||
* @private | ||
*/ | ||
_create: function(value) { | ||
return value; | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.