Skip to content

Commit

Permalink
hover on donut shows percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowrna committed Oct 31, 2024
1 parent 2c02e01 commit 9f0e1e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 16 additions & 1 deletion frontend/src/components/Chart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ const DEFAULT_DONUT = {
},
bodySpacing: 10,
padding: 10,
callbacks: {
label: (item) => {
const data = item.chart.data.datasets[item.datasetIndex];
const total = data.data.reduce((acc, val) => acc + val, 0);
const val = data.data[item.dataIndex];
const percentage = ((val / total) * 100).toFixed(2);
const MAX_LABEL_LENGTH = 20;
let displayLabel = item.label;
if (displayLabel.length > MAX_LABEL_LENGTH) {
displayLabel = `${displayLabel.slice(0, MAX_LABEL_LENGTH)}...`; // Truncate
return [`${displayLabel}:`, `${val} (${percentage}%)`];
}
return `${displayLabel}: ${val} (${percentage}%)`;
},
},
},
},
},
Expand Down Expand Up @@ -128,7 +143,7 @@ export default {
type: { type: String, default: 'line' },
onClick: { type: Function, default: () => { } },
},
mounted() {
const ctx = this.$el.querySelector('.chart-canvas');
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/views/CampaignAnalytics.vue
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ export default Vue.extend({
out[c.id] = c;
return out;
}, {});
const campIDs = Object.keys(camps);
// datasets[] array for line chart.
const lines = campIDs.map((id, n) => {
const cId = parseInt(id, 10);
Expand Down Expand Up @@ -242,7 +240,6 @@ export default Vue.extend({
data: points, backgroundColor: chartColors, borderWidth: 6,
}],
};
return { points: { datasets: lines }, donut };
},
Expand All @@ -269,7 +266,6 @@ export default Vue.extend({
getData(typ, camps) {
this.charts[typ].loading = true;
// Call the HTTP API.
this.charts[typ].fn({
id: camps.map((c) => c.id),
Expand Down

0 comments on commit 9f0e1e3

Please sign in to comment.