shlink-web-client/shlink-web-component/visits/charts/DoughnutChartLegend.tsx

29 lines
922 B
TypeScript
Raw Normal View History

2023-02-18 12:40:37 +03:00
import type { Chart } from 'chart.js';
2023-02-18 13:11:01 +03:00
import type { FC } from 'react';
import './DoughnutChartLegend.scss';
interface DoughnutChartLegendProps {
2021-09-18 13:59:54 +03:00
chart: Chart;
}
export const DoughnutChartLegend: FC<DoughnutChartLegendProps> = ({ chart }) => {
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;
return (
<ul className="doughnut-chart-legend">
{(labels as string[]).map((label, index) => (
<li key={label} className="doughnut-chart-legend__item d-flex">
<div
className="doughnut-chart-legend__item-color"
2021-09-18 13:59:54 +03:00
style={{ backgroundColor: (colors as string[])[index] ?? defaultColor }}
/>
<small className="doughnut-chart-legend__item-text flex-fill">{label}</small>
</li>
))}
</ul>
);
};