-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e9031a
commit c3fdb7c
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,8 @@ | ||
{ | ||
"name": "chartjs-adapter-date-fns", | ||
"description": "Chart.js adapter to use date-fns for time functionalities", | ||
"homepage": "https://www.chartjs.org", | ||
"license": "MIT", | ||
"version": "1.0.0-alpha3", | ||
"main": "dist/chartjs-adapter-date-fns.js" | ||
} |
This file contains 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,114 @@ | ||
/*! | ||
* chartjs-adapter-date-fns v1.0.0-alpha3 | ||
* https://www.chartjs.org | ||
* (c) 2019 Chart.js Contributors | ||
* Released under the MIT license | ||
*/ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('chart.js'), require('date-fns')) : | ||
typeof define === 'function' && define.amd ? define(['chart.js', 'date-fns'], factory) : | ||
(global = global || self, factory(global.Chart, global.dateFns)); | ||
}(this, (function (chart_js, dateFns) { 'use strict'; | ||
|
||
var FORMATS = { | ||
datetime: 'MMM d, yyyy, h:mm:ss aaaa', | ||
millisecond: 'h:mm:ss.SSS aaaa', | ||
second: 'h:mm:ss aaaa', | ||
minute: 'h:mm aaaa', | ||
hour: 'ha', | ||
day: 'MMM d', | ||
week: 'PP', | ||
month: 'MMM yyyy', | ||
quarter: 'qqq - yyyy', | ||
year: 'yyyy' | ||
}; | ||
|
||
chart_js._adapters._date.override({ | ||
_id: 'date-fns', // DEBUG | ||
|
||
formats: function() { | ||
return FORMATS; | ||
}, | ||
|
||
parse: function(value, fmt) { | ||
if (chart_js.helpers.isNullOrUndef(value)) { | ||
return null; | ||
} | ||
var type = typeof value; | ||
if (type === 'number' || value instanceof Date) { | ||
value = dateFns.toDate(value); | ||
} else if (type === 'string') { | ||
if (typeof fmt === 'string') { | ||
value = dateFns.parse(value, fmt, new Date(), this.options); | ||
} else { | ||
value = dateFns.parseISO(value, this.options); | ||
} | ||
} | ||
return dateFns.isValid(value) ? value.getTime() : null; | ||
}, | ||
|
||
format: function(time, fmt) { | ||
return dateFns.format(time, fmt, this.options); | ||
}, | ||
|
||
add: function(time, amount, unit) { | ||
switch (unit) { | ||
case 'millisecond': return dateFns.addMilliseconds(time, amount); | ||
case 'second': return dateFns.addSeconds(time, amount); | ||
case 'minute': return dateFns.addMinutes(time, amount); | ||
case 'hour': return dateFns.addHours(time, amount); | ||
case 'day': return dateFns.addDays(time, amount); | ||
case 'week': return dateFns.addWeeks(time, amount); | ||
case 'month': return dateFns.addMonths(time, amount); | ||
case 'quarter': return dateFns.addQuarters(time, amount); | ||
case 'year': return dateFns.addYears(time, amount); | ||
default: return time; | ||
} | ||
}, | ||
|
||
diff: function(max, min, unit) { | ||
switch (unit) { | ||
case 'millisecond': return dateFns.differenceInMilliseconds(max, min); | ||
case 'second': return dateFns.differenceInSeconds(max, min); | ||
case 'minute': return dateFns.differenceInMinutes(max, min); | ||
case 'hour': return dateFns.differenceInHours(max, min); | ||
case 'day': return dateFns.differenceInDays(max, min); | ||
case 'week': return dateFns.differenceInWeeks(max, min); | ||
case 'month': return dateFns.differenceInMonths(max, min); | ||
case 'quarter': return dateFns.differenceInQuarters(max, min); | ||
case 'year': return dateFns.differenceInYears(max, min); | ||
default: return 0; | ||
} | ||
}, | ||
|
||
startOf: function(time, unit, weekday) { | ||
switch (unit) { | ||
case 'second': return dateFns.startOfSecond(time); | ||
case 'minute': return dateFns.startOfMinute(time); | ||
case 'hour': return dateFns.startOfHour(time); | ||
case 'day': return dateFns.startOfDay(time); | ||
case 'week': return dateFns.startOfWeek(time); | ||
case 'isoWeek': return dateFns.startOfWeek(time, { weekStartsOn: +weekday }); | ||
case 'month': return dateFns.startOfMonth(time); | ||
case 'quarter': return dateFns.startOfQuarter(time); | ||
case 'year': return dateFns.startOfYear(time); | ||
default: return time; | ||
} | ||
}, | ||
|
||
endOf: function(time, unit) { | ||
switch (unit) { | ||
case 'second': return dateFns.endOfSecond(time); | ||
case 'minute': return dateFns.endOfMinute(time); | ||
case 'hour': return dateFns.endOfHour(time); | ||
case 'day': return dateFns.endOfDay(time); | ||
case 'week': return dateFns.endOfWeek(time); | ||
case 'month': return dateFns.endOfMonth(time); | ||
case 'quarter': return dateFns.endOfQuarter(time); | ||
case 'year': return dateFns.endOfYear(time); | ||
default: return time; | ||
} | ||
} | ||
}); | ||
|
||
}))); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.