chartjs-adapter-date-std
Version:
Chart.js adapter using browser standard Intl API for time functionalities
8 lines (7 loc) • 5.76 kB
JavaScript
/*!
* chartjs-adapter-date-std v0.1.13
* https://github.com/gcollin/chartjs-adapter-date-std/blob/master/README.md
* (c) 2023 chartjs-adapter-date-std Contributors
* Released under the MIT license
*/
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";exports.__esModule=!0,exports.StdDateAdapter=void 0;var e=require("chart.js"),t=function(){function e(e){var t;this.formatLabels=["datetime","millisecond","second","minute","hour","day","week","month","quarter","year"],this.FORMATS={},this.intlFormats={datetime:{year:"numeric",month:"short",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric"},millisecond:{hour:"numeric",minute:"numeric",second:"numeric",fractionalSecondDigits:3},second:{hour:"numeric",minute:"numeric",second:"numeric"},minute:{hour:"numeric",minute:"numeric"},hour:{hour:"numeric"},day:{day:"numeric",month:"short"},week:{day:"numeric",month:"short",year:"numeric"},month:{month:"short",year:"numeric"},year:{year:"numeric"}},this.formatters={},this.configuredLocale="",this.updateLocale(e||(null===(t=null===globalThis||void 0===globalThis?void 0:globalThis.window)||void 0===t?void 0:t.navigator.language)||"en-US");for(var r=0,n=this.formatLabels;r<n.length;r++){var a=n[r];this.FORMATS[a]=a}}return e.chartJsStandardAdapter=function(){return console.log("Registering GraphJs Standard Date Adaptor"),{_id:"StdDataAdapter",delegate:new e,override:function(e){return this.delegate.override(e)},init:function(e){return this.delegate.init(e)},formats:function(){return this.delegate.formats()},parse:function(e,t){return this.delegate.parse(e,t)},format:function(e,t){return this.delegate.format(e,t)},add:function(e,t,r){return this.delegate.add(e,t,r)},diff:function(e,t,r){return this.delegate.diff(e,t,r)},startOf:function(e,t,r){return this.delegate.startOf(e,t,r)},endOf:function(e,t){return this.delegate.endOf(e,t)}}},e.prototype.updateLocale=function(e){if(e&&e!=this.configuredLocale){for(var t=0,r=this.formatLabels;t<r.length;t++){var n=r[t];this.formatters[n]=new Intl.DateTimeFormat(e,this.intlFormats[n])}this.configuredLocale=e}},e.prototype.override=function(e){},e.prototype.init=function(e){},e.prototype.toInteger=function(e){if(null===e||!0===e||!1===e)return NaN;var t=Number(e);return isNaN(t)?t:t<0?Math.ceil(t):Math.floor(t)},e.prototype.toDate=function(e){if(null==e)return null;if(e instanceof Date)return e;if("number"==typeof e)return new Date(e);if("string"==typeof e){var t=Date.parse(e);if(isNaN(t)){var r=e.toUpperCase().trim(),n=-1,a=r.indexOf("Q1");if(-1!=a)n=1;else if(-1!=(a=r.indexOf("Q2")))n=2;else if(-1!=(a=r.indexOf("Q3")))n=3;else{if(-1==(a=r.indexOf("Q4")))return null;n=4}var i=NaN,o=r.substring(a).lastIndexOf(" ");return-1!=o&&(i=Number.parseInt(r.substring(a+o))),isNaN(i)&&(i=(new Date).getFullYear()),new Date(i,3*(n-1)+1)}return new Date(t)}return null},e.prototype.addMonths=function(e,t){var r=new Date(e.getTime());return r.setMonth(e.getMonth()+t),r.getMonth()!=(e.getMonth()+t)%12&&r.setDate(0),r.getTime()},e.prototype.diffInMonths=function(e,t){var r=12*(e.getFullYear()-t.getFullYear());return r+=e.getMonth()-t.getMonth(),this.addMonths(t,r)>e.getTime()&&(r-=1),r},e.prototype.formats=function(){return this.FORMATS},e.prototype.parse=function(e,t){var r,n=null===(r=this.toDate(e))||void 0===r?void 0:r.getTime();return null==n?null:n},e.prototype.format=function(e,t){if("quarter"===t){var r=new Date(e);return"Q"+Math.floor(r.getMonth()/3+1).toString()+" - "+r.getFullYear().toString()}return this.formatLabels.includes(t)?this.formatters[t].format(e):"Unknown"},e.prototype.add=function(e,t,r){if(t=this.toInteger(t),isNaN(t))return t;var n=this.toDate(e);if(!t||null==n)return NaN;switch(r){case"week":t*=7;case"day":t*=24;case"hour":t*=60;case"minute":t*=60;case"second":t*=1e3;case"millisecond":break;case"year":return this.addMonths(n,12*t);case"quarter":return this.addMonths(n,3*t);case"month":return this.addMonths(n,t);default:return e}return e+t},e.prototype.diff=function(e,t,r){var n=this.toDate(e),a=this.toDate(t);if(null==n||null==a)return NaN;var i=n.getTime()-a.getTime();switch(r){case"week":i/=7;case"day":i/=24;case"hour":i/=60;case"minute":i/=60;case"second":i/=1e3;case"millisecond":break;case"month":return this.diffInMonths(n,a);case"quarter":return this.diffInMonths(n,a)/3;case"year":return this.diffInMonths(n,a)/12;default:return 0}return i<0?Math.ceil(i):Math.floor(i)},e.prototype.startOf=function(e,t,r){var n=this.toDate(e);if(null==n)return NaN;switch(t){case"second":n.setMilliseconds(0);break;case"minute":n.setSeconds(0,0);break;case"hour":n.setMinutes(0,0,0);break;case"quarter":var a=n.getMonth();n.setMonth(a-a%3);break;case"month":n.setDate(1);break;case"day":n.setHours(0,0,0,0);break;case"isoWeek":case"week":a=(n.getDay()+6)%7;n.setDate(n.getDate()-a),n.setHours(0,0,0,0);break;case"year":var i=new Date(0);return i.setFullYear(n.getFullYear(),0,1),i.setHours(0,0,0,0),i.getTime();default:return n.getTime()}return n.getTime()},e.prototype.endOf=function(e,t){var r=this.toDate(e);if(null==r)return NaN;switch(t){case"second":r.setMilliseconds(999);break;case"minute":r.setSeconds(59,999);break;case"hour":r.setMinutes(59,59,999);break;case"day":r.setHours(23,59,59,999);break;case"isoWeek":case"week":var n=(r.getDay()+6)%7;r.setDate(r.getDate()+6-n),r.setHours(23,59,59,999);break;case"month":var a=r.getMonth();r.setFullYear(r.getFullYear(),a+1,0),r.setHours(23,59,59,999);break;case"quarter":var i=r.getMonth();r.setMonth(i-i%3+3,0),r.setHours(23,59,59,999);break;case"year":var o=r.getFullYear();r.setFullYear(o+1,0,0),r.setHours(23,59,59,999);break;default:return e}return r.getTime()},e}();exports.StdDateAdapter=t,e._adapters._date.override(t.chartJsStandardAdapter())}));