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
2 years ago
delete_data_controller.js
1 year ago
delete_report_controller.js
2 years ago
easepick_controller.js
2 years ago
export_overview_controller.js
1 year ago
export_reports_controller.js
2 years ago
filters_controller.js
2 years ago
group_controller.js
2 years ago
import_reports_controller.js
2 years ago
map_controller.js
1 year 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
1 year 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
2 years ago
pie_chart_controller.js
97 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 | |
| 6 | Chart.register(...registerables) |
| 7 | |
| 8 | export default class extends Controller { |
| 9 | static targets = ['canvas'] |
| 10 | |
| 11 | static values = { |
| 12 | data: Array, |
| 13 | locale: String, |
| 14 | } |
| 15 | |
| 16 | connect() { |
| 17 | this.renderChart() |
| 18 | } |
| 19 | |
| 20 | renderChart() { |
| 21 | const labels = this.dataValue.map((item) => item.label) |
| 22 | const values = this.dataValue.map((item) => item.value) |
| 23 | |
| 24 | const data = { |
| 25 | labels: labels, |
| 26 | datasets: [{ |
| 27 | data: values, |
| 28 | backgroundColor: [ |
| 29 | '#7B5BB3', |
| 30 | '#7FBAFD', |
| 31 | '#8ADBB0', |
| 32 | '#FD799E', |
| 33 | '#FFEA9C', |
| 34 | ], |
| 35 | hoverOffset: 4 |
| 36 | }] |
| 37 | } |
| 38 | |
| 39 | const config = { |
| 40 | type: 'pie', |
| 41 | options: { |
| 42 | responsive: true, |
| 43 | maintainAspectRatio: false, |
| 44 | borderColor: this.shouldUseDarkMode() ? '#363040' : '#ffffff' , |
| 45 | plugins: { |
| 46 | legend: { |
| 47 | position: 'left', |
| 48 | labels: { |
| 49 | color: this.shouldUseDarkMode() ? '#ffffff' : '#6D6A73', |
| 50 | } |
| 51 | }, |
| 52 | tooltip: { |
| 53 | callbacks: { |
| 54 | title: (tooltips) => { |
| 55 | const tooltip = tooltips[0] |
| 56 | const label = tooltip.label || '' |
| 57 | |
| 58 | return label |
| 59 | }, |
| 60 | label: (tooltip) => { |
| 61 | const chart = tooltip.chart |
| 62 | const data = this.dataValue[tooltip.dataIndex] |
| 63 | const total = this.dataValue.reduce((total, dataset, index) => { |
| 64 | if(!chart.getDataVisibility(index)) { |
| 65 | return total; |
| 66 | } |
| 67 | |
| 68 | return total + dataset.value |
| 69 | }, 0) |
| 70 | |
| 71 | return ` ${data.formatted_value ?? data.value} ${data.unit} (${this.formatPercent(data.value, total)})` |
| 72 | }, |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | }, |
| 77 | data: data, |
| 78 | } |
| 79 | |
| 80 | if(!this.chart) { |
| 81 | this.chart = new Chart(this.canvasTarget, config) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | shouldUseDarkMode() { |
| 86 | return document.body.classList.contains('iawp-dark-mode') |
| 87 | } |
| 88 | |
| 89 | formatPercent(value, total) { |
| 90 | const number = value > 0 ? value / total : 0 |
| 91 | const asPercent = new Intl.NumberFormat(this.localeValue, { |
| 92 | style: "percent", |
| 93 | }) |
| 94 | |
| 95 | return asPercent.format(number) |
| 96 | } |
| 97 | } |