2021-08-29 23:20:36 +03:00
|
|
|
import { FC } from 'react';
|
|
|
|
import { Chart } from 'chart.js';
|
2021-09-18 20:05:28 +03:00
|
|
|
import './DoughnutChartLegend.scss';
|
2021-08-29 23:20:36 +03:00
|
|
|
|
2021-09-18 20:05:28 +03:00
|
|
|
interface DoughnutChartLegendProps {
|
2021-09-18 13:59:54 +03:00
|
|
|
chart: Chart;
|
|
|
|
}
|
|
|
|
|
2021-09-18 20:05:28 +03:00
|
|
|
export const DoughnutChartLegend: FC<DoughnutChartLegendProps> = ({ chart }) => {
|
2021-08-29 23:20:36 +03:00
|
|
|
const { config } = chart;
|
|
|
|
const { labels = [], datasets = [] } = config.data ?? {};
|
|
|
|
const [{ backgroundColor: colors }] = datasets;
|
2021-09-18 13:59:54 +03:00
|
|
|
const { defaultColor } = config.options ?? {} as any;
|
2021-08-29 23:20:36 +03:00
|
|
|
|
|
|
|
return (
|
2021-09-18 20:05:28 +03:00
|
|
|
<ul className="doughnut-chart-legend">
|
2021-08-29 23:20:36 +03:00
|
|
|
{(labels as string[]).map((label, index) => (
|
2021-09-18 20:05:28 +03:00
|
|
|
<li key={label} className="doughnut-chart-legend__item d-flex">
|
2021-08-29 23:20:36 +03:00
|
|
|
<div
|
2021-09-18 20:05:28 +03:00
|
|
|
className="doughnut-chart-legend__item-color"
|
2021-09-18 13:59:54 +03:00
|
|
|
style={{ backgroundColor: (colors as string[])[index] ?? defaultColor }}
|
2021-08-29 23:20:36 +03:00
|
|
|
/>
|
2021-09-18 20:05:28 +03:00
|
|
|
<small className="doughnut-chart-legend__item-text flex-fill">{label}</small>
|
2021-08-29 23:20:36 +03:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|