The Open Standard for Generative UI
Refactor scatter chart (#216)
subham1414015373b6
diff --git a/js/packages/react-ui/src/components/Charts/HorizontalBarChart/HorizontalBarChart.tsx b/js/packages/react-ui/src/components/Charts/HorizontalBarChart/HorizontalBarChart.tsxindex 8b4ab2365..e4366eb4d 100644--- a/js/packages/react-ui/src/components/Charts/HorizontalBarChart/HorizontalBarChart.tsx+++ b/js/packages/react-ui/src/components/Charts/HorizontalBarChart/HorizontalBarChart.tsx@@ -11,6 +11,7 @@ import {CustomTooltipContent,DefaultLegend,SideBarTooltip,+ SimpleXAxisTick,verticalCartesianGrid,YAxisTick,} from "../shared";@@ -274,7 +275,7 @@ const HorizontalBarChartComponent = <T extends HorizontalBarChartData>({tickLine={false}axisLine={false}tickFormatter={numberTickFormatter}- tick={{ fontSize: 12 }}+ tick={<SimpleXAxisTick />}/>{/* Invisible bars to maintain scale synchronization */}{dataKeys.map((key) => {diff --git a/js/packages/react-ui/src/components/Charts/ScatterChart/ScatterChart.tsx b/js/packages/react-ui/src/components/Charts/ScatterChart/ScatterChart.tsxindex fc717bc2f..f4a80d252 100644--- a/js/packages/react-ui/src/components/Charts/ScatterChart/ScatterChart.tsx+++ b/js/packages/react-ui/src/components/Charts/ScatterChart/ScatterChart.tsx@@ -10,6 +10,7 @@ import {DefaultLegend,gridCartesianGrid,SideBarTooltip,+ SimpleXAxisTick,YAxisTick,} from "../shared";import { LegendItem } from "../types";@@ -42,9 +43,7 @@ export interface ScatterChartProps {}const DEFAULT_CHART_HEIGHT = 296;-const DEFAULT_MARGIN = 20;const X_AXIS_HEIGHT = 40;-const Y_AXIS_OFFSET = 10;export const ScatterChart = ({data,@@ -102,11 +101,12 @@ export const ScatterChart = ({values: [],});- const isFixedNumericHeight = useMemo(() => {- if (typeof height === "number") return true;- if (typeof height === "string" && height.endsWith("px")) return true;- return false;- }, [height]);+ const chartWidth = useMemo(() => {+ if (!containerWidth) {+ return undefined;+ }+ return containerWidth - yAxisWidth;+ }, [containerWidth, yAxisWidth]);const chartHeight = useMemo(() => {if (!chartWrapperRef.current) {@@ -119,9 +119,26 @@ export const ScatterChart = ({return height - legendHeight - xAxisHeight;}+ if (typeof height === "string" && height.endsWith("px")) {+ const numericHeight = parseInt(height, 10);+ if (!isNaN(numericHeight)) {+ return numericHeight - legendHeight - xAxisHeight;+ }+ }++ if (!height) {+ return DEFAULT_CHART_HEIGHT;+ }+return chartWrapperRef.current.offsetHeight - legendHeight - xAxisHeight;}, [containerWidth, height]);+ const isFixedNumericHeight = useMemo(() => {+ if (typeof height === "number") return true;+ if (typeof height === "string" && height.endsWith("px")) return true;+ return false;+ }, [height]);+// Calculate domains for x and y axesconst xDomain = useMemo(() => {return calculateScatterDomain(data, xAxisDataKey as "x" | "y");@@ -172,73 +189,103 @@ export const ScatterChart = ({const xAxis = useMemo(() => {return (- <div className="crayon-scatter-chart-x-axis-container-outer" ref={xAxisContainerRef}>- <div- className="crayon-scatter-chart-x-axis-container"- style={- containerWidth- ? {- width: containerWidth - yAxisWidth - Y_AXIS_OFFSET,- marginLeft: yAxisWidth + Y_AXIS_OFFSET,- }- : {- width: "90%",- }- }+ <div className="crayon-scatter-chart-x-axis-container" ref={xAxisContainerRef}>+ <ChartContainer+ config={chartConfig}+ style={{ width: "100%", height: X_AXIS_HEIGHT }}+ rechartsProps={{+ height: X_AXIS_HEIGHT,+ }}>- {/* X-axis only chart - synchronized with main chart */}- <ChartContainer- config={chartConfig}- style={{ width: "100%", height: X_AXIS_HEIGHT }}- rechartsProps={{- height: X_AXIS_HEIGHT,+ <RechartsScatterChart+ key={`x-axis-scatter-chart-${id}`}+ data={transformedData}+ margin={{+ top: 10,+ bottom: 0,+ left: yAxisWidth,+ right: 0,}}>- <RechartsScatterChart- key={`x-axis-scatter-chart-${id}`}+ <XAxis+ type="number"+ height={X_AXIS_HEIGHT}+ name={xAxisLabel as string}+ tickLine={false}+ axisLine={false}+ tickFormatter={numberTickFormatter}+ tick={<SimpleXAxisTick />}+ domain={xDomain}+ dataKey={xAxisDataKey}+ />+ {/* Invisible scatter to maintain scale synchronization */}+ <Scatterdata={transformedData}- margin={{- top: 10,- bottom: 0,- left: DEFAULT_MARGIN - 3,- right: 2,- }}- >- <XAxis- type="number"- height={X_AXIS_HEIGHT}- name={xAxisLabel as string}- tickLine={false}- axisLine={false}- tickFormatter={numberTickFormatter}- tick={{ fontSize: 12 }}- domain={xDomain}- dataKey={xAxisDataKey}- />- {/* Invisible scatter to maintain scale synchronization */}- <Scatter- data={transformedData}- fill="transparent"- isAnimationActive={isAnimationActive}- shape="circle"- />- </RechartsScatterChart>- </ChartContainer>- </div>+ fill="transparent"+ isAnimationActive={isAnimationActive}+ shape="circle"+ />+ </RechartsScatterChart>+ </ChartContainer></div>);}, [chartConfig,transformedData,id,- yAxisWidth,xDomain,xAxisDataKey,isAnimationActive,- containerWidth,xAxisLabel,+ yAxisWidth,+ ]);+ const yAxis = useMemo(() => {+ return (+ <div className="crayon-scatter-chart-y-axis-container" style={{ height: chartHeight + 20 }}>+ <RechartsScatterChart+ key={`y-axis-scatter-chart-${id}`}+ width={yAxisWidth}+ height={chartHeight + 10}+ data={transformedData}+ margin={{+ top: 15,+ bottom: 12,+ left: 0,+ right: 0,+ }}+ >+ <YAxis+ type="number"+ width={yAxisWidth}+ dataKey={yAxisDataKey}+ name={yAxisLabel as string}+ domain={yDomain}+ tickLine={false}+ axisLine={false}+ tick={<YAxisTick setLabelWidth={setLabelWidth} />}+ tickFormatter={numberTickFormatter}+ />+ {/* Invisible scatter to maintain scale synchronization */}+ <Scatter+ data={transformedData}+ fill="transparent"+ isAnimationActive={isAnimationActive}+ shape="circle"+ />+ </RechartsScatterChart>+ </div>+ );+ }, [+ transformedData,+ id,+ yAxisWidth,+ chartHeight,+ yDomain,+ yAxisDataKey,+ yAxisLabel,+ isAnimationActive,+ setLabelWidth,]);-return (<SideBarTooltipProviderisSideBarTooltipOpen={isSideBarTooltipOpen}@@ -255,78 +302,84 @@ export const ScatterChart = ({ref={chartWrapperRef}><div className="crayon-scatter-chart-container-inner">+ {yAxis}<div- className="crayon-scatter-chart-main-container"- style={{- width: "100%",- height: typeof height === "number" ? `${height}px` : height || DEFAULT_CHART_HEIGHT,- }}+ className="crayon-scatter-chart-main-and-x-axis-container"+ style={{ width: chartWidth }}>- <ChartContainer- config={chartConfig}+ <div+ className="crayon-scatter-chart-main-container"style={{width: "100%",- height: "100%",- aspectRatio: 0,- }}- rechartsProps={{- width: "100%",- height: "100%",+ height: chartHeight,}}>- <RechartsScatterChart- key={`scatter-chart-${id}`}- margin={{- bottom: 10,+ <ChartContainer+ config={chartConfig}+ style={{+ width: "100%",+ height: "100%",+ aspectRatio: 0,+ }}+ rechartsProps={{+ width: "100%",+ height: "100%",}}>- {grid && gridCartesianGrid({ horizontal: true, vertical: true })}- <XAxis- type="number"- height={X_AXIS_HEIGHT}- name={xAxisLabel as string}- tickLine={false}- axisLine={false}- tickFormatter={numberTickFormatter}- tick={{ fontSize: 12 }}- domain={xDomain}- dataKey={xAxisDataKey}- hide- />+ <RechartsScatterChart+ key={`scatter-chart-${id}`}+ margin={{+ top: 10,+ right: 2,+ bottom: 2,+ left: 2,+ }}+ >+ {grid && gridCartesianGrid({ horizontal: true, vertical: true })}+ <XAxis+ type="number"+ name={xAxisLabel as string}+ domain={xDomain}+ dataKey={xAxisDataKey}+ hide+ />- <YAxis- type="number"- dataKey={yAxisDataKey}- name={yAxisLabel as string}- domain={yDomain}- tickLine={false}- axisLine={false}- tick={<YAxisTick setLabelWidth={setLabelWidth} />}- tickFormatter={numberTickFormatter}- />+ <YAxis+ type="number"+ dataKey={yAxisDataKey}+ name={yAxisLabel as string}+ domain={yDomain}+ hide+ />- <ChartTooltip- content={<CustomTooltipContent parentRef={chartWrapperRef} hideIndicator />}- offset={15}- />+ <ChartTooltip+ content={+ <CustomTooltipContent+ parentRef={chartWrapperRef}+ hideIndicator+ labelKey="dataset"+ />+ }+ offset={15}+ />- <Scatter- key={`scatter-${id}`}- data={transformedData}- shape={renderDotShape}- isAnimationActive={isAnimationActive}- >- {transformedData.map((entry, index) => (- <Cell key={`cell-${index}`} fill={entry["color"] as string} />- ))}- </Scatter>- </RechartsScatterChart>- </ChartContainer>+ <Scatter+ key={`scatter-${id}`}+ data={transformedData}+ shape={renderDotShape}+ isAnimationActive={isAnimationActive}+ >+ {transformedData.map((entry, index) => (+ <Cell key={`cell-${index}`} fill={entry["color"] as string} />+ ))}+ </Scatter>+ </RechartsScatterChart>+ </ChartContainer>+ </div></div>-+ {xAxis}{isSideBarTooltipOpen && chartHeight > 0 && <SideBarTooltip height={chartHeight} />}</div>- {xAxis}<div className="crayon-scatter-chart-legend-container" ref={legendContainerRef}>{legend && (<DefaultLegenddiff --git a/js/packages/react-ui/src/components/Charts/ScatterChart/scatterChart.scss b/js/packages/react-ui/src/components/Charts/ScatterChart/scatterChart.scssindex a1df6173e..1ce2c6dac 100644--- a/js/packages/react-ui/src/components/Charts/ScatterChart/scatterChart.scss+++ b/js/packages/react-ui/src/components/Charts/ScatterChart/scatterChart.scss@@ -14,33 +14,29 @@flex: 1 1 auto;min-width: 0;}-.crayon-scatter-chart-x-axis-container-outer {- width: 100%;- display: flex;- justify-content: flex-end;- border-bottom: 1px solid cssUtils.$stroke-default;- box-sizing: border-box;-}.crayon-scatter-chart-x-axis-container {+ position: absolute;+ bottom: 0;+ left: 0;flex-shrink: 0;width: 100%;- padding-top: cssUtils.$spacing-2xs;}.crayon-scatter-chart-y-axis-container {flex-shrink: 0;}-.crayon-scatter-chart-main-container {+.crayon-scatter-chart-main-and-x-axis-container {+ display: flex;+ flex-direction: column;width: 100%;- overflow-x: auto;- /* Hide scrollbar for Chrome, Safari and Opera */- &::-webkit-scrollbar {- display: none;- }+}- /* Hide scrollbar for Firefox */- scrollbar-width: none;+.crayon-scatter-chart-main-container {+ width: 100%;+ overflow: hidden;+ margin-bottom: 45px;+}- /* Hide scrollbar for IE and Edge */- -ms-overflow-style: none;+.crayon-scatter-chart-legend-container {+ border-top: 1px solid cssUtils.$stroke-default;}diff --git a/js/packages/react-ui/src/components/Charts/ScatterChart/stories/ScatterChart.stories.tsx b/js/packages/react-ui/src/components/Charts/ScatterChart/stories/ScatterChart.stories.tsxindex 364a1b179..3e9894a80 100644--- a/js/packages/react-ui/src/components/Charts/ScatterChart/stories/ScatterChart.stories.tsx+++ b/js/packages/react-ui/src/components/Charts/ScatterChart/stories/ScatterChart.stories.tsx@@ -713,8 +713,8 @@ export const ShapeComparison: Story = {>{shape} Shape</h4>- <Card style={{ width: "500px" }}>- <ScatterChart {...args} shape={shape} />+ <Card style={{ width: "500px", height: "600px" }}>+ <ScatterChart {...args} shape={shape} height={"100%"} /></Card></div>))}@@ -812,7 +812,7 @@ export const DataExplorer: Story = {</div></div><Card style={{ width: "600px" }}>- <ScatterChart {...args} data={currentData} height="600px" />+ <ScatterChart {...args} data={currentData} height={"600px"} /></Card></div>);diff --git a/js/packages/react-ui/src/components/Charts/charts.scss b/js/packages/react-ui/src/components/Charts/charts.scssindex 08364d4e4..38404c71a 100644--- a/js/packages/react-ui/src/components/Charts/charts.scss+++ b/js/packages/react-ui/src/components/Charts/charts.scss@@ -37,6 +37,7 @@@forward "./shared/ScrollButtonsHorizontal/scrollButtonsHorizontal.scss";@forward "./shared/ScrollButtonsVertical/scrollButtonsVertical.scss";@forward "./shared/LabelTooltip/labelTooltip.scss";+@forward "./shared/SimpleXAxisTick/simpleXAxisTick.scss";// portal tooltip css@forward "./shared/PortalTooltip/portalTooltip.scss";diff --git a/js/packages/react-ui/src/components/Charts/shared/SimpleXAxisTick/SimpleXAxisTick.tsx b/js/packages/react-ui/src/components/Charts/shared/SimpleXAxisTick/SimpleXAxisTick.tsxnew file mode 100644index 000000000..8b96ff136--- /dev/null+++ b/js/packages/react-ui/src/components/Charts/shared/SimpleXAxisTick/SimpleXAxisTick.tsx@@ -0,0 +1,27 @@+import { numberTickFormatter } from "../../utils";+interface SimpleXAxisTickProps {+ x?: number;+ y?: number;+ payload?: {+ value: string | number;+ };+}++export const SimpleXAxisTick = ({ x, y, payload }: SimpleXAxisTickProps) => {+ if (x === undefined || y === undefined || !payload) {+ return null;+ }++ const displayValue =+ typeof payload?.value === "number"+ ? numberTickFormatter(payload?.value)+ : String(payload?.value);++ return (+ <g transform={`translate(${x},${y})`}>+ <text x={0} y={0} dy={16} textAnchor="middle" className="crayon-chart-numeric-x-axis-tick">+ {displayValue}+ </text>+ </g>+ );+};diff --git a/js/packages/react-ui/src/components/Charts/shared/SimpleXAxisTick/simpleXAxisTick.scss b/js/packages/react-ui/src/components/Charts/shared/SimpleXAxisTick/simpleXAxisTick.scssnew file mode 100644index 000000000..18ef3e997--- /dev/null+++ b/js/packages/react-ui/src/components/Charts/shared/SimpleXAxisTick/simpleXAxisTick.scss@@ -0,0 +1,5 @@+@use "../../../../cssUtils" as cssUtils;+.crayon-chart-numeric-x-axis-tick {+ @include cssUtils.typography(label, extra-small);+ fill: cssUtils.$secondary-text;+}diff --git a/js/packages/react-ui/src/components/Charts/shared/index.ts b/js/packages/react-ui/src/components/Charts/shared/index.tsindex e3c26acb4..c3bcda086 100644--- a/js/packages/react-ui/src/components/Charts/shared/index.ts+++ b/js/packages/react-ui/src/components/Charts/shared/index.ts@@ -7,6 +7,7 @@ export * from "./PortalTooltip";export * from "./ScrollButtonsHorizontal";export * from "./ScrollButtonsVertical";export * from "./SideBarTooltip";+export * from "./SimpleXAxisTick/SimpleXAxisTick";export * from "./StackedLegend/StackedLegend";export * from "./XAxisTick/XAxisTick";export * from "./YAxisTick/YAxisTick";