|
| 1 | +/*! |
| 2 | + * chartjs-adapter-date-fns v0.1.2 |
| 3 | + * https://www.chartjs.org |
| 4 | + * (c) 2019 Chart.js Contributors |
| 5 | + * Released under the MIT license |
| 6 | + */ |
| 7 | +(function (global, factory) { |
| 8 | +typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('chart.js'), require('date-fns')) : |
| 9 | +typeof define === 'function' && define.amd ? define(['chart.js', 'date-fns'], factory) : |
| 10 | +(global = global || self, factory(global.Chart, global.dateFns)); |
| 11 | +}(this, function (chart_js, dateFns) { 'use strict'; |
| 12 | + |
| 13 | +var FORMATS = { |
| 14 | + datetime: 'MMM D, YYYY, h:mm:ss a', |
| 15 | + millisecond: 'h:mm:ss.SSS a', |
| 16 | + second: 'h:mm:ss a', |
| 17 | + minute: 'h:mm a', |
| 18 | + hour: 'ha', |
| 19 | + day: 'MMM D', |
| 20 | + week: 'DD', |
| 21 | + month: 'MMM YYYY', |
| 22 | + quarter: '[Q]Q - YYYY', |
| 23 | + year: 'YYYY' |
| 24 | +}; |
| 25 | + |
| 26 | +chart_js._adapters._date.override({ |
| 27 | + _id: 'date-fns', // DEBUG |
| 28 | + |
| 29 | + formats: function() { |
| 30 | + return FORMATS; |
| 31 | + }, |
| 32 | + |
| 33 | + parse: function(value) { |
| 34 | + if (chart_js.helpers.isNullOrUndef(value)) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + value = dateFns.parse(value, this.options); |
| 38 | + return dateFns.isValid(value) ? value.getTime() : null; |
| 39 | + }, |
| 40 | + |
| 41 | + format: function(time, fmt) { |
| 42 | + return dateFns.format(time, fmt, this.options); |
| 43 | + }, |
| 44 | + |
| 45 | + add: function(time, amount, unit) { |
| 46 | + switch (unit) { |
| 47 | + case 'millisecond': return dateFns.addMilliseconds(time, amount); |
| 48 | + case 'second': return dateFns.addSeconds(time, amount); |
| 49 | + case 'minute': return dateFns.addMinutes(time, amount); |
| 50 | + case 'hour': return dateFns.addHours(time, amount); |
| 51 | + case 'day': return dateFns.addDays(time, amount); |
| 52 | + case 'week': return dateFns.addWeeks(time, amount); |
| 53 | + case 'month': return dateFns.addMonths(time, amount); |
| 54 | + case 'quarter': return dateFns.addQuarters(time, amount); |
| 55 | + case 'year': return dateFns.addYears(time, amount); |
| 56 | + default: return time; |
| 57 | + } |
| 58 | + }, |
| 59 | + |
| 60 | + diff: function(max, min, unit) { |
| 61 | + switch (unit) { |
| 62 | + case 'millisecond': return dateFns.differenceInMilliseconds(max, min); |
| 63 | + case 'second': return dateFns.differenceInSeconds(max, min); |
| 64 | + case 'minute': return dateFns.differenceInMinutes(max, min); |
| 65 | + case 'hour': return dateFns.differenceInHours(max, min); |
| 66 | + case 'day': return dateFns.differenceInDays(max, min); |
| 67 | + case 'week': return dateFns.differenceInWeeks(max, min); |
| 68 | + case 'month': return dateFns.differenceInMonths(max, min); |
| 69 | + case 'quarter': return dateFns.differenceInQuarters(max, min); |
| 70 | + case 'year': return dateFns.differenceInYears(max, min); |
| 71 | + default: return 0; |
| 72 | + } |
| 73 | + }, |
| 74 | + |
| 75 | + startOf: function(time, unit, weekday) { |
| 76 | + switch (unit) { |
| 77 | + case 'second': return dateFns.startOfSecond(time); |
| 78 | + case 'minute': return dateFns.startOfMinute(time); |
| 79 | + case 'hour': return dateFns.startOfHour(time); |
| 80 | + case 'day': return dateFns.startOfDay(time); |
| 81 | + case 'week': return dateFns.startOfWeek(time); |
| 82 | + case 'isoWeek': return dateFns.startOfWeek(time, { weekStartsOn: weekday }); |
| 83 | + case 'month': return dateFns.startOfMonth(time); |
| 84 | + case 'quarter': return dateFns.startOfQuarter(time); |
| 85 | + case 'year': return dateFns.startOfYear(time); |
| 86 | + default: return time; |
| 87 | + } |
| 88 | + }, |
| 89 | + |
| 90 | + endOf: function(time, unit) { |
| 91 | + switch (unit) { |
| 92 | + case 'second': return dateFns.endOfSecond(time); |
| 93 | + case 'minute': return dateFns.endOfMinute(time); |
| 94 | + case 'hour': return dateFns.endOfHour(time); |
| 95 | + case 'day': return dateFns.endOfDay(time); |
| 96 | + case 'week': return dateFns.endOfWeek(time); |
| 97 | + case 'month': return dateFns.endOfMonth(time); |
| 98 | + case 'quarter': return dateFns.endOfQuarter(time); |
| 99 | + case 'year': return dateFns.endOfYear(time); |
| 100 | + default: return time; |
| 101 | + } |
| 102 | + } |
| 103 | +}); |
| 104 | + |
| 105 | +})); |
0 commit comments