Skip to content

The Open Standard for Generative UI

Adding more funtionality like resize the pie and radial chart according to the Layout

i-subham7469171d99af
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx
index d200dcadc..69a84bc6a 100644
--- a/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx
@@ -1,4 +1,8 @@
+import clsx from "clsx";
+import { debounce } from "lodash-es";
+import { useEffect, useRef, useState } from "react";
import { Cell, Pie, PieChart as RechartsPieChart } from "recharts";
+import { useLayoutContext } from "../../../context/LayoutContext";
import {
ChartConfig,
ChartContainer,
@@ -43,11 +47,53 @@ export const PieChart = <T extends PieChartData>({
format = "number",
legend = true,
label = true,
- outerRadius = 120,
- innerRadius = variant === "donut" ? 60 : 0,
+ outerRadius,
+ innerRadius,
width = 800,
height = 400,
}: PieChartProps<T>) => {
+ const { layout } = useLayoutContext() || {};
+ const [calculatedOuterRadius, setCalculatedOuterRadius] = useState(120);
+ const [calculatedInnerRadius, setCalculatedInnerRadius] = useState(0);
+ const containerRef = useRef<HTMLDivElement>(null);
+
+ // Calculate dynamic radius based on layout
+ useEffect(() => {
+ if (!containerRef.current) return;
+
+ const resizeObserver = new ResizeObserver(
+ debounce((entries: any) => {
+ const { width } = entries[0].contentRect;
+
+ // Calculate outer radius
+ let newOuterRadius = 120; // default
+ if (layout === "mobile") {
+ newOuterRadius = label ? (width > 300 ? 85 : 75) : width > 300 ? 95 : 80;
+ } else if (layout === "fullscreen") {
+ newOuterRadius = 120;
+ } else if (layout === "tray" || layout === "copilot") {
+ newOuterRadius = 90;
+ }
+
+ // Calculate inner radius for donut
+ let newInnerRadius = 0;
+ if (variant === "donut") {
+ if (layout === "mobile") {
+ newInnerRadius = label ? (width > 300 ? 50 : 30) : width > 300 ? 60 : 50;
+ } else {
+ newInnerRadius = 60;
+ }
+ }
+
+ setCalculatedOuterRadius(outerRadius ?? newOuterRadius);
+ setCalculatedInnerRadius(innerRadius ?? newInnerRadius);
+ }, 100),
+ );
+
+ resizeObserver.observe(containerRef.current);
+ return () => resizeObserver.disconnect();
+ }, [layout, label, variant, outerRadius, innerRadius]);
+
// Calculate total for percentage calculations
const total = data.reduce((sum, item) => sum + Number(item[dataKey]), 0);
@@ -90,6 +136,7 @@ export const PieChart = <T extends PieChartData>({
y={y}
textAnchor={textAnchor}
dominantBaseline={dominantBaseline}
+ className="fill-t-comp-secondary p-0"
>
{formattedValue}
{format === "percentage" ? "%" : ""}
@@ -99,7 +146,11 @@ export const PieChart = <T extends PieChartData>({
};
return (
- <ChartContainer config={chartConfig} className="crayon-pie-chart-container">
+ <ChartContainer
+ ref={containerRef}
+ config={chartConfig}
+ className={clsx("crayon-pie-chart-container", `crayon-pie-chart-container-${layout}`)}
+ >
<RechartsPieChart width={width} height={height}>
<ChartTooltip content={<ChartTooltipContent showPercentage={format === "percentage"} />} />
{legend && <ChartLegend content={<ChartLegendContent nameKey={String(categoryKey)} />} />}
@@ -108,8 +159,8 @@ export const PieChart = <T extends PieChartData>({
dataKey={format === "percentage" ? "percentage" : String(dataKey)}
nameKey={String(categoryKey)}
labelLine={false}
- outerRadius={outerRadius}
- innerRadius={innerRadius}
+ outerRadius={calculatedOuterRadius}
+ innerRadius={calculatedInnerRadius}
label={label ? renderCustomLabel : false}
>
{Object.entries(chartConfig).map(([key, config]) => (
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/pieChart.scss b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/pieChart.scss
index 5f8686431..10b7560c0 100644
--- a/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/pieChart.scss
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/pieChart.scss
@@ -3,8 +3,27 @@
.crayon-pie-chart {
&-container {
- min-height: 400px;
margin-left: auto;
margin-right: auto;
+
+ &-fullscreen {
+ min-height: 400px;
+ width: 100%;
+ }
+
+ &-mobile {
+ min-height: 300px;
+ width: 100%;
+ }
+
+ &-tray {
+ min-height: 350px;
+ width: 100%;
+ }
+
+ &-copilot {
+ min-height: 350px;
+ width: 100%;
+ }
}
}
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx b/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx
index 3c919d0a0..f79adf3bd 100644
--- a/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx
@@ -1,4 +1,8 @@
+import clsx from "clsx";
+import { debounce } from "lodash-es";
+import { useEffect, useRef, useState } from "react";
import { Cell, LabelList, PolarGrid, RadialBar, RadialBarChart } from "recharts";
+import { useLayoutContext } from "../../../context/LayoutContext";
import {
ChartConfig,
ChartContainer,
@@ -27,7 +31,7 @@ interface RadialChartProps<T extends RadialChartData> {
height?: number;
}
-// Helper function to calculate percentage (same as PieChart)
+// Helper function to calculate percentage
const calculatePercentage = (value: number, total: number): number => {
if (total === 0) return 0;
return Number(((value / total) * 100).toFixed(2));
@@ -43,11 +47,54 @@ export const RadialChart = <T extends RadialChartData>({
legend = true,
label = true,
grid = true,
- outerRadius = 150,
- innerRadius = 40,
+ outerRadius,
+ innerRadius,
width = 800,
height = 400,
}: RadialChartProps<T>) => {
+ const { layout } = useLayoutContext() || {};
+ const [calculatedOuterRadius, setCalculatedOuterRadius] = useState(110);
+ const [calculatedInnerRadius, setCalculatedInnerRadius] = useState(30);
+ const containerRef = useRef<HTMLDivElement>(null);
+
+ useEffect(() => {
+ if (!containerRef.current) return;
+
+ const resizeObserver = new ResizeObserver(
+ debounce((entries: any) => {
+ const { width } = entries[0].contentRect;
+
+ // Calculate outer radius
+ let newOuterRadius = 110; // default
+ if (layout === "mobile") {
+ newOuterRadius = label ? (width > 300 ? 110 : 90) : width > 300 ? 95 : 80;
+ } else if (layout === "fullscreen") {
+ newOuterRadius = 200;
+ } else if (layout === "tray" || layout === "copilot") {
+ newOuterRadius = 130;
+ } else {
+ newOuterRadius = 100;
+ }
+
+ // Calculate inner radius
+ let newInnerRadius = 30; // default
+ if (layout === "mobile") {
+ newInnerRadius = 30;
+ } else if (layout === "fullscreen") {
+ newInnerRadius = 50;
+ } else {
+ newInnerRadius = 30;
+ }
+
+ setCalculatedOuterRadius(outerRadius ?? newOuterRadius);
+ setCalculatedInnerRadius(innerRadius ?? newInnerRadius);
+ }, 100),
+ );
+
+ resizeObserver.observe(containerRef.current);
+ return () => resizeObserver.disconnect();
+ }, [layout, label, outerRadius, innerRadius]);
+
// Calculate total for percentage calculations
const total = data.reduce((sum, item) => sum + Number(item[dataKey]), 0);
@@ -90,15 +137,23 @@ export const RadialChart = <T extends RadialChartData>({
};
return (
- <ChartContainer config={chartConfig} className="crayon-radial-chart-container">
+ <ChartContainer
+ ref={containerRef}
+ config={chartConfig}
+ className={clsx(
+ "crayon-radial-chart-container",
+ `crayon-radial-chart-container-${layout}`,
+ "aspect-square",
+ )}
+ >
<RadialBarChart
width={width}
height={height}
data={transformedData}
startAngle={variant === "circular" ? -90 : 0}
endAngle={variant === "circular" ? 270 : 180}
- innerRadius={innerRadius}
- outerRadius={outerRadius}
+ innerRadius={calculatedInnerRadius}
+ outerRadius={calculatedOuterRadius}
>
{grid && <PolarGrid gridType="circle" />}
<ChartTooltip
@@ -131,7 +186,7 @@ export const RadialChart = <T extends RadialChartData>({
position="insideStart"
offset={12}
fill="currentColor"
- className="capitalize"
+ className="capitalize mix-blend-luminosity"
fontSize={getFontSize(data.length)}
formatter={formatLabel}
/>