Skip to content

The Open Standard for Generative UI

Enhance SegmentedBarChart and DefaultLegend components with percentage display (#210)

Ankit Das4932f2923087
diff --git a/js/packages/react-ui/package.json b/js/packages/react-ui/package.json
index 423e5e823..dced55b9b 100644
--- a/js/packages/react-ui/package.json
+++ b/js/packages/react-ui/package.json
@@ -2,7 +2,7 @@
"type": "module",
"name": "@crayonai/react-ui",
"license": "MIT",
- "version": "0.8.37",
+ "version": "0.8.38",
"description": "Component library for Generative UI SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/index.ts b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/index.ts
deleted file mode 100644
index 305fbc61a..000000000
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export { SegmentedBar } from "./SegmentedBarChart";
-export type { SegmentedBarProps } from "./SegmentedBarChart";
-export type { SegmentedBarData } from "./types";
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/types/index.ts b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/types/index.ts
deleted file mode 100644
index 8c584efbb..000000000
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/types/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export type SegmentedBarData = Array<Record<string, string | number>>;
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/SegmentedBarChart.tsx b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/SingleStackedBarChart.tsx
similarity index 86%
rename from js/packages/react-ui/src/components/Charts/SegmentedBarChart/SegmentedBarChart.tsx
rename to js/packages/react-ui/src/components/Charts/SingleStackedBarChart/SingleStackedBarChart.tsx
index 4826108ba..408b22ba9 100644
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/SegmentedBarChart.tsx
+++ b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/SingleStackedBarChart.tsx
@@ -1,14 +1,15 @@
import clsx from "clsx";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
-import { SegmentedBarData } from ".";
+import { Separator } from "../../Separator";
import { DefaultLegend } from "../shared/DefaultLegend/DefaultLegend";
import { FloatingUIPortal } from "../shared/PortalTooltip";
import { StackedLegend } from "../shared/StackedLegend/StackedLegend";
import { LegendItem, StackedLegendItem } from "../types";
import { getDistributedColors, getPalette, PaletteName } from "../utils/PalletUtils";
import { ToolTip } from "./components";
+import { SingleStackedBarData } from "./types";
-export interface SegmentedBarProps<T extends SegmentedBarData> {
+export interface SingleStackedBarProps<T extends SingleStackedBarData> {
data: T;
categoryKey: keyof T[number];
dataKey: keyof T[number];
@@ -20,7 +21,7 @@ export interface SegmentedBarProps<T extends SegmentedBarData> {
animated?: boolean;
}
-export const SegmentedBar = <T extends SegmentedBarData>({
+export const SingleStackedBar = <T extends SingleStackedBarData>({
data,
categoryKey,
dataKey,
@@ -30,7 +31,7 @@ export const SegmentedBar = <T extends SegmentedBarData>({
className,
style,
animated = true,
-}: SegmentedBarProps<T>) => {
+}: SingleStackedBarProps<T>) => {
const [isLegendExpanded, setIsLegendExpanded] = useState(false);
const [activeIndex, setActiveIndex] = useState<number | null>(null);
const wrapperRef = useRef<HTMLDivElement>(null);
@@ -79,6 +80,7 @@ export const SegmentedBar = <T extends SegmentedBarData>({
key: `${segment.category}-${index}`,
label: segment.category,
color: colors[index % colors.length] || "",
+ percentage: segment.percentage,
}));
}, [segments, colors]);
@@ -100,7 +102,7 @@ export const SegmentedBar = <T extends SegmentedBarData>({
if (hoverIndex !== null) {
// Try to position tooltip above the hovered segment
const segmentEl = wrapperRef.current?.querySelectorAll(
- ".crayon-segmented-bar-chart-segment",
+ ".crayon-single-stacked-bar-chart-segment",
)?.[hoverIndex] as HTMLDivElement | undefined;
if (segmentEl) {
const rect = segmentEl.getBoundingClientRect();
@@ -122,17 +124,19 @@ export const SegmentedBar = <T extends SegmentedBarData>({
return (
<div
ref={wrapperRef}
- className={clsx("crayon-segmented-bar-chart-container", className)}
+ className={clsx("crayon-single-stacked-bar-chart-container", className, {
+ "crayon-single-stacked-bar-chart-container-gap": legend && legendVariant === "default",
+ })}
style={style}
>
- <div className="crayon-segmented-bar-chart">
+ <div className="crayon-single-stacked-bar-chart">
{segments.map((segment, index) => {
const isActive = activeIndex === null || activeIndex === index;
return (
<div
key={`segment-${index}`}
- className={clsx("crayon-segmented-bar-chart-segment", {
- "crayon-segmented-bar-chart-animated": animated,
+ className={clsx("crayon-single-stacked-bar-chart-segment", {
+ "crayon-single-stacked-bar-chart-animated": animated,
})}
style={{
width: `${segment.percentage}%`,
@@ -154,7 +158,7 @@ export const SegmentedBar = <T extends SegmentedBarData>({
}}
onMouseLeave={() => setActiveIndex(null)}
>
- <div className="crayon-segmented-bar-chart-segment-line" />
+ <div className="crayon-single-stacked-bar-chart-segment-line" />
</div>
);
})}
@@ -169,12 +173,16 @@ export const SegmentedBar = <T extends SegmentedBarData>({
/>
</FloatingUIPortal>
)}
+
+ {legend && legendVariant === "default" && <Separator />}
+
{legend && legendVariant === "default" && (
<DefaultLegend
items={legendItems}
isExpanded={isLegendExpanded}
setIsExpanded={setIsLegendExpanded}
containerWidth={containerWidth}
+ style={{ paddingTop: 0 }}
/>
)}
{legend && legendVariant === "stacked" && (
@@ -182,6 +190,8 @@ export const SegmentedBar = <T extends SegmentedBarData>({
items={stackedLegendItems}
containerWidth={containerWidth}
onLegendItemHover={handleLegendItemHover}
+ separator
+ showTitle={false}
/>
)}
</div>
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/ToolTip.tsx b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/components/ToolTip.tsx
similarity index 91%
rename from js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/ToolTip.tsx
rename to js/packages/react-ui/src/components/Charts/SingleStackedBarChart/components/ToolTip.tsx
index 8cbb31e65..fadaf97aa 100644
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/ToolTip.tsx
+++ b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/components/ToolTip.tsx
@@ -1,4 +1,5 @@
import React from "react";
+import { numberTickFormatter } from "../../utils";
interface ToolTipProps {
label: string;
@@ -24,7 +25,9 @@ export const ToolTip: React.FC<ToolTipProps> = ({ label, color, value, percentag
<div className="crayon-chart-tooltip-content-label">
<span>Value</span>
</div>
- <span className="crayon-chart-tooltip-content-value">{value}</span>
+ <span className="crayon-chart-tooltip-content-value">
+ {typeof value === "number" ? numberTickFormatter(value) : value}
+ </span>
</div>
</div>
<div className="crayon-chart-tooltip-content-item-separator" />
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/index.ts b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/components/index.ts
similarity index 100%
rename from js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/index.ts
rename to js/packages/react-ui/src/components/Charts/SingleStackedBarChart/components/index.ts
diff --git a/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/index.ts b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/index.ts
new file mode 100644
index 000000000..76025dbd6
--- /dev/null
+++ b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/index.ts
@@ -0,0 +1,3 @@
+export { SingleStackedBar } from "./SingleStackedBarChart";
+export type { SingleStackedBarProps } from "./SingleStackedBarChart";
+export type { SingleStackedBarData } from "./types";
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/segmentedBarChart.scss b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/singleStackedBarChart.scss
similarity index 85%
rename from js/packages/react-ui/src/components/Charts/SegmentedBarChart/segmentedBarChart.scss
rename to js/packages/react-ui/src/components/Charts/SingleStackedBarChart/singleStackedBarChart.scss
index 8b24abd0d..4a7af4bd4 100644
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/segmentedBarChart.scss
+++ b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/singleStackedBarChart.scss
@@ -1,15 +1,18 @@
@use "../../../cssUtils.scss" as cssUtils;
-.crayon-segmented-bar-chart-container {
+.crayon-single-stacked-bar-chart-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
- gap: cssUtils.$spacing-m;
position: relative;
+
+ &-gap {
+ gap: cssUtils.$spacing-m;
+ }
}
-.crayon-segmented-bar-chart {
+.crayon-single-stacked-bar-chart {
width: 100%;
height: 20px;
font-family: inherit;
@@ -22,7 +25,7 @@
gap: cssUtils.$spacing-3xs;
}
-.crayon-segmented-bar-chart-segment {
+.crayon-single-stacked-bar-chart-segment {
height: 100%;
display: flex;
align-items: center;
@@ -58,6 +61,6 @@
}
}
-.crayon-segmented-bar-chart-animated {
+.crayon-single-stacked-bar-chart-animated {
transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/stories/SegmentedBarChart.stories.tsx b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/stories/SingleStackedBarChart.stories.tsx
similarity index 85%
rename from js/packages/react-ui/src/components/Charts/SegmentedBarChart/stories/SegmentedBarChart.stories.tsx
rename to js/packages/react-ui/src/components/Charts/SingleStackedBarChart/stories/SingleStackedBarChart.stories.tsx
index 8460e90a6..53dcb45b6 100644
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/stories/SegmentedBarChart.stories.tsx
+++ b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/stories/SingleStackedBarChart.stories.tsx
@@ -1,24 +1,24 @@
import type { Meta, StoryObj } from "@storybook/react";
import { Card } from "../../../Card";
-import { SegmentedBar, SegmentedBarProps } from "../SegmentedBarChart";
+import { SingleStackedBar, SingleStackedBarProps } from "../SingleStackedBarChart";
-const meta: Meta<SegmentedBarProps<any>> = {
- title: "Components/Charts/SegmentedBar",
- component: SegmentedBar,
+const meta: Meta<SingleStackedBarProps<any>> = {
+ title: "Components/Charts/SingleStackedBar",
+ component: SingleStackedBar,
parameters: {
layout: "centered",
docs: {
description: {
component: `
-// Note: While the component is named 'ProgressBar', it's presented here as a 'SegmentedBar'
+// Note: While the component is named 'ProgressBar', it's presented here as a 'SingleStackedBar'
// to emphasize its use for showing proportional data, much like a pie chart.
## Installation and Basic Usage
\`\`\`tsx
-import { SegmentedBar } from '@crayon-ui/react-ui/Charts/SegmentedBar';
+import { SingleStackedBar } from '@crayon-ui/react-ui/Charts/SingleStackedBar';
// Basic implementation
-<SegmentedBar
+<SingleStackedBar
data={[
{ category: 'A', value: 25 },
{ category: 'B', value: 30 },
@@ -44,7 +44,7 @@ const data = [
];
// Component usage
-<SegmentedBar
+<SingleStackedBar
data={data}
categoryKey="category"
dataKey="value"
@@ -135,7 +135,7 @@ These values determine the proportional width of each segment.
},
legend: {
description: `
-**Legend Display.** Shows or hides the legend below the segmented bar chart.
+**Legend Display.** Shows or hides the legend below the single stacked bar chart.
The legend displays category names with their corresponding colors.
`,
control: "boolean",
@@ -159,7 +159,7 @@ Stacked legend appears at the bottom and shows values as percentages.
},
},
},
-} satisfies Meta<typeof SegmentedBar>;
+} satisfies Meta<typeof SingleStackedBar>;
export default meta;
type Story = StoryObj<typeof meta>;
@@ -207,14 +207,14 @@ export const DefaultConfiguration: Story = {
Visualizing the breakdown of a dataset into its constituent parts.
</p>
</div>
- <SegmentedBar {...args} />
+ <SingleStackedBar {...args} />
</Card>
),
parameters: {
docs: {
description: {
story:
- "This is the standard appearance of the Segmented Bar. It shows multiple segments, each with a color from the selected theme, representing the proportional breakdown of a whole.",
+ "This is the standard appearance of the Single Stacked Bar. It shows multiple segments, each with a color from the selected theme, representing the proportional breakdown of a whole.",
},
},
},
@@ -247,7 +247,7 @@ export const ThemeShowcase: Story = {
>
{theme}
</h4>
- <SegmentedBar {...args} theme={theme} />
+ <SingleStackedBar {...args} theme={theme} />
</div>
))}
</div>
@@ -256,7 +256,7 @@ export const ThemeShowcase: Story = {
docs: {
description: {
story:
- "The Segmented Bar supports six different color themes. This allows it to fit seamlessly into various application designs and visual identities.",
+ "The Single Stacked Bar supports six different color themes. This allows it to fit seamlessly into various application designs and visual identities.",
},
},
},
@@ -281,14 +281,14 @@ export const ManySegments: Story = {
The component gracefully handles numerous small segments.
</p>
</div>
- <SegmentedBar {...args} />
+ <SingleStackedBar {...args} />
</Card>
),
parameters: {
docs: {
description: {
story:
- "The Segmented Bar can display many segments. The colors will cycle through the selected theme palette if the number of segments exceeds the number of available colors.",
+ "The Single Stacked Bar can display many segments. The colors will cycle through the selected theme palette if the number of segments exceeds the number of available colors.",
},
},
},
@@ -313,7 +313,7 @@ export const FullComposition: Story = {
Displaying a bar where segments add up to 100%.
</p>
</div>
- <SegmentedBar {...args} />
+ <SingleStackedBar {...args} />
</Card>
),
parameters: {
@@ -348,14 +348,14 @@ export const WithLegend: Story = {
automatically expands when there are many items.
</p>
</div>
- <SegmentedBar {...args} />
+ <SingleStackedBar {...args} />
</Card>
),
parameters: {
docs: {
description: {
story:
- "This example demonstrates the SegmentedBar with legend enabled. The legend displays below the chart and shows the category names with their corresponding colors. For larger datasets, the legend will show a 'Show More' button to expand and see all items.",
+ "This example demonstrates the SingleStackedBar with legend enabled. The legend displays below the chart and shows the category names with their corresponding colors. For larger datasets, the legend will show a 'Show More' button to expand and see all items.",
},
},
},
@@ -383,14 +383,14 @@ export const LegendWithManyItems: Story = {
values. The legend automatically manages overflow with expand/collapse functionality.
</p>
</div>
- <SegmentedBar {...args} />
+ <SingleStackedBar {...args} />
</Card>
),
parameters: {
docs: {
description: {
story:
- "This example shows how the SegmentedBar handles many segments with legend enabled. The legend automatically shows a 'Show More' button when items don't fit in the available space, providing a clean way to manage large datasets.",
+ "This example shows how the SingleStackedBar handles many segments with legend enabled. The legend automatically shows a 'Show More' button when items don't fit in the available space, providing a clean way to manage large datasets.",
},
},
},
@@ -417,7 +417,7 @@ export const StackedLegendVariant: Story = {
Displays a vertical legend beneath the bar with percentage values and overflow handling.
</p>
</div>
- <SegmentedBar {...args} />
+ <SingleStackedBar {...args} />
</Card>
),
};
diff --git a/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/types/index.ts b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/types/index.ts
new file mode 100644
index 000000000..6f8e60302
--- /dev/null
+++ b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/types/index.ts
@@ -0,0 +1 @@
+export type SingleStackedBarData = Array<Record<string, string | number>>;
diff --git a/js/packages/react-ui/src/components/Charts/charts.scss b/js/packages/react-ui/src/components/Charts/charts.scss
index 7d546567b..f9894b101 100644
--- a/js/packages/react-ui/src/components/Charts/charts.scss
+++ b/js/packages/react-ui/src/components/Charts/charts.scss
@@ -16,8 +16,8 @@
// radar chart css
@forward "./RadarChart/radarChart.scss";
-// segmented bar chart css
-@forward "./SegmentedBarChart/segmentedBarChart.scss";
+// single stacked bar chart css
+@forward "./SingleStackedBarChart/singleStackedBarChart.scss";
// pie chart css
@forward "./PieChart/pieChart.scss";
diff --git a/js/packages/react-ui/src/components/Charts/hooks/useYAxisLabelWidth.tsx b/js/packages/react-ui/src/components/Charts/hooks/useYAxisLabelWidth.tsx
index 4fbc4b86b..ec276a97c 100644
--- a/js/packages/react-ui/src/components/Charts/hooks/useYAxisLabelWidth.tsx
+++ b/js/packages/react-ui/src/components/Charts/hooks/useYAxisLabelWidth.tsx
@@ -39,7 +39,7 @@ export const useYAxisLabelWidth = (
values.forEach((value) => {
const displayValue = numberTickFormatter(value);
- const textWidth = context.measureText(displayValue + "123").width;
+ const textWidth = context.measureText(displayValue).width;
maxWidth = Math.max(maxWidth, textWidth);
});
diff --git a/js/packages/react-ui/src/components/Charts/index.ts b/js/packages/react-ui/src/components/Charts/index.ts
index c9675066b..a1bf1bc40 100644
--- a/js/packages/react-ui/src/components/Charts/index.ts
+++ b/js/packages/react-ui/src/components/Charts/index.ts
@@ -9,4 +9,4 @@ export * from "./PieChart";
export * from "./RadarChart";
export * from "./RadialChart";
export * from "./ScatterChart";
-export * from "./SegmentedBarChart";
+export * from "./SingleStackedBarChart";
diff --git a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/DefaultLegend.tsx b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/DefaultLegend.tsx
index 68b7f2183..08cfb1f3d 100644
--- a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/DefaultLegend.tsx
+++ b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/DefaultLegend.tsx
@@ -99,7 +99,14 @@ const DefaultLegend = memo(
style={{ backgroundColor: item.color }}
/>
)}
- <span className="crayon-chart-legend-item-label">{item.label}</span>
+ <div className="crayon-chart-legend-item-label-container">
+ <span className="crayon-chart-legend-item-label">{item.label}</span>
+ {item.percentage !== undefined && (
+ <span className="crayon-chart-legend-item-percentage">
+ {item.percentage.toFixed(1)}%
+ </span>
+ )}
+ </div>
</div>
))}
diff --git a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/defaultLegend.scss b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/defaultLegend.scss
index 62b841c3b..ebc3b7f98 100644
--- a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/defaultLegend.scss
+++ b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/defaultLegend.scss
@@ -83,4 +83,15 @@
color: cssUtils.$primary-text;
}
}
+
+ &-item-label-container {
+ display: flex;
+ align-items: center;
+ gap: cssUtils.$spacing-2xs;
+ }
+
+ &-item-percentage {
+ @include cssUtils.typography(label, extra-small);
+ color: cssUtils.$secondary-text;
+ }
}
diff --git a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/hooks/useDefaultLegend.ts b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/hooks/useDefaultLegend.ts
index c04bce878..5c444ed81 100644
--- a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/hooks/useDefaultLegend.ts
+++ b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/hooks/useDefaultLegend.ts
@@ -29,14 +29,21 @@ export const useDefaultLegend = ({
const calculateItemWidth = useMemo(
() =>
- (label: string): number => {
+ (item: LegendItem): number => {
+ let displayText = item.label;
+
+ // If percentage is provided, include it in the width calculation
+ if (item.percentage !== undefined) {
+ displayText += ` (${item.percentage.toFixed(1)}%)`;
+ }
+
if (canvasContext) {
// If canvas is supported, measure text width accurately
- return canvasContext.measureText(label).width + INDICATOR_WIDTH + GAP_WIDTH;
+ return canvasContext.measureText(displayText).width + INDICATOR_WIDTH + GAP_WIDTH;
}
// Fallback for SSR or if canvas is not supported
- return label.length * CHARACTER_WIDTH + INDICATOR_WIDTH + GAP_WIDTH;
+ return displayText.length * CHARACTER_WIDTH + INDICATOR_WIDTH + GAP_WIDTH;
},
[canvasContext],
);
@@ -54,7 +61,7 @@ export const useDefaultLegend = ({
const item = items[i];
if (!item) continue;
- const itemWidth = calculateItemWidth(item.label);
+ const itemWidth = calculateItemWidth(item);
// Add gap between items except for the first one
const requiredWidth = visibleCount > 0 ? itemWidth + GAP_WIDTH : itemWidth;
diff --git a/js/packages/react-ui/src/components/Charts/shared/StackedLegend/StackedLegend.tsx b/js/packages/react-ui/src/components/Charts/shared/StackedLegend/StackedLegend.tsx
index 17dbc0c67..d2e3c5f2e 100644
--- a/js/packages/react-ui/src/components/Charts/shared/StackedLegend/StackedLegend.tsx
+++ b/js/packages/react-ui/src/components/Charts/shared/StackedLegend/StackedLegend.tsx
@@ -2,6 +2,7 @@ import { ChevronDown, ChevronUp } from "lucide-react";
import { useEffect, useRef, useState } from "react";
import { Button } from "../../../Button";
import { IconButton } from "../../../IconButton";
+import { Separator } from "../../../Separator";
import { StackedLegendItem } from "../../types";
interface StackedLegendProps {
@@ -11,6 +12,8 @@ interface StackedLegendProps {
onLegendItemHover?: (index: number | null) => void;
containerWidth?: number;
title?: string;
+ separator?: boolean;
+ showTitle?: boolean;
}
const formatPercentage = (value: number, total: number): string => {
@@ -29,6 +32,8 @@ export const StackedLegend = ({
activeKey,
onLegendItemHover,
containerWidth,
+ separator = false,
+ showTitle = true,
}: StackedLegendProps) => {
const containerRef = useRef<HTMLDivElement>(null);
const listRef = useRef<HTMLDivElement>(null);
@@ -119,7 +124,9 @@ export const StackedLegend = ({
}}
>
<div className="crayon-stacked-legend-header">
- <div className="crayon-stacked-legend-header-title">{items.length} values</div>
+ {showTitle && (
+ <div className="crayon-stacked-legend-header-title">{items.length} values</div>
+ )}
<div className="crayon-stacked-legend-header-buttons">
{!isShowMoreLayout && isOverflowing && (
<>
@@ -147,27 +154,32 @@ export const StackedLegend = ({
</div>
<div ref={listRef} className="crayon-stacked-legend">
{itemsToDisplay.map((item, index) => (
- <div
- key={item.key}
- className={`crayon-stacked-legend__item ${
- activeKey === item.key ? "crayon-stacked-legend__item--active" : ""
- }`}
- onMouseEnter={() => handleMouseEnter(item.key, index)}
- onMouseLeave={handleMouseLeave}
- >
- <div className="crayon-stacked-legend__item-label">
- <div className="crayon-stacked-legend__item-color-container">
- <div
- className="crayon-stacked-legend__item-color"
- style={{ backgroundColor: item.color }}
- />
+ <>
+ <div
+ key={item.key}
+ className={`crayon-stacked-legend__item ${
+ activeKey === item.key ? "crayon-stacked-legend__item--active" : ""
+ }`}
+ onMouseEnter={() => handleMouseEnter(item.key, index)}
+ onMouseLeave={handleMouseLeave}
+ >
+ <div className="crayon-stacked-legend__item-label">
+ <div className="crayon-stacked-legend__item-color-container">
+ <div
+ className="crayon-stacked-legend__item-color"
+ style={{ backgroundColor: item.color }}
+ />
+ </div>
+ <div className="crayon-stacked-legend__item-label-text">{item.label}</div>
+ </div>
+ <div className="crayon-stacked-legend__item-value">
+ {formatPercentage(item.value, total)}
</div>
- <div className="crayon-stacked-legend__item-label-text">{item.label}</div>
- </div>
- <div className="crayon-stacked-legend__item-value">
- {formatPercentage(item.value, total)}
</div>
- </div>
+ {index !== itemsToDisplay.length - 1 && separator && (
+ <Separator className="crayon-stacked-legend-separator" />
+ )}
+ </>
))}
</div>
{isShowMoreLayout && !showAll && items.length > LEGEND_ITEM_LIMIT && (
diff --git a/js/packages/react-ui/src/components/Charts/shared/StackedLegend/stackedLegend.scss b/js/packages/react-ui/src/components/Charts/shared/StackedLegend/stackedLegend.scss
index 11ec1894f..75580e80f 100644
--- a/js/packages/react-ui/src/components/Charts/shared/StackedLegend/stackedLegend.scss
+++ b/js/packages/react-ui/src/components/Charts/shared/StackedLegend/stackedLegend.scss
@@ -149,3 +149,7 @@
justify-content: center;
}
}
+
+.crayon-stacked-legend-separator {
+ margin: cssUtils.$spacing-2xs 0;
+}
diff --git a/js/packages/react-ui/src/components/Charts/types/Legend.ts b/js/packages/react-ui/src/components/Charts/types/Legend.ts
index 776e84b31..91a35c198 100644
--- a/js/packages/react-ui/src/components/Charts/types/Legend.ts
+++ b/js/packages/react-ui/src/components/Charts/types/Legend.ts
@@ -3,6 +3,7 @@ export interface LegendItem {
label: string;
color: string;
icon?: React.ComponentType;
+ percentage?: number;
}
export interface StackedLegendItem {