Skip to content

Commit

Permalink
Register all registerables (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
barbara-chaves authored Jan 9, 2024
1 parent 2a859e5 commit 7a73dca
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions client/src/components/chart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@ import { Chart } from 'react-chartjs-2';

import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
BarElement,
Title,
Tooltip,
Legend,
ChartData,
ChartType,
ChartTypeRegistry,
Point,
BubbleDataPoint,
Filler,
registerables,
} from 'chart.js';
import { ChartJSOrUndefined } from 'react-chartjs-2/dist/types';

Expand All @@ -40,17 +33,7 @@ const chartTypes = [
'radar',
] as const;

ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
BarElement,
Title,
Tooltip,
Legend,
Filler
);
ChartJS.register(...registerables, Filler);

ChartJS.defaults.font.size = 10;

Expand All @@ -62,24 +45,24 @@ const ChartJs = ({ widget, ...props }: ChartJsProps) => {
unknown
> | null>(null);

const dataWithDefaults = useMemo(() => getChartDefaultData(data, chartRef), [data]);
const dataWithDefaults = useMemo(
() => getChartDefaultData(data, chartRef),
[data, chartRef.current]
);
const optionsWithDefaults = useMemo(() => getChartDefaultOptions(options), [options]);

const chartType = type as ChartType;

// If the type is incorrect of there is no dataset return null
if (!chartTypes.includes(chartType) || !data || !(data as ChartData).datasets) {
return null;
}

return (
<div {...props}>
<Chart
ref={chartRef}
type={chartType}
options={optionsWithDefaults}
data={dataWithDefaults}
/>
{!chartTypes.includes(chartType) || !data || !(data as ChartData).datasets ? null : (
<Chart
ref={chartRef}
type={chartType}
options={optionsWithDefaults}
data={dataWithDefaults}
/>
)}
</div>
);
};
Expand Down

0 comments on commit 7a73dca

Please sign in to comment.