|
|
@@ -4,6 +4,7 @@
|
|
|
|
|
|
export let data = {};
|
|
|
export let currency = 'USD';
|
|
|
+ export let total = 0;
|
|
|
|
|
|
let chartContainer;
|
|
|
let chartInstance;
|
|
|
@@ -15,7 +16,37 @@
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- Chart.register(DoughnutController, ArcElement, Tooltip, Legend, Title);
|
|
|
+ const centerTextPlugin = {
|
|
|
+ id: 'centerText',
|
|
|
+ beforeDraw(chart) {
|
|
|
+ const centerTextOptions = chart.options.plugins.centerText;
|
|
|
+ if (!centerTextOptions || centerTextOptions.displayTotal === undefined || !centerTextOptions.displayCurrency) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const displayTotal = centerTextOptions.displayTotal;
|
|
|
+ const displayCurrency = centerTextOptions.displayCurrency;
|
|
|
+
|
|
|
+ const { width, height, ctx } = chart;
|
|
|
+ ctx.save();
|
|
|
+
|
|
|
+ ctx.font = 'bold 20px sans-serif';
|
|
|
+
|
|
|
+ const probe = document.getElementById('chart-text-color');
|
|
|
+ const computedColor = getComputedStyle(probe)?.color;
|
|
|
+ ctx.fillStyle = computedColor || '#333';
|
|
|
+
|
|
|
+ ctx.textAlign = 'center';
|
|
|
+ ctx.textBaseline = 'middle';
|
|
|
+
|
|
|
+ const text = `${formatCurrency(displayTotal, displayCurrency)}`;
|
|
|
+ ctx.fillText(text, width / 2, height / 2);
|
|
|
+
|
|
|
+ ctx.restore();
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ Chart.register(DoughnutController, ArcElement, Tooltip, Legend, Title, centerTextPlugin);
|
|
|
|
|
|
const groupByMarket = (stocks) => {
|
|
|
const groups = {
|
|
|
@@ -81,6 +112,10 @@
|
|
|
return `${label}: ${formatCurrency(value, currency)}`;
|
|
|
}
|
|
|
}
|
|
|
+ },
|
|
|
+ centerText: {
|
|
|
+ displayTotal: total,
|
|
|
+ displayCurrency: currency
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
@@ -109,6 +144,7 @@
|
|
|
<p class="text-center font-semibold text-gray-700 dark:text-gray-300 mb-2 text-base">
|
|
|
Market Distribution
|
|
|
</p>
|
|
|
+ <span id="chart-text-color" class="hidden text-gray-900 dark:text-gray-100"></span>
|
|
|
<canvas bind:this={chartContainer} class="w-full h-full"></canvas>
|
|
|
{:else}
|
|
|
<p class="text-center text-gray-500 dark:text-gray-400">No data available.</p>
|