Doughnut chart widget
ui/src/components/adminDashboard/widgets/DoughnutChartWidget
DoughnutChartWidget is a side widget which is displaying amount of leads for each choosen business.
Data set and fetch perform the same as on other widgets.
For correct display of label inside a chart we should add a custom plugin which will draw a text exactly at the DoughtnutChart center:
const doughnutLabel = {
id: 'dounghnutLabel',
afterDatasetsDraw(chart, args, plugins) {
const { ctx } = chart;
const centerX = chart.getDatasetMeta(0).data[0].x;
const centerY = chart.getDatasetMeta(0).data[0].y;
ctx.save();
ctx.font = `500 ${fontSize.lg} Inter`;
ctx.fillStyle = primitiveColors.gray900;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(title, centerX, centerY);
},
};
Also for correct display of labels exactly on chart we will use custom plugin ChartDataLabels from chartjs-plugin-datalabels library. Then we write plugin to chart options and add dataLabels styles:
Backend
On backend we have a function getLeadsPerBusinesses that return us required dataset:
Here is used query that will count leads for each business based on their createdAt column. The result of query will be an array of following objects:
{ businessName: 'Retail', leadCount: '126' }
Because of that here used a loop which will combine data in acceptable way for DoughnutChart.
Last updated