UNPKG

vue-data-ui

Version:

A user-empowering data visualization Vue 3 components library for eloquent data storytelling

2 lines (1 loc) 17.6 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue"),r=require("./index-CBRI5uwl.cjs"),A=require("./useNestedProp-0jJvFRhK.cjs"),X=require("./vue-ui-skeleton-CM4wEsYA.cjs"),O=require("./useResponsive-N8jl0-aC.cjs"),H=require("./PackageVersion-D_gJvbGU.cjs"),U=require("./useChartAccessibility-Cm7nkzTG.cjs"),D=require("./_plugin-vue_export-helper-BHFhmbuH.cjs"),Z={__name:"SparkTooltip",props:{svgRef:{type:Object},x:{type:Number,required:!0},y:{type:Number,required:!0},prevX:{type:Number,required:!0},prevY:{type:Number,required:!0},offsetY:{type:Number,default:0},background:{type:String},backgroundOpacity:{type:Number,default:100},borderRadius:{type:Number,default:2},borderWidth:{type:Number,default:0},borderColor:{type:String,default:"#FFFFFF"},color:{type:String},fontSize:{type:Number}},setup(f){const c=f,w=e.toRef(c.svgRef),u=e.ref(null),y=e.ref(0),p=e.ref(0),m=e.ref(!1),V=e.computed(()=>r.setOpacity(c.background,c.backgroundOpacity)),N=async()=>{if(!w.value||!u.value)return;const t=r.placeHTMLElementAtSVGCoordinates({svgElement:w.value,element:u.value,x:c.x,y:c.y,offsetY:c.offsetY}),g=r.placeHTMLElementAtSVGCoordinates({svgElement:w.value,element:u.value,x:c.prevX,y:c.prevY,offsetY:c.offsetY});!t||!g||(m.value=!1,y.value=g.top,p.value=g.left,await e.nextTick(),setTimeout(()=>{m.value=!0,y.value=t.top,p.value=t.left},50))};return e.onMounted(N),e.watch(()=>[c.x,c.y,c.prevX,c.prevY],()=>N(),{immediate:!0}),(t,g)=>(e.openBlock(),e.createElementBlock("div",{ref_key:"tooltipRef",ref:u,class:"vue-data-ui-spark-tooltip",style:e.normalizeStyle({position:"fixed",top:`${y.value}px`,left:`${p.value}px`,pointerEvents:"none",background:V.value,color:c.color,fontSize:`${c.fontSize}px`,borderRadius:`${c.borderRadius}px`,border:`${c.borderWidth}px solid ${c.borderColor}`,transition:m.value?"top 0.3s ease-out, left 0.3s ease-out":"none"})},[e.renderSlot(t.$slots,"default",{},void 0,!0)],4))}},J=D._export_sfc(Z,[["__scopeId","data-v-c8e27542"]]),K=["id"],Q=["xmlns","viewBox"],ee=["width","height"],te=["id"],le=["stop-color"],ae=["stop-color"],oe=["id"],re=["stop-color"],ue=["stop-color"],ne=["id"],se=["stop-color"],ie=["stop-color"],de={key:1},ce=["d","fill"],ve=["d","fill"],pe=["d","stroke","stroke-width"],fe=["d","stroke","stroke-width"],ye=["x","y","width","height","fill","rx"],he=["x1","x2","y1","y2","stroke","stroke-width","stroke-dasharray"],me=["x1","x2","y1","y2","stroke","stroke-dasharray","stroke-width"],ge=["cx","cy","r","fill","stroke","stroke-width"],ke=["x","y","font-size","font-weight","fill"],be=["x","y","height","width","onMouseenter","onClick"],xe={__name:"vue-ui-sparkline",props:{config:{type:Object,default(){return{}}},dataset:{type:Array,default(){return[]}},showInfo:{type:Boolean,default:!0},selectedIndex:{type:Number,default:void 0},heightRatio:{type:Number,default:1},forcedPadding:{type:Number,default:30}},emits:["hoverIndex","selectDatapoint"],setup(f,{emit:c}){const{vue_ui_sparkline:w}=r.useConfig(),u=f,y=e.computed(()=>Array.isArray(u.dataset)&&u.dataset.length>0),p=e.ref(r.createUid()),m=e.ref(null),V=e.ref(null),N=e.ref(null),t=e.computed({get:()=>M(),set:l=>l}),{svgRef:g}=U.useChartAccessibility({config:t.value.style.title});function M(){const l=A.useNestedProp({userConfig:u.config,defaultConfig:w});let o={};return l.theme?o={...A.useNestedProp({userConfig:r.themes.vue_ui_sparkline[l.theme]||u.config,defaultConfig:l})}:o=l,u.config&&r.hasDeepProperty(u.config,"style.scaleMin")?o.style.scaleMin=u.config.style.scaleMin:o.style.scaleMin=null,u.config&&r.hasDeepProperty(u.config,"style.scaleMax")?o.style.scaleMax=u.config.style.scaleMax:o.style.scaleMax=null,o}const L=e.computed(()=>r.largestTriangleThreeBucketsArrayObjects({data:u.dataset,threshold:t.value.downsample.threshold}));e.watch(()=>u.config,l=>{t.value=M(),z(),n.value.chartWidth=t.value.style.chartWidth},{deep:!0}),e.watch(()=>u.dataset,l=>{k.value=r.largestTriangleThreeBucketsArrayObjects({data:u.dataset.map(o=>({...o,value:[void 0].includes(o.value)?null:o.value})),threshold:t.value.downsample.threshold})},{deep:!0});const k=e.ref(W());function W(){return r.largestTriangleThreeBucketsArrayObjects({data:u.dataset.map(l=>t.value.style.animation.show&&u.dataset.length>1?{...l,value:null}:{...l,value:[void 0].includes(l.value)?null:l.value}),threshold:t.value.downsample.threshold})}const I=e.ref(null);e.onMounted(()=>{if(z(),t.value.style.animation.show&&u.dataset.length>1){let a=function(){o<L.value.length?(k.value.push(L.value[o]),setTimeout(()=>{requestAnimationFrame(a)},l)):k.value=L.value,o+=1};k.value=[];const l=t.value.style.animation.animationFrames/u.dataset.length;let o=0;a()}});function z(){if(r.objectIsEmpty(u.dataset)?r.error({componentName:"VueUiSparkline",type:"dataset"}):u.dataset.forEach((l,o)=>{r.getMissingDatasetAttributes({datasetObject:l,requiredAttributes:["period","value"]}).forEach(a=>{r.error({componentName:"VueUiSparkline",type:"datasetSerieAttribute",property:a,index:o})})}),t.value.responsive){const l=O.throttle(()=>{const{width:o,height:a}=O.useResponsive({chart:m.value,title:t.value.style.title.show&&u.showInfo?V.value:null,source:N.value});requestAnimationFrame(()=>{n.value.width=o,n.value.height=a,n.value.chartWidth=t.value.style.chartWidth/500*o,n.value.padding=u.forcedPadding/500*o})});I.value=new ResizeObserver(l),I.value.observe(m.value.parentNode)}}const n=e.ref({height:80*u.heightRatio,width:500,chartWidth:t.value.style.chartWidth,padding:u.forcedPadding}),$=c,s=e.computed(()=>{const{top:l,right:o,bottom:a,left:i}=t.value.style.padding;return{top:l,left:i,right:n.value.width-o,bottom:n.value.height-a,start:u.showInfo&&t.value.style.dataLabel.show&&t.value.style.dataLabel.position==="left"?n.value.width-n.value.chartWidth+i:n.value.padding+i,width:u.showInfo&&t.value.style.dataLabel.show?n.value.chartWidth-i-o:n.value.width-n.value.padding-i-o,height:n.value.height-l-a}}),E=e.computed(()=>[null,void 0].includes(t.value.style.scaleMin)?Math.min(...k.value.map(l=>isNaN(l.value)||[void 0,null,"NaN",NaN,1/0,-1/0].includes(l.value)?0:l.value||0)):t.value.style.scaleMin),F=e.computed(()=>[null,void 0].includes(t.value.style.scaleMax)?Math.max(...k.value.map(l=>isNaN(l.value)||[void 0,null,"NaN",NaN,1/0,-1/0].includes(l.value)?0:l.value||0)):t.value.style.scaleMax),B=e.computed(()=>{const l=E.value>=0?0:E.value;return Math.abs(l)}),P=e.computed(()=>F.value+B.value),x=e.computed(()=>s.value.bottom-s.value.height*S(B.value));function S(l){return isNaN(l/P.value)?0:l/P.value}const b=e.computed(()=>L.value.length-1),v=e.computed(()=>k.value.map((l,o)=>{const a=isNaN(l.value)||[void 0,null,"NaN",NaN,1/0,-1/0].includes(l.value)?0:l.value||0,i=s.value.width/(b.value+1)>n.padding?n.padding:s.value.width/(b.value+1);return{absoluteValue:a,period:l.period,plotValue:a+B.value,toMax:S(a+B.value),x:s.value.start+o*(i>s.value.width/12?s.value.width/12:i),y:s.value.bottom-s.value.height*S(a+B.value),id:`plot_${p.value}_${o}`,color:C.value?t.value.style.bar.color:t.value.style.area.useGradient?r.shiftHue(t.value.style.line.color,.05*(1-o/b.value)):t.value.style.line.color,width:i>s.value.width/12?s.value.width/12:i}})),q=e.computed(()=>{const l={x:v.value[0].x||0,y:(n.value.height||0)-6},o={x:v.value[v.value.length-1].x||0,y:(n.value.height||0)-6},a=[];return v.value.forEach(i=>{a.push(`${i.x||0},${i.y||0} `)}),[l.x,l.y,...a,o.x,o.y].toString()}),d=e.ref(void 0),_=e.ref(void 0);function Y(l,o){d.value=l,_.value||(_.value=l),$("hoverIndex",{index:o})}function j(){_.value=d.value,d.value=void 0,$("hoverIndex",{index:void 0})}const h=e.computed(()=>{if(y.value){const l=v.value.map(a=>a.absoluteValue),o=l.reduce((a,i)=>a+i,0);return{latest:v.value[v.value.length-1].absoluteValue,sum:o,average:o/v.value.length,median:r.calcMedian(l),trend:r.calcLinearProgression(v.value.map(({x:a,y:i,absoluteValue:T})=>({x:a,y:i,value:T}))).trend}}else return{latest:null,sum:null,average:null,median:null,trend:null}}),R=e.computed(()=>y.value?t.value.style.dataLabel.valueType==="latest"?h.value.latest:t.value.style.dataLabel.valueType==="sum"?h.value.sum:t.value.style.dataLabel.valueType==="average"?h.value.average:0:0),C=e.computed(()=>t.value.type&&t.value.type==="bar");function G(l,o){$("selectDatapoint",{datapoint:l,index:o})}return(l,o)=>(e.openBlock(),e.createElementBlock("div",{ref_key:"sparklineChart",ref:m,class:"vue-ui-sparkline",id:p.value,style:e.normalizeStyle(`width:100%;font-family:${t.value.style.fontFamily};`)},[e.renderSlot(l.$slots,"before",e.normalizeProps(e.guardReactiveProps({selected:d.value,latest:h.value.latest,sum:h.value.sum,average:h.value.average,median:h.value.median,trend:h.value.trend})),void 0,!0),t.value.style.title.show&&f.showInfo?(e.openBlock(),e.createElementBlock("div",{key:0,ref_key:"chartTitle",ref:V,class:"vue-ui-sparkline-title",style:e.normalizeStyle(`display:flex;align-items:center;width:100%;color:${t.value.style.title.color};background:${t.value.style.backgroundColor};justify-content:${t.value.style.title.textAlign==="left"?"flex-start":t.value.style.title.textAlign==="right"?"flex-end":"center"};height:${t.value.style.title.fontSize*2}px;font-size:${t.value.style.title.fontSize}px;font-weight:${t.value.style.title.bold?"bold":"normal"};`)},[e.createElementVNode("span",{style:e.normalizeStyle(`padding:${t.value.style.title.textAlign==="left"?"0 0 0 12px":t.value.style.title.textAlign==="right"?"0 12px 0 0":"0"}`)},e.toDisplayString(d.value?d.value.period:t.value.style.title.text),5)],4)):e.createCommentVNode("",!0),y.value?(e.openBlock(),e.createElementBlock("svg",{key:1,ref_key:"svgRef",ref:g,xmlns:e.unref(r.XMLNS),viewBox:`0 0 ${n.value.width} ${n.value.height}`,style:e.normalizeStyle(`background:${t.value.style.backgroundColor};overflow:visible`),onMouseleave:o[0]||(o[0]=a=>_.value=void 0)},[e.createVNode(H._sfc_main),l.$slots["chart-background"]?(e.openBlock(),e.createElementBlock("foreignObject",{key:0,x:0,y:0,width:n.value.width<=0?10:n.value.width,height:n.value.height<=0?10:n.value.height,style:{pointerEvents:"none"}},[e.renderSlot(l.$slots,"chart-background",{},void 0,!0)],8,ee)):e.createCommentVNode("",!0),e.createElementVNode("defs",null,[e.createElementVNode("linearGradient",{x1:"0%",y1:"0%",x2:"100%",y2:"0%",id:`sparkline_gradient_${p.value}`},[e.createElementVNode("stop",{offset:"0%","stop-color":e.unref(r.setOpacity)(e.unref(r.shiftHue)(t.value.style.area.color,.05),t.value.style.area.opacity)},null,8,le),e.createElementVNode("stop",{offset:"100%","stop-color":e.unref(r.setOpacity)(t.value.style.area.color,t.value.style.area.opacity)},null,8,ae)],8,te),e.createElementVNode("linearGradient",{x2:"0%",y2:"100%",id:`sparkline_bar_gradient_pos_${p.value}`},[e.createElementVNode("stop",{offset:"0%","stop-color":t.value.style.bar.color},null,8,re),e.createElementVNode("stop",{offset:"100%","stop-color":e.unref(r.shiftHue)(t.value.style.bar.color,.05)},null,8,ue)],8,oe),e.createElementVNode("linearGradient",{x2:"0%",y2:"100%",id:`sparkline_bar_gradient_neg_${p.value}`},[e.createElementVNode("stop",{offset:"0%","stop-color":e.unref(r.shiftHue)(t.value.style.bar.color,.05)},null,8,se),e.createElementVNode("stop",{offset:"100%","stop-color":t.value.style.bar.color},null,8,ie)],8,ne)]),t.value.style.area.show&&!C.value&&v.value[0]?(e.openBlock(),e.createElementBlock("g",de,[t.value.style.line.smooth?(e.openBlock(),e.createElementBlock("path",{key:0,d:`M ${v.value[0].x},${s.value.bottom} ${e.unref(r.createSmoothPath)(v.value)} L ${v.value.at(-1).x},${s.value.bottom} Z`,fill:t.value.style.area.useGradient?`url(#sparkline_gradient_${p.value})`:e.unref(r.setOpacity)(t.value.style.area.color,t.value.style.area.opacity)},null,8,ce)):(e.openBlock(),e.createElementBlock("path",{key:1,d:`M${q.value}Z`,fill:t.value.style.area.useGradient?`url(#sparkline_gradient_${p.value})`:e.unref(r.setOpacity)(t.value.style.area.color,t.value.style.area.opacity)},null,8,ve))])):e.createCommentVNode("",!0),t.value.style.line.smooth&&!C.value?(e.openBlock(),e.createElementBlock("path",{key:2,d:`M ${e.unref(r.createSmoothPath)(v.value)}`,stroke:t.value.style.line.color,fill:"none","stroke-width":t.value.style.line.strokeWidth,"stroke-linecap":"round"},null,8,pe)):e.createCommentVNode("",!0),!t.value.style.line.smooth&&!C.value?(e.openBlock(),e.createElementBlock("path",{key:3,d:`M ${e.unref(r.createStraightPath)(v.value)}`,stroke:t.value.style.line.color,fill:"none","stroke-width":t.value.style.line.strokeWidth,"stroke-linecap":"round"},null,8,fe)):e.createCommentVNode("",!0),(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(v.value,(a,i)=>(e.openBlock(),e.createElementBlock("g",null,[C.value?(e.openBlock(),e.createElementBlock("rect",{key:0,x:a.x-a.width/2,y:isNaN(a.absoluteValue>0?a.y:x.value)?0:a.absoluteValue>0?a.y:x.value,width:a.width,height:isNaN(Math.abs(a.y-x.value))?0:Math.abs(a.y-x.value),fill:a.absoluteValue>0?`url(#sparkline_bar_gradient_pos_${p.value})`:`url(#sparkline_bar_gradient_neg_${p.value})`,rx:t.value.style.bar.borderRadius},null,8,ye)):e.createCommentVNode("",!0),t.value.style.verticalIndicator.show&&(d.value&&a.id===d.value.id||f.selectedIndex===i)?(e.openBlock(),e.createElementBlock("line",{key:1,x1:a.x,x2:a.x,y1:s.value.top-6,y2:s.value.bottom,stroke:t.value.style.verticalIndicator.color||a.color,"stroke-width":t.value.style.verticalIndicator.strokeWidth,"stroke-linecap":"round","stroke-dasharray":t.value.style.verticalIndicator.strokeDasharray||0},null,8,he)):e.createCommentVNode("",!0)]))),256)),E.value<0?(e.openBlock(),e.createElementBlock("line",{key:4,x1:s.value.start,x2:s.value.start+s.value.width-16,y1:e.unref(r.forceValidValue)(x.value,s.value.bottom),y2:e.unref(r.forceValidValue)(x.value,s.value.bottom),stroke:t.value.style.zeroLine.color,"stroke-dasharray":t.value.style.zeroLine.strokeWidth*2,"stroke-width":t.value.style.zeroLine.strokeWidth,"stroke-linecap":"round"},null,8,me)):e.createCommentVNode("",!0),t.value.style.plot.show?(e.openBlock(!0),e.createElementBlock(e.Fragment,{key:5},e.renderList(v.value,(a,i)=>(e.openBlock(),e.createElementBlock("g",null,[d.value&&a.id===d.value.id||f.selectedIndex===i||f.dataset.length===1?(e.openBlock(),e.createElementBlock("circle",{key:0,cx:a.x,cy:a.y,r:t.value.style.plot.radius,fill:a.color,stroke:t.value.style.plot.stroke,"stroke-width":t.value.style.plot.strokeWidth},null,8,ge)):e.createCommentVNode("",!0)]))),256)):e.createCommentVNode("",!0),f.showInfo&&t.value.style.dataLabel.show?(e.openBlock(),e.createElementBlock("text",{key:6,x:t.value.style.dataLabel.position==="left"?12+t.value.style.dataLabel.offsetX:s.value.width+12+t.value.style.dataLabel.offsetX,y:n.value.height/2+t.value.style.dataLabel.fontSize/2.5+t.value.style.dataLabel.offsetY,"font-size":t.value.style.dataLabel.fontSize,"font-weight":t.value.style.dataLabel.bold?"bold":"normal",fill:t.value.style.dataLabel.color},e.toDisplayString(d.value?e.unref(r.applyDataLabel)(t.value.style.dataLabel.formatter,d.value.absoluteValue,e.unref(r.dataLabel)({p:t.value.style.dataLabel.prefix,v:d.value.absoluteValue,s:t.value.style.dataLabel.suffix,r:t.value.style.dataLabel.roundingValue}),{datapoint:d.value}):e.unref(r.applyDataLabel)(t.value.style.dataLabel.formatter,R.value,e.unref(r.dataLabel)({p:t.value.style.dataLabel.prefix,v:R.value,s:t.value.style.dataLabel.suffix,r:t.value.style.dataLabel.roundingValue}))),9,ke)):e.createCommentVNode("",!0),(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(v.value,(a,i)=>(e.openBlock(),e.createElementBlock("rect",{x:a.x-(s.value.width/(b.value+1)>n.value.padding?n.value.padding:s.value.width/(b.value+1))/2,y:s.value.top-6,height:s.value.height+6,width:s.value.width/(b.value+1)>n.value.padding?n.value.padding:s.value.width/(b.value+1),fill:"transparent",onMouseenter:T=>Y(a,i),onMouseleave:j,onClick:()=>G(a,i)},null,40,be))),256)),e.renderSlot(l.$slots,"svg",{svg:n.value},void 0,!0)],44,Q)):e.createCommentVNode("",!0),d.value&&t.value.style.tooltip.show?(e.openBlock(),e.createBlock(J,{key:2,x:d.value.x,y:d.value.y,prevX:_.value.x,prevY:_.value.y,offsetY:t.value.style.plot.radius*3+t.value.style.tooltip.offsetY,svgRef:e.unref(g),background:t.value.style.tooltip.backgroundColor,color:t.value.style.tooltip.color,fontSize:t.value.style.tooltip.fontSize,borderWidth:t.value.style.tooltip.borderWidth,borderColor:t.value.style.tooltip.borderColor,borderRadius:t.value.style.tooltip.borderRadius,backgroundOpacity:t.value.style.tooltip.backgroundOpacity},{default:e.withCtx(()=>[e.renderSlot(l.$slots,"tooltip",e.normalizeProps(e.guardReactiveProps({...d.value})),()=>[e.createTextVNode(e.toDisplayString(d.value.period)+": "+e.toDisplayString(e.unref(r.applyDataLabel)(t.value.style.dataLabel.formatter,d.value.absoluteValue,e.unref(r.dataLabel)({p:t.value.style.dataLabel.prefix,v:d.value.absoluteValue,s:t.value.style.dataLabel.suffix,r:t.value.style.dataLabel.roundingValue}),{datapoint:d.value})),1)],!0)]),_:3},8,["x","y","prevX","prevY","offsetY","svgRef","background","color","fontSize","borderWidth","borderColor","borderRadius","backgroundOpacity"])):e.createCommentVNode("",!0),l.$slots.source?(e.openBlock(),e.createElementBlock("div",{key:3,ref_key:"source",ref:N,dir:"auto"},[e.renderSlot(l.$slots,"source",{},void 0,!0)],512)):e.createCommentVNode("",!0),y.value?e.createCommentVNode("",!0):(e.openBlock(),e.createBlock(X.default,{key:4,config:{type:"sparkline",style:{backgroundColor:t.value.style.backgroundColor,sparkline:{color:"#CCCCCC"}}}},null,8,["config"]))],12,K))}},_e=D._export_sfc(xe,[["__scopeId","data-v-246ce941"]]);exports.default=_e;