Skip to content

The Open Standard for Generative UI

Refactor PieChart and RadialChart components to utilize color palette directly (#243)

subham4cf8c48de2ea
diff --git a/js/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx b/js/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx
index 8c30372f2..a63ac092c 100644
--- a/js/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx
+++ b/js/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx
@@ -139,6 +139,13 @@ const PieChartComponent = <T extends PieChartData>({
[],
);
+ const colors = useChartPalette({
+ chartThemeName: theme,
+ customPalette,
+ themePaletteName: "pieChartPalette",
+ dataLength: sortedProcessedData.length,
+ });
+
// Memoize expensive data transformations and configurations
const transformedData = useMemo(
() => transformDataWithPercentages(sortedProcessedData as T, dataKey),
@@ -146,8 +153,8 @@ const PieChartComponent = <T extends PieChartData>({
);
const chartConfig = useMemo(
- () => getCategoricalChartConfig(sortedProcessedData as T, categoryKey, theme, transformedKeys),
- [sortedProcessedData, categoryKey, theme, transformedKeys],
+ () => getCategoricalChartConfig(sortedProcessedData as T, categoryKey, colors, transformedKeys),
+ [sortedProcessedData, categoryKey, colors, transformedKeys],
);
const animationConfig = useMemo(
@@ -179,13 +186,6 @@ const PieChartComponent = <T extends PieChartData>({
};
}, [cornerRadius, variant, paddingAngle, userTheme.rounded2xs]);
- const colors = useChartPalette({
- chartThemeName: theme,
- customPalette,
- themePaletteName: "pieChartPalette",
- dataLength: sortedProcessedData.length,
- });
-
const legendItems = useMemo(
() =>
sortedProcessedData.map((item, index) => ({
diff --git a/js/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx b/js/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx
index b7b0114f0..c12ec93d2 100644
--- a/js/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx
+++ b/js/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx
@@ -142,15 +142,23 @@ export const RadialChart = <T extends RadialChartData>({
// Calculate chart radii
const dimensions = useMemo(() => calculateRadialChartDimensions(chartSize), [chartSize]);
+ // Get color palette and distribute colors
+ const colors = useChartPalette({
+ chartThemeName: theme,
+ customPalette,
+ themePaletteName: "radialChartPalette",
+ dataLength: sortedProcessedData.length,
+ });
+
// Memoize expensive data transformations and configurations
const transformedData = useMemo(
- () => transformRadialDataWithPercentages(sortedProcessedData as T, dataKey, theme),
- [sortedProcessedData, dataKey, theme],
+ () => transformRadialDataWithPercentages(sortedProcessedData as T, dataKey, colors),
+ [sortedProcessedData, dataKey, colors],
);
const chartConfig = useMemo(
- () => getCategoricalChartConfig(sortedProcessedData as T, categoryKey, theme, transformedKeys),
- [sortedProcessedData, categoryKey, theme, transformedKeys],
+ () => getCategoricalChartConfig(sortedProcessedData as T, categoryKey, colors, transformedKeys),
+ [sortedProcessedData, categoryKey, colors, transformedKeys],
);
const animationConfig = useMemo(
@@ -163,14 +171,6 @@ export const RadialChart = <T extends RadialChartData>({
[onMouseEnter, onMouseLeave, onClick],
);
- // Get color palette and distribute colors
- const colors = useChartPalette({
- chartThemeName: theme,
- customPalette,
- themePaletteName: "radialChartPalette",
- dataLength: sortedProcessedData.length,
- });
-
// Create legend items for both variants
const legendItems = useMemo(
() =>
diff --git a/js/packages/react-ui/src/components/Charts/RadialChart/utils/RadialChartUtils.ts b/js/packages/react-ui/src/components/Charts/RadialChart/utils/RadialChartUtils.ts
index 19236297f..0d601baeb 100644
--- a/js/packages/react-ui/src/components/Charts/RadialChart/utils/RadialChartUtils.ts
+++ b/js/packages/react-ui/src/components/Charts/RadialChart/utils/RadialChartUtils.ts
@@ -2,7 +2,6 @@
* Utility functions for radial charts
*/
import { useState } from "react";
-import { getDistributedColors, getPalette } from "../../utils/PalletUtils";
import { RadialChartData } from "../types";
// ==========================================
@@ -107,11 +106,9 @@ export const getRadialHoverStyles = (
export const transformRadialDataWithPercentages = <T extends RadialChartData>(
data: T,
dataKey: keyof T[number],
- theme: string = "ocean",
+ colors: string[], // Change this from 'theme: string' to 'colors: string[]'
) => {
const total = data.reduce((sum, item) => sum + Number(item[dataKey]), 0);
- const palette = getPalette(theme);
- const colors = getDistributedColors(palette.colors, data.length);
return data.map((item, index) => ({
...item,
diff --git a/js/packages/react-ui/src/components/Charts/utils/dataUtils.ts b/js/packages/react-ui/src/components/Charts/utils/dataUtils.ts
index f704a1df2..fe0755e86 100644
--- a/js/packages/react-ui/src/components/Charts/utils/dataUtils.ts
+++ b/js/packages/react-ui/src/components/Charts/utils/dataUtils.ts
@@ -2,7 +2,6 @@ import { ChartConfig } from "../Charts";
import { PieChartData } from "../PieChart";
import { RadialChartData } from "../RadialChart";
import { LegendItem } from "../types";
-import { getDistributedColors, getPalette } from "../utils/PalletUtils";
/**
* This function returns the data keys for the chart, used for the data keys of the chart.
@@ -53,12 +52,9 @@ type CategoricalChartData = RadialChartData | PieChartData;
export const getCategoricalChartConfig = <T extends CategoricalChartData>(
data: T,
categoryKey: keyof T[number],
- theme: string = "ocean",
+ colors: string[], // Change this from 'theme: string' to 'colors: string[]'
transformedKeys: Record<string, string>,
): ChartConfig => {
- const palette = getPalette(theme);
- const colors = getDistributedColors(palette.colors, data.length);
-
return data.reduce<ChartConfig>((config, item, index) => {
const originalKey = String(item[categoryKey]);
const transformedKey = `key-${transformedKeys[originalKey] ?? originalKey}`;