overview
1 year ago
campaign_builder_controller.js
1 year ago
chart_controller.js
1 year 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
10 months ago
examiner_header_controller.js
1 year ago
export_overview_controller.js
10 months ago
export_reports_controller.js
2 years ago
filters_controller.js
10 months ago
group_controller.js
2 years ago
import_reports_controller.js
2 years ago
map_controller.js
10 months ago
migration_redirect_controller.js
2 years ago
modal_controller.js
2 years ago
pie_chart_controller.js
1 year ago
plugin_group_options_controller.js
1 year ago
pruner_controller.js
1 year ago
quick_stats_controller.js
1 year ago
real_time_controller.js
1 year ago
refresh_overview_controller.js
1 year ago
rename_report_controller.js
2 years ago
report_controller.js
10 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
woocommerce_settings_controller.js
1 year ago
pie_chart_controller.js
94 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 labels = this.dataValue.map((item) => item.label) |
| 23 | const values = this.dataValue.map((item) => item.value) |
| 24 | |
| 25 | const data = { |
| 26 | labels: labels, |
| 27 | datasets: [{ |
| 28 | data: values, |
| 29 | backgroundColor: [ |
| 30 | '#7B5BB3', |
| 31 | '#7FBAFD', |
| 32 | '#8ADBB0', |
| 33 | '#FD799E', |
| 34 | '#FFEA9C', |
| 35 | ], |
| 36 | hoverOffset: 4 |
| 37 | }] |
| 38 | } |
| 39 | |
| 40 | const config = { |
| 41 | type: 'pie', |
| 42 | options: { |
| 43 | responsive: true, |
| 44 | maintainAspectRatio: false, |
| 45 | borderColor: isDarkMode() ? '#363040' : '#ffffff' , |
| 46 | plugins: { |
| 47 | legend: { |
| 48 | position: 'left', |
| 49 | labels: { |
| 50 | color: isDarkMode() ? '#ffffff' : '#6D6A73', |
| 51 | } |
| 52 | }, |
| 53 | tooltip: { |
| 54 | callbacks: { |
| 55 | title: (tooltips) => { |
| 56 | const tooltip = tooltips[0] |
| 57 | const label = tooltip.label || '' |
| 58 | |
| 59 | return label |
| 60 | }, |
| 61 | label: (tooltip) => { |
| 62 | const chart = tooltip.chart |
| 63 | const data = this.dataValue[tooltip.dataIndex] |
| 64 | const total = this.dataValue.reduce((total, dataset, index) => { |
| 65 | if(!chart.getDataVisibility(index)) { |
| 66 | return total; |
| 67 | } |
| 68 | |
| 69 | return total + dataset.value |
| 70 | }, 0) |
| 71 | |
| 72 | return ` ${data.formatted_value ?? data.value} ${data.unit} (${this.formatPercent(data.value, total)})` |
| 73 | }, |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | }, |
| 78 | data: data, |
| 79 | } |
| 80 | |
| 81 | if(!this.chart) { |
| 82 | this.chart = new Chart(this.canvasTarget, config) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | formatPercent(value, total) { |
| 87 | const number = value > 0 ? value / total : 0 |
| 88 | const asPercent = new Intl.NumberFormat(this.localeValue, { |
| 89 | style: "percent", |
| 90 | }) |
| 91 | |
| 92 | return asPercent.format(number) |
| 93 | } |
| 94 | } |