Files
MoneyFlix/public/assets/js/alpine/charts/apexcharts-line.js
T
1sonhador 8542395fbe Add public campaign analytics functionality
- Add PublicCampaignsController for public campaign analytics
- Add public routes for /public/analytics/{slug} and /api/campaigns/{slug}
- Add public campaign analytics view with charts and KPIs
- Support for campaign sharing via custom slugs
- Real-time analytics with ApexCharts integration
2025-10-15 00:30:00 +01:00

1 line
3.7 KiB
JavaScript

export default()=>({charts:{},chartData:{},init(){this.$nextTick(()=>{"undefined"!=typeof ApexCharts?this.initializeLineCharts():(console.error("ApexCharts not found. Deferring initialization."),setTimeout(()=>this.initializeLineCharts(),500))})},async initializeLineCharts(){const t=document.querySelectorAll('[data-chart="line"]');for(const a of t)await this.createLineChart(a)},async createLineChart(t){const a=t.id;if(!a)return;const e=await this.loadChartData(t),s=this.getLineChartOptions(t,e);this.charts[a]=new ApexCharts(t,s),this.charts[a].render(),this.chartData[a]=e},async loadChartData(t){const a=t.getAttribute("data-api-url"),e=this.getFallbackData(t);if(a)try{const t=await fetch(a);if(t.ok){const s=await t.json();return console.log(`📊 Loaded data from API: ${a}`,s),this.mergeWithFallback(s,e)}}catch(t){console.warn(`⚠️ API failed for ${a}, using fallback data:`,t)}return console.log(`📊 Using fallback data for chart: ${t.id}`),e},mergeWithFallback:(t,a)=>({series:t.series||a.series,categories:t.categories||a.categories,colors:t.colors||a.colors,config:{...a.config,...t.config}}),getFallbackData(t){const a={sales:{series:[{name:"Vendas",data:[30,40,35,50,49,60,70,91,125]},{name:"Receitas",data:[20,29,37,36,44,45,50,58,60]}],categories:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep"],colors:["#3577f1","#51bb25"]},analytics:{series:[{name:"Utilizadores",data:[45,52,38,24,33,26,21,20,6,8,15,10]},{name:"Sessões",data:[35,41,62,42,13,18,29,37,36,51,32,35]}],categories:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],colors:["#f1b44c","#f06548"]},crm:{series:[{name:"Leads",data:[25,35,45,55,65,75,85,95,105,115,125,135]},{name:"Conversões",data:[15,25,35,45,55,65,75,85,95,105,115,125]}],categories:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],colors:["#5ac7fa","#6c757d"]}};return a[t.getAttribute("data-chart-type")||"sales"]||a.sales},getLineChartOptions(t,a){const e=this.getColors(t,a.colors);return{series:a.series,chart:{type:"line",height:parseInt(t.getAttribute("data-height"))||350,toolbar:{show:"false"!==t.getAttribute("data-toolbar")},zoom:{enabled:"false"!==t.getAttribute("data-zoom")}},colors:e,dataLabels:{enabled:"false"!==t.getAttribute("data-labels")},stroke:{curve:t.getAttribute("data-curve")||"smooth",width:parseInt(t.getAttribute("data-stroke-width"))||2},markers:{size:parseInt(t.getAttribute("data-marker-size"))||4,hover:{size:parseInt(t.getAttribute("data-marker-hover-size"))||6}},xaxis:{categories:a.categories,labels:{style:{colors:"#6c757d"}}},yaxis:{labels:{style:{colors:"#6c757d"}}},grid:{borderColor:"#f1f1f1"},tooltip:{theme:"dark",x:{show:!0}},legend:{position:t.getAttribute("data-legend-position")||"top",horizontalAlign:t.getAttribute("data-legend-align")||"left"}}},getColors(t,a=null){const e=t.getAttribute("data-colors");return e?e.split(",").map(t=>t.trim()):a||["#3577f1","#51bb25","#f1b44c","#f06548","#5ac7fa"]},async refreshChartData(t){const a=document.getElementById(t);if(!a)return;const e=await this.loadChartData(a);this.updateChartData(t,e),this.chartData[t]=e},updateChartData(t,a){this.charts[t]&&(this.charts[t].updateSeries(a.series),a.categories&&this.charts[t].updateOptions({xaxis:{categories:a.categories}}))},addDataPoint(t,a,e,s=null){if(this.charts[t]){const r=this.charts[t],i=r.w.config.series;if(i[a]&&(i[a].data.push(e),r.updateSeries(i),s)){const t=r.w.config.xaxis.categories;t.push(s),r.updateOptions({xaxis:{categories:t}})}}},removeDataPoint(t,a,e){if(this.charts[t]){const s=this.charts[t],r=s.w.config.series;r[a]&&r[a].data[e]&&(r[a].data.splice(e,1),s.updateSeries(r))}},animateChart(t){this.charts[t]&&this.charts[t].updateSeries([],!0,!0)},getChart(t){return this.charts[t]},getChartData(t){return this.chartData[t]}});