Smart-Dashboard/js/chartjs-scale-timestack.min.js
2026-02-14 20:08:34 +01:00

2 lines
10 KiB
JavaScript

var _timestack=(()=>{var Z=Object.create;var v=Object.defineProperty;var tt=Object.getOwnPropertyDescriptor;var et=Object.getOwnPropertyNames;var it=Object.getPrototypeOf,nt=Object.prototype.hasOwnProperty;var Y=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports),ot=(r,e)=>{for(var t in e)v(r,t,{get:e[t],enumerable:!0})},N=(r,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of et(e))!nt.call(r,n)&&n!==t&&v(r,n,{get:()=>e[n],enumerable:!(i=tt(e,n))||i.enumerable});return r};var R=(r,e,t)=>(t=r!=null?Z(it(r)):{},N(e||!r||!r.__esModule?v(t,"default",{value:r,enumerable:!0}):t,r)),st=r=>N(v({},"__esModule",{value:!0}),r);var z=Y((lt,P)=>{P.exports=Chart});var E=Y((_t,$)=>{$.exports=luxon});var rt={};ot(rt,{DEF_TICK_GENERATORS:()=>G,DEF_TOOLTIP_FORMAT:()=>q,DaysTickGenerator:()=>D,HM:()=>h,HMS:()=>S,MD:()=>u,MDAY:()=>F,MON:()=>b,TickGenerator:()=>j,TimestackScale:()=>x,YEAR:()=>y,YM:()=>M,YMD:()=>f,YearsTickGenerator:()=>g});var K=R(z());var V=R(z()),U=R(E());var A=R(E());var C=R(E()),B=new Map,H=new Map;function w(r,e,t){let i=[{},0];for(let n of e){if(!n.isValid)throw"invalid datetime";let o=n.toLocaleString(t),s=r.measureText(o).width;s>i[1]&&(i=[n,s,o])}return i[0]}function at(r,e,t,i){let n=`${e.month}/${e.weekday}`,o=B.get(r);if(o)return o[n];let s={year:2024,month:12,day:22,hour:23,minute:59,second:59},m=C.DateTime.fromObject(s,i);function*l(I){for(let k=1;k<=12;k++)yield I.set({month:k})}let c=w(t,l(m),{month:"short"}),a=w(t,l(m),{month:"long"});function*d(I){for(let k=22;k<29;k++)yield I.set({day:k})}let p=w(t,d(c),{weekday:"short"}),T=w(t,d(c),{weekday:"long"}),O=w(t,d(c),{weekday:"narrow"}),Q=w(t,d(a),{weekday:"short"}),W=w(t,d(a),{weekday:"long"}),X=w(t,d(a),{weekday:"narrow"}),J=Object.fromEntries(Object.entries({"undefined/undefined":m,"undefined/short":p,"undefined/long":T,"undefined/narrow":O,"numeric/undefined":m,"numeric/short":p,"numeric/long":T,"numeric/narrow":O,"2-digit/undefined":m,"2-digit/short":p,"2-digit/long":T,"2-digit/narrow":O,"short/undefined":c,"short/short":p,"short/long":T,"short/narrow":O,"long/undefined":a,"long/short":Q,"long/long":W,"long/narrow":X}).map(([I,k])=>[I,k.toObject()]));return B.set(r,J),J[n]}function L(r,e,t){let i=JSON.stringify(r),n=`${t.locale||C.Settings.defaultLocale}/${e.font}`,o=`${n}/${i}`,s=H.get(o);if(s)return s;let m=at(n,r,e,t),c=C.DateTime.fromObject(m,t).toLocaleString(r),a=e.measureText(c).width;return H.set(o,a),a}var j=class{*seq(e){}constructor(e,t){this.top=e,this.bottom=t}estimate(e,t,i,n=!0){let o=this.top,s=this.bottom,m=L(o.fmt,t,i),l=o.maj_fmt?L(o.maj_fmt,t,i):0,c={nticks:e/o.size,label_width:Math.max(m,l)},a;if(s){let d=L(s.short_fmt,t,i),p=n&&s.long_fmt?L(s.long_fmt,t,i):0;a={nticks:e/s.size,label_width:Math.max(d,p)}}return{top:c,bottom:a}}format(e,t,i,n){let o=e.toLocaleString(t&&this.top.maj_fmt?this.top.maj_fmt:this.top.fmt);if(!i||!this.bottom)return o;let s=e.toLocaleString(n&&this.bottom.long_fmt?this.bottom.long_fmt:this.bottom.short_fmt);return[o,s]}create(e,t,i){let n=[];for(let{dt:o,is_major:s,with_bottom:m}of this.seq(e))if(!(o<e)){if(o>=t)break;n.push({value:o.toMillis(),major:s,label:this.format(o,s,m,i(o))})}return n}create_floating(e,t,i){let n=this.bottom,o=n?i(e)&&n.long_fmt?n.long_fmt:n.short_fmt:void 0,s=o?e.toLocaleString(o):"";return{value:e.toMillis(),label:["",t==="left"?"\u2026"+s:s+"\u2026"]}}patch_formats(e){var i,n;function t(o){for(let s of Object.entries(e)){let m=s[0],l=s[1];m==="hour12"&&o.hour&&(o.hour12=l),o[m]&&(o[m]=s[1])}}t(this.top.fmt),this.top.maj_fmt&&t(this.top.maj_fmt),(i=this.bottom)!=null&&i.short_fmt&&t(this.bottom.short_fmt),(n=this.bottom)!=null&&n.long_fmt&&t(this.bottom.long_fmt)}},_=class extends j{constructor(e,t){let{fmt:i,maj_fmt:n,align:o,maj_unit:s,...m}=e;super({fmt:i,maj_fmt:n,size:A.Duration.fromDurationLike(m).toMillis()},t&&{...t,size:A.Duration.fromObject({[t.unit]:1}).toMillis()}),this.step=m,this.bottom_unit=t==null?void 0:t.unit,this.align=o,this.maj_unit=s}*seq(e){let t=e.startOf(this.align);for(;;){let i=this.bottom_unit?t.startOf(this.bottom_unit).equals(t):!1,n=this.maj_unit?t.startOf(this.maj_unit).equals(t):!1;yield{dt:t,is_major:n,with_bottom:i},t=t.plus(this.step)}}},D=class extends j{constructor(e,t){super({...e,size:e.step*86400*1e3},t&&{...t,size:30*86400*1e3}),this.days=e.days,this.maj_days=e.maj_days??[1],this.bottom_days=t?t.days??[1]:[]}*seq(e){let t=e.startOf("month");for(;;){for(let i of this.days){let n=t.set({day:i}),o=this.maj_days.includes(i),s=this.bottom_days.includes(i);yield{dt:n,is_major:o,with_bottom:s}}t=t.plus({month:1})}}},g=class extends j{constructor(e,t){super({...t,size:e*365*86400*1e3}),this.by_years=e}*seq(e){let t=(e.year/this.by_years|0)*this.by_years,i=e.startOf("year").set({year:t});for(;;)yield{dt:i,is_major:!1,with_bottom:!1},i=i.plus({year:this.by_years})}};var S={hour:"numeric",minute:"numeric",second:"numeric"},h={hour:"numeric",minute:"numeric"},F={day:"numeric"},b={month:"short"},y={year:"numeric"},f={year:"numeric",month:"short",day:"numeric"},M={year:"numeric",month:"short"},u={month:"short",day:"numeric"},G=[new _({second:1,align:"second",maj_unit:"minute",fmt:S,maj_fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({second:5,align:"minute",maj_unit:"minute",fmt:S,maj_fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({second:10,align:"minute",maj_unit:"minute",fmt:S,maj_fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({second:30,align:"minute",maj_unit:"minute",fmt:S,maj_fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({minute:1,align:"minute",maj_unit:"hour",fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({minute:5,align:"hour",maj_unit:"hour",fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({minute:10,align:"hour",maj_unit:"hour",fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({minute:15,align:"hour",maj_unit:"hour",fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({minute:30,align:"hour",maj_unit:"hour",fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({hour:1,align:"hour",maj_unit:"day",fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({hour:3,align:"day",maj_unit:"day",fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({hour:6,align:"day",maj_unit:"day",fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({hour:12,align:"day",maj_unit:"day",fmt:h},{unit:"day",short_fmt:u,long_fmt:f}),new _({day:1,align:"day",maj_unit:"month",fmt:F},{unit:"month",short_fmt:b,long_fmt:M}),new D({days:[1,5,10,15,20,25],step:5,fmt:F},{short_fmt:b,long_fmt:M}),new D({days:[1,10,20],step:10,fmt:F},{short_fmt:b,long_fmt:M}),new D({days:[1,15],step:15,fmt:F},{short_fmt:b,long_fmt:M}),new _({month:1,align:"month",maj_unit:"year",fmt:b},{unit:"year",short_fmt:y}),new _({month:3,align:"year",maj_unit:"year",fmt:b},{unit:"year",short_fmt:y}),new _({month:6,align:"year",maj_unit:"year",fmt:b},{unit:"year",short_fmt:y}),new g(1,{fmt:y}),new g(5,{fmt:y}),new g(10,{fmt:y}),new g(25,{fmt:y}),new g(50,{fmt:y}),new g(100,{fmt:y}),new g(1e3,{fmt:y})];var q={year:"numeric",month:"long",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric"},x=class extends V.Scale{constructor(e){var n,o,s,m,l;super(e);let t=(s=(o=(n=e.chart)==null?void 0:n.config.options)==null?void 0:o.scales)==null?void 0:s[e.id],i=(m=t==null?void 0:t.timestack)!=null&&m.make_tick_generators?(l=t==null?void 0:t.timestack)==null?void 0:l.make_tick_generators():G;if(this._gens=i,t!=null&&t.timestack.format_style)for(let c of i)c.patch_formats(t==null?void 0:t.timestack.format_style)}init(e){this._dt_opts=e.timestack.datetime??{},super.init(e)}determineDataLimits(){let{min:e,max:t}=this.getMinMax(!1);e=isFinite(e)?e:this._dt_now().startOf("day").toMillis(),t=isFinite(t)?t:this._dt_now().endOf("day").toMillis()+1,this.min=Math.min(e,t-1),this.max=Math.max(e+1,t)}_dt_from_ts(e){return U.DateTime.fromMillis(e,this._dt_opts)}_dt_from_object(e){return U.DateTime.fromObject(e,this._dt_opts)}_dt_now(){return U.DateTime.local(this._dt_opts)}_choose_gen(e){let t=this._gens,i=this.options.timestack.max_density,n=this.options.timestack.density,o=this.options.ticks.maxTicksLimit??1/0,s=[];for(let l of t){let{top:c,bottom:a}=l.estimate(e,this.ctx,this._dt_opts),d=Math.max(c.nticks,(a==null?void 0:a.nticks)??0),p=c.nticks*c.label_width/this.width,T=a?a.nticks*a.label_width/this.width:0,O=Math.max(p,T);O<=i&&d<=o&&s.push([l,Math.abs(O-n)])}return s.length?s.reduce((l,c)=>c[1]<l[1]?c:l)[0]:void 0}_need_floating_left_tick(e){let t=this.options.timestack.left_floating_tick_thres;return t===!1?!1:e.length?(e[0].value-this.min)/(this.max-this.min)>t:!0}_need_floating_right_tick(e){let t=this.options.timestack.right_floating_tick_thres;return t===!1?!1:e.length?(this.max-e[e.length-1].value)/(this.max-this.min)>t:!0}_build_ticks(){let{min:e,max:t}=this,i=this._choose_gen(t-e);if(!i)return console.warn("Failed to choose the tick generator"),[];let n=this._dt_from_ts(e),o=this._dt_from_ts(t),s=this._dt_now(),m=a=>!a.hasSame(s,"year"),l=i.create(n,o,m);if(!i.bottom)return l;let c=l.filter(a=>Array.isArray(a.label)&&a.label.length>1);if(this._need_floating_left_tick(c)){let a;if(a=i.create_floating(n,"left",m),c.length){let d=c[0],p=this.ctx.measureText(a.label[1]).width,T=(d.value-this.min)*this.width/(this.max-this.min);p*2>T&&(a=void 0)}a&&l.unshift(a)}if(this._need_floating_right_tick(c)){let a;if(a=i.create_floating(o,"right",m),c.length){let d=c[c.length-1],p=this.ctx.measureText(a.label[1]).width,T=(this.max-d.value)*this.width/(this.max-this.min);p*2>T&&(a=void 0)}a&&l.push(a)}return l}buildTicks(){let e;try{e=this._resolveTickFontOptions(0).string}catch{console.warn("failed to resolve the font")}this.ctx.save(),e&&(this.ctx.font=e);let t=this._build_ticks();return this.ctx.restore(),t}getLabelForValue(e){return this._dt_from_ts(e).toLocaleString(this.options.timestack.tooltip_format)}generateTickLabels(e){}getPixelForValue(e){let t=e===null?NaN:(e-this.min)/(this.max-this.min);return this.getPixelForDecimal(t)}getValueForPixel(e){let t=this.getDecimalForPixel(e);return this.min+t*(this.max-this.min)}};x.id="timestack",x.defaults={timestack:{tooltip_format:q,density:.5,max_density:.75,left_floating_tick_thres:.33,right_floating_tick_thres:!1},ticks:{source:"",maxRotation:0,autoSkip:!1}};K.Chart.register(x);return st(rt);})();