diff --git a/src/visits/helpers/DefaultChart.tsx b/src/visits/helpers/DefaultChart.tsx
index 7792aac7..33587161 100644
--- a/src/visits/helpers/DefaultChart.tsx
+++ b/src/visits/helpers/DefaultChart.tsx
@@ -1,8 +1,8 @@
-import { useRef } from 'react';
+import { useState } from 'react';
import { Doughnut, Bar } from 'react-chartjs-2';
import { keys, values } from 'ramda';
import classNames from 'classnames';
-import { Chart, ChartData, ChartDataset, ChartOptions, LegendItem } from 'chart.js';
+import { Chart, ChartData, ChartDataset, ChartOptions } from 'chart.js';
import { fillTheGaps } from '../../utils/helpers/visits';
import { Stats } from '../types';
import { prettify } from '../../utils/helpers/numbers';
@@ -16,7 +16,7 @@ import {
PRIMARY_DARK_COLOR,
PRIMARY_LIGHT_COLOR,
} from '../../utils/theme';
-import './DefaultChart.scss';
+import { PieChartLegend } from './PieChartLegend';
export interface DefaultChartProps {
title: Function | string;
@@ -79,32 +79,6 @@ const determineHeight = (isBarChart: boolean, labels: string[]): number | undefi
return isBarChart && labels.length > 20 ? labels.length * 8 : undefined;
};
-const renderPieChartLegend = ({ config }: Chart): LegendItem[] => {
- const { labels = [] /* , datasets = [] */ } = config.data ?? {};
- // const { defaultColor } = config.options ?? {} as any;
- // const [{ backgroundColor: colors }] = datasets;
-
- return labels.map((label, datasetIndex) => ({
- datasetIndex,
- text: label as string,
- }));
-
- // TODO
- // return (
- //
- // {labels.map((label, index) => (
- // -
- //
- // {label}
- //
- // ))}
- //
- // );
-};
-
const chartElementAtEvent = (onClick?: (label: string) => void) => ([ chart ]: [{ _index: number; _chart: Chart }]) => {
// TODO Check this function actually works with Chart.js 3
if (!onClick || !chart) {
@@ -123,7 +97,7 @@ const DefaultChart = (
{ title, isBarChart = false, stats, max, highlightedStats, highlightedLabel, onClick }: DefaultChartProps,
) => {
const Component = isBarChart ? Bar : Doughnut;
- const chartRef = useRef();
+ const [ chartRef, setChartRef ] = useState();
const labels = keys(stats).map(dropLabelIfHidden);
const data = values(
!statsAreDefined(highlightedStats) ? stats : keys(highlightedStats).reduce((acc, highlightedKey) => {
@@ -138,12 +112,7 @@ const DefaultChart = (
const options: ChartOptions = {
plugins: {
- legend: {
- display: false,
- labels: isBarChart ? undefined : {
- generateLabels: renderPieChartLegend,
- },
- },
+ legend: { display: false },
tooltip: {
intersect: !isBarChart,
// Do not show tooltip on items with empty label when in a bar chart
@@ -177,7 +146,7 @@ const DefaultChart = (
{
- chartRef.current = element ?? undefined;
+ setChartRef(element ?? undefined);
}}
key={height}
data={graphData}
@@ -188,8 +157,7 @@ const DefaultChart = (
{!isBarChart && (
- No Legend in v3.0 unfortunately :(
- {/* {chartRef?.chartInstance.generateLegend()} */}
+ {chartRef &&
}
)}
diff --git a/src/visits/helpers/DefaultChart.scss b/src/visits/helpers/PieChartLegend.scss
similarity index 100%
rename from src/visits/helpers/DefaultChart.scss
rename to src/visits/helpers/PieChartLegend.scss
diff --git a/src/visits/helpers/PieChartLegend.tsx b/src/visits/helpers/PieChartLegend.tsx
new file mode 100644
index 00000000..6b765068
--- /dev/null
+++ b/src/visits/helpers/PieChartLegend.tsx
@@ -0,0 +1,24 @@
+import { FC } from 'react';
+import { Chart } from 'chart.js';
+import './PieChartLegend.scss';
+
+export const PieChartLegend: FC<{ chart: Chart }> = ({ chart }) => {
+ const { config } = chart;
+ const { labels = [], datasets = [] } = config.data ?? {};
+ const { defaultColor } = config.options ?? {} as any;
+ const [{ backgroundColor: colors }] = datasets;
+
+ return (
+
+ {(labels as string[]).map((label, index) => (
+ -
+
+ {label}
+
+ ))}
+
+ );
+};