diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..4f7490e --- /dev/null +++ b/bower.json @@ -0,0 +1,8 @@ +{ + "name": "chartjs-adapter-moment", + "description": "Chart.js adapter to use Moment.js for time functionalities", + "homepage": "https://www.chartjs.org", + "license": "MIT", + "version": "0.1.1", + "main": "dist/chartjs-adapter-moment.js" +} \ No newline at end of file diff --git a/dist/chartjs-adapter-moment.js b/dist/chartjs-adapter-moment.js new file mode 100644 index 0000000..702aced --- /dev/null +++ b/dist/chartjs-adapter-moment.js @@ -0,0 +1,69 @@ +/*! + * chartjs-adapter-moment v0.1.1 + * https://www.chartjs.org + * (c) 2020 Chart.js Contributors + * Released under the MIT license + */ +(function (global, factory) { +typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('moment'), require('chart.js')) : +typeof define === 'function' && define.amd ? define(['moment', 'chart.js'], factory) : +(global = global || self, factory(global.moment, global.Chart)); +}(this, (function (moment, chart_js) { 'use strict'; + +moment = moment && moment.hasOwnProperty('default') ? moment['default'] : moment; + +const FORMATS = { + datetime: 'MMM D, YYYY, h:mm:ss a', + 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' +}; + +chart_js._adapters._date.override(typeof moment === 'function' ? { + _id: 'moment', // DEBUG ONLY + + formats: function() { + return FORMATS; + }, + + 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.valueOf() : null; + }, + + format: function(time, format) { + return moment(time).format(format); + }, + + add: function(time, amount, unit) { + return moment(time).add(amount, unit).valueOf(); + }, + + diff: function(max, min, unit) { + return moment(max).diff(moment(min), unit); + }, + + startOf: function(time, unit, weekday) { + time = moment(time); + if (unit === 'isoWeek') { + return time.isoWeekday(weekday).valueOf(); + } + return time.startOf(unit).valueOf(); + }, + + endOf: function(time, unit) { + return moment(time).endOf(unit).valueOf(); + } +} : {}); + +}))); diff --git a/dist/chartjs-adapter-moment.min.js b/dist/chartjs-adapter-moment.min.js new file mode 100644 index 0000000..d83ab14 --- /dev/null +++ b/dist/chartjs-adapter-moment.min.js @@ -0,0 +1,7 @@ +/*! + * chartjs-adapter-moment v0.1.1 + * https://www.chartjs.org + * (c) 2020 Chart.js Contributors + * Released under the MIT license + */ +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("moment"),require("chart.js")):"function"==typeof define&&define.amd?define(["moment","chart.js"],t):t((e=e||self).moment,e.Chart)}(this,(function(e,t){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;const n={datetime:"MMM D, YYYY, h:mm:ss a",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"};t._adapters._date.override("function"==typeof e?{_id:"moment",formats:function(){return n},parse:function(t,n){return"string"==typeof t&&"string"==typeof n?t=e(t,n):t instanceof e||(t=e(t)),t.isValid()?t.valueOf():null},format:function(t,n){return e(t).format(n)},add:function(t,n,f){return e(t).add(n,f).valueOf()},diff:function(t,n,f){return e(t).diff(e(n),f)},startOf:function(t,n,f){return t=e(t),"isoWeek"===n?t.isoWeekday(f).valueOf():t.startOf(n).valueOf()},endOf:function(t,n){return e(t).endOf(n).valueOf()}}:{})}));