- 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
1 line
3.5 KiB
JavaScript
1 line
3.5 KiB
JavaScript
export default()=>({charts:{},chartData:{},init(){this.initializeBarCharts()},async initializeBarCharts(){const t=document.querySelectorAll('[data-chart="bar"]');for(const a of t)await this.createBarChart(a)},async createBarChart(t){const a=t.id;if(!a)return;const e=await this.loadChartData(t),r=this.getBarChartOptions(t,e);this.charts[a]=new ApexCharts(t,r),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 r=await t.json();return console.log(`📊 Loaded bar chart data from API: ${a}`,r),this.mergeWithFallback(r,e)}}catch(t){console.warn(`⚠️ API failed for ${a}, using fallback data:`,t)}return console.log(`📊 Using fallback data for bar 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:[44,55,57,56,61,58,63,60,66]},{name:"Receitas",data:[76,85,101,98,87,105,91,114,94]}],categories:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep"],colors:["#3577f1","#51bb25"]},products:{series:[{name:"Produtos",data:[35,41,62,42,13,18,29,37,36,51,32,35]},{name:"Vendas",data:[87,57,74,99,75,38,62,47,82,56,45,47]}],categories:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],colors:["#f1b44c","#f06548"]},performance:{series:[{name:"Performance",data:[25,35,45,55,65,75,85,95,105,115,125,135]},{name:"Meta",data:[20,30,40,50,60,70,80,90,100,110,120,130]}],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},getBarChartOptions(t,a){const e=this.getColors(t,a.colors);return{series:a.series,chart:{type:"bar",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")},plotOptions:{bar:{horizontal:"true"===t.getAttribute("data-horizontal"),columnWidth:t.getAttribute("data-column-width")||"55%",borderRadius:parseInt(t.getAttribute("data-border-radius"))||4}},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,r=null){if(this.charts[t]){const s=this.charts[t],i=s.w.config.series;if(i[a]&&(i[a].data.push(e),s.updateSeries(i),r)){const t=s.w.config.xaxis.categories;t.push(r),s.updateOptions({xaxis:{categories:t}})}}},removeDataPoint(t,a,e){if(this.charts[t]){const r=this.charts[t],s=r.w.config.series;s[a]&&s[a].data[e]&&(s[a].data.splice(e,1),r.updateSeries(s))}},animateChart(t){this.charts[t]&&this.charts[t].updateSeries([],!0,!0)},getChart(t){return this.charts[t]},getChartData(t){return this.chartData[t]}}); |