import { useEffect, useRef } from "react"; import Chart, { ChartConfiguration } from "chart.js/auto"; import { applyChartThemeColors, attachChartData, cloneChartOptions, useChartThemeMode, } from "../chart-utils"; import "./rds-comp-chart-scatter.scss"; import { resolveScatterChartType, type ScatterChartType } from "./rds-comp-chart-scatter-utils"; export type { ScatterChartType } from "./rds-comp-chart-scatter-utils"; function resolveDatasetColor( color: unknown, fallback: string ): string { if (Array.isArray(color)) { return (color[0] as string) ?? fallback; } return typeof color === "string" ? color : fallback; } function normalizeScatterDataset( dataset: any, chartType: ScatterChartType ) { const normalized = { ...dataset, type: chartType, }; if (chartType !== "line") { return normalized; } const lineColor = resolveDatasetColor( dataset.borderColor ?? dataset.backgroundColor, "rgb(255, 99, 132)" ); return { ...normalized, showLine: true, fill: false, borderColor: lineColor, borderWidth: dataset.borderWidth ?? 2, pointBackgroundColor: resolveDatasetColor( dataset.pointBackgroundColor ?? dataset.backgroundColor, lineColor ), pointBorderColor: resolveDatasetColor( dataset.pointBorderColor ?? dataset.backgroundColor, lineColor ), }; } export interface RdsCompScatterChartProps { labels: any[]; options: ChartConfiguration["options"]; dataSets: ChartConfiguration["data"]["datasets"]; id: string; chartLabel?: string; chartType?: ScatterChartType; } const RdsCompScatterChart = ({ id, labels, options, dataSets, chartLabel, chartType, }: RdsCompScatterChartProps) => { const chartRef = useRef(null); const canvasRef = useRef(null); const themeMode = useChartThemeMode(); useEffect(() => { chartRef.current?.destroy(); const ctx = canvasRef.current?.getContext("2d"); if (!ctx) return; const resolvedChartType = resolveScatterChartType(chartType); const chartOptions = cloneChartOptions(options); const chartData = { labels, datasets: dataSets.map((dataset) => normalizeScatterDataset(dataset, resolvedChartType) ), }; attachChartData(chartOptions ?? {}, chartData); applyChartThemeColors(chartOptions ?? {}); chartRef.current = new Chart(ctx, { type: resolvedChartType, data: chartData as any, options: chartOptions, }); return () => { chartRef.current?.destroy(); }; }, [id, labels, options, dataSets, themeMode, chartType]); return (
); }; RdsCompScatterChart.displayName = "RdsCompScatterChart"; export default RdsCompScatterChart;