overview
1 year ago
campaign_builder_controller.js
1 year ago
chart_controller.js
2 months ago
chart_interval_controller.js
2 years ago
clipboard_controller.js
1 year ago
copy_report_controller.js
2 years ago
create_report_controller.js
1 year ago
delete_data_controller.js
1 year ago
delete_report_controller.js
2 years ago
easepick_controller.js
2 years ago
examiner_controller.js
2 months ago
examiner_header_controller.js
11 months ago
export_overview_controller.js
9 months ago
export_reports_controller.js
2 years ago
filters_controller.js
6 months ago
group_controller.js
2 years ago
import_reports_controller.js
2 years ago
journey_controller.js
6 months ago
map_controller.js
9 months ago
migration_redirect_controller.js
2 years ago
modal_controller.js
2 years ago
pause_emails_controller.js
6 months ago
pie_chart_controller.js
6 months ago
plugin_group_options_controller.js
1 year ago
pruner_controller.js
5 months ago
quick_stats_controller.js
1 year ago
real_time_controller.js
6 months ago
refresh_overview_controller.js
1 year ago
rename_report_controller.js
2 years ago
report_controller.js
2 months ago
reset_analytics_controller.js
1 year ago
reset_overview_controller.js
1 year ago
save_report_controller.js
2 years ago
select_input_controller.js
2 years ago
set_favorite_report_controller.js
2 years ago
sort_controller.js
2 years ago
sortable_reports_controller.js
2 years ago
table_columns_controller.js
2 years ago
tooltip_controller.js
6 months ago
woocommerce_settings_controller.js
1 year ago
pie_chart_controller.js
117 lines
| 1 | import {Controller} from "@hotwired/stimulus" |
| 2 | import corsairPlugin from '../chart_plugins/corsair_plugin' |
| 3 | import {Chart, registerables} from 'chart.js' |
| 4 | import color from 'color' |
| 5 | import {isDarkMode} from "../utils/appearance"; |
| 6 | |
| 7 | Chart.register(...registerables) |
| 8 | |
| 9 | export default class extends Controller { |
| 10 | static targets = ['canvas'] |
| 11 | |
| 12 | static values = { |
| 13 | data: Array, |
| 14 | locale: String, |
| 15 | } |
| 16 | |
| 17 | connect() { |
| 18 | this.renderChart() |
| 19 | } |
| 20 | |
| 21 | renderChart() { |
| 22 | const sortedData = this.dataValue.sort((a, b) => b.value - a.value); |
| 23 | const labels = sortedData.map((item) => item.label) |
| 24 | const values = sortedData.map((item) => item.value) |
| 25 | |
| 26 | const data = { |
| 27 | labels: labels, |
| 28 | datasets: [{ |
| 29 | data: values, |
| 30 | backgroundColor: [ |
| 31 | '#7B5BB3', |
| 32 | '#7FBAFD', |
| 33 | '#8ADBB0', |
| 34 | '#FD799E', |
| 35 | '#FFEA9C', |
| 36 | ], |
| 37 | hoverOffset: 4 |
| 38 | }] |
| 39 | } |
| 40 | |
| 41 | const config = { |
| 42 | type: 'pie', |
| 43 | options: { |
| 44 | responsive: true, |
| 45 | maintainAspectRatio: false, |
| 46 | borderColor: isDarkMode() ? '#363040' : '#ffffff' , |
| 47 | plugins: { |
| 48 | legend: { |
| 49 | position: 'left', |
| 50 | labels: { |
| 51 | color: isDarkMode() ? '#ffffff' : '#6D6A73', |
| 52 | boxHeight: 18, |
| 53 | boxWidth: 18, |
| 54 | useBorderRadius: true, |
| 55 | borderRadius: 9, |
| 56 | generateLabels: (chart) => { |
| 57 | const original = Chart.overrides.pie.plugins.legend.labels.generateLabels(chart); |
| 58 | const total = chart.data.datasets[0].data.reduce((sum, value, index) => { |
| 59 | return chart.getDataVisibility(index) ? sum + value : sum; |
| 60 | }, 0); |
| 61 | |
| 62 | return original.map((label, index) => { |
| 63 | const value = chart.data.datasets[0].data[index]; |
| 64 | const dataItem = sortedData[index]; |
| 65 | |
| 66 | return { |
| 67 | ...label, |
| 68 | text: `${label.text} (${this.formatPercent(value, total)})`, |
| 69 | lineWidth: 0, |
| 70 | }; |
| 71 | }); |
| 72 | } |
| 73 | } |
| 74 | }, |
| 75 | tooltip: { |
| 76 | callbacks: { |
| 77 | title: (tooltips) => { |
| 78 | const tooltip = tooltips[0] |
| 79 | const label = tooltip.label || '' |
| 80 | |
| 81 | return label |
| 82 | }, |
| 83 | label: (tooltip) => { |
| 84 | const chart = tooltip.chart |
| 85 | const data = sortedData[tooltip.dataIndex] |
| 86 | const total = sortedData.reduce((total, dataset, index) => { |
| 87 | if(!chart.getDataVisibility(index)) { |
| 88 | return total; |
| 89 | } |
| 90 | |
| 91 | return total + dataset.value |
| 92 | }, 0) |
| 93 | |
| 94 | return ` ${data.formatted_value ?? data.value} ${data.unit} (${this.formatPercent(data.value, total)})` |
| 95 | }, |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | }, |
| 100 | data: data, |
| 101 | } |
| 102 | |
| 103 | if(!this.chart) { |
| 104 | this.chart = new Chart(this.canvasTarget, config) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | formatPercent(value, total) { |
| 109 | const number = value > 0 ? value / total : 0 |
| 110 | const asPercent = new Intl.NumberFormat(this.localeValue, { |
| 111 | style: "percent", |
| 112 | }) |
| 113 | |
| 114 | return asPercent.format(number) |
| 115 | } |
| 116 | } |
| 117 |