UNPKG

framework7

Version:

Full featured mobile HTML framework for building iOS & Android apps

2 lines (1 loc) 4.48 kB
(function framework7ComponentLoader(t,e){void 0===e&&(e=!0);var o=t.$,r=t.utils,s=(t.getDevice,t.getSupport,t.Class),a=(t.Modal,t.ConstructorMethods),i=(t.ModalMethods,t.$jsx),l=r.extend,n=r.deleteProps;class p extends s{constructor(t,e){void 0===e&&(e={}),super(e,[t]);const r=this,s=l({},t.params.pieChart);r.useModulesParams(s),r.params=l(s,e);const{el:a}=r.params;if(!a)return r;const i=o(a);return 0===i.length?r:i[0].f7PieChart?i[0].f7PieChart:(l(r,{app:t,$el:i,el:i&&i[0],currentIndex:null,f7Tooltip:null}),i[0].f7PieChart=r,r.useModules(),r.showTooltip=r.showTooltip.bind(this),r.hideTooltip=r.hideTooltip.bind(this),r.init(),r)}getSummValue(){const{datasets:t}=this.params;let e=0;return t.map((t=>t.value||0)).forEach((t=>{e+=t})),e}getPaths(){const{datasets:t,size:e}=this.params,o=[];let r=0;function s(t){return[Math.cos(2*Math.PI*t)*(e/3),Math.sin(2*Math.PI*t)*(e/3)]}return t.forEach((t=>{let{value:a,label:i,color:l}=t;const n=a/this.getSummValue(),[p,h]=s(r);r+=n;const[d,c]=s(r),u=[`M ${p} ${h}`,`A ${e/3} ${e/3} 0 ${n>.5?1:0} 1 ${d} ${c}`,"L 0 0"].join(" ");o.push({points:u,label:i,color:l})})),o}formatTooltipText(){const{datasets:t}=this.params,{currentIndex:e}=this;if(null===e)return"";const{value:o,label:r,color:s}=t[e],a=o/this.getSummValue()*100,i=t=>parseInt(t,10)===t?t:Math.round(100*t)/100;if(this.params.formatTooltip)return this.params.formatTooltip.call(this,{index:e,value:o,label:r,color:s,percentage:a});return`\n <div class="pie-chart-tooltip-label">\n <span class="pie-chart-tooltip-color" style="background-color: ${s};"></span> ${`${r?`${r}: `:""}${i(o)} (${i(a)}%)`}\n </div>\n `}setTooltip(){const t=this,{currentIndex:e,el:o,app:r,params:s}=t,{tooltip:a}=s;if((null!==e||t.f7Tooltip)&&a&&o)return null===e||t.f7Tooltip?void(t.f7Tooltip&&(null!==e?(t.f7Tooltip.setText(t.formatTooltipText()),t.f7Tooltip.setTargetEl(o.querySelector(`path[data-index="${e}"]`)),t.f7Tooltip.show()):t.f7Tooltip.hide())):(t.f7Tooltip=r.tooltip.create({trigger:"manual",containerEl:o,targetEl:o.querySelector(`path[data-index="${e}"]`),text:t.formatTooltipText(),cssClass:"pie-chart-tooltip"}),void t.f7Tooltip.show())}render(){const t=this.params.size,e=this.getPaths();return i("svg",{xmlns:"http://www.w3.org/2000/svg",width:t,viewBox:`-${t/3} -${t/3} ${2*t/3} ${2*t/3}`,style:"transform: rotate(-90deg)"},e.map(((t,e)=>i("path",{d:t.points,fill:t.color,"data-index":e}))))}update(t){void 0===t&&(t={});const e=this,{params:r}=e;if(Object.keys(t).forEach((e=>{void 0!==t[e]&&(r[e]=t[e])})),0===e.$svgEl.length)return e;e.$svgEl.remove(),delete e.$svgEl.f7PieChart;const s=o(e.render()).eq(0);return s.f7PieChart=e,l(e,{$svgEl:s,svgEl:s&&s[0]}),e.$el.append(s),e}setCurrentIndex(t){const e=this;if(t===e.currentIndex)return;const{datasets:o}=e.params;e.currentIndex=t,e.$el.trigger("piechart:select",{index:t,dataset:o[t]}),e.emit("local::select pieChartSelect",e,t,o[t])}showTooltip(t){const e=parseInt(t.target.getAttribute("data-index"),10);this.setCurrentIndex(e),this.$svgEl.find("path").removeClass("pie-chart-hidden").forEach(((t,e)=>{e!==this.currentIndex&&o(t).addClass("pie-chart-hidden")})),this.setTooltip()}hideTooltip(){this.setCurrentIndex(null),this.$svgEl.find("path").removeClass("pie-chart-hidden"),this.setTooltip()}init(){const t=this,e=o(t.render()).eq(0);return e.f7PieChart=t,l(t,{$svgEl:e,svgEl:e&&e[0]}),t.$el.append(e),t.$el.on("click mouseenter","path",t.showTooltip,!0),t.$el.on("mouseleave","path",t.hideTooltip,!0),t}destroy(){const t=this;t.$el&&!t.destroyed&&(t.$el.trigger("piechart:beforedestroy"),t.emit("local::beforeDestroy pieChartBeforeDestroy",t),t.$el.off("click mouseenter","path",t.showTooltip,!0),t.$el.off("mouseleave","path",t.hideTooltip,!0),t.$svgEl.remove(),t.f7Tooltip&&t.f7Tooltip.destroy&&t.f7Tooltip.destroy(),delete t.$el[0].f7PieChart,n(t),t.destroyed=!0)}}var h={name:"pieChart",params:{pieChart:{el:null,datasets:[],size:320,tooltip:!1,formatTooltip:null}},create(){const t=this;t.pieChart=a({defaultSelector:".pie-chart",constructor:p,app:t,domProp:"f7PieChart"}),t.pieChart.update=function(e,r){if(0===o(e).length)return;const s=t.pieChart.get(e);return s?(s.update(r),s):void 0}}};if(e){if(t.prototype.modules&&t.prototype.modules[h.name])return;t.use(h),t.instance&&(t.instance.useModuleParams(h,t.instance.params),t.instance.useModule(h))}return h}(Framework7, typeof Framework7AutoInstallComponent === 'undefined' ? undefined : Framework7AutoInstallComponent))