Skip to content

The Open Standard for Generative UI

Segmentated-bar-chart [TH-824] (#200)

Ankit Das7bca3f7f25fb
diff --git a/.cursor/rules/styling-rule.mdc b/.cursor/rules/styling-rule.mdc
deleted file mode 100644
index 151554f1c..000000000
--- a/.cursor/rules/styling-rule.mdc
+++ /dev/null
@@ -1,6 +0,0 @@
----
-description:
-globs: .scss
-alwaysApply: false
----
-for styling component use cssUtils.scss
\ No newline at end of file
diff --git a/.cursor/rules/use-pnpm.mdc b/.cursor/rules/use-pnpm.mdc
deleted file mode 100644
index 84750494b..000000000
--- a/.cursor/rules/use-pnpm.mdc
+++ /dev/null
@@ -1,6 +0,0 @@
----
-description: any task that uses terminal
-globs:
-alwaysApply: false
----
-Always use pnpm instead of npm for this project
diff --git a/js/packages/react-ui/package.json b/js/packages/react-ui/package.json
index f7f31edc5..62b2fd9e0 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.34",
+ "version": "0.8.35",
"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/SegmentedBarChart.tsx b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/SegmentedBarChart.tsx
index 3555bb24a..4826108ba 100644
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/SegmentedBarChart.tsx
+++ b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/SegmentedBarChart.tsx
@@ -1,37 +1,71 @@
import clsx from "clsx";
-import { useMemo } from "react";
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { SegmentedBarData } from ".";
+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";
-export interface SegmentedBarProps {
- data: SegmentedBarData;
+export interface SegmentedBarProps<T extends SegmentedBarData> {
+ data: T;
+ categoryKey: keyof T[number];
+ dataKey: keyof T[number];
theme?: PaletteName;
+ legend?: boolean;
+ legendVariant?: "default" | "stacked";
className?: string;
style?: React.CSSProperties;
animated?: boolean;
}
-export const SegmentedBar = ({
+export const SegmentedBar = <T extends SegmentedBarData>({
data,
+ categoryKey,
+ dataKey,
theme = "ocean",
+ legend = true,
+ legendVariant = "default",
className,
style,
animated = true,
-}: SegmentedBarProps) => {
+}: SegmentedBarProps<T>) => {
+ const [isLegendExpanded, setIsLegendExpanded] = useState(false);
+ const [activeIndex, setActiveIndex] = useState<number | null>(null);
+ const wrapperRef = useRef<HTMLDivElement>(null);
+ const [containerWidth, setContainerWidth] = useState(0);
+ const [tooltipPosition, setTooltipPosition] = useState<{ x: number; y: number } | null>(null);
+
+ useEffect(() => {
+ const wrapper = wrapperRef.current;
+ if (!wrapper) return;
+
+ const observer = new ResizeObserver((entries) => {
+ const entry = entries[0];
+ if (entry) {
+ setContainerWidth(entry.contentRect.width);
+ }
+ });
+
+ observer.observe(wrapper);
+ return () => observer.disconnect();
+ }, []);
// Calculate percentages
const segments = useMemo(() => {
if (!data || data.length === 0) {
return [];
}
- const total = data.reduce((acc, value) => acc + value, 0);
+ const total = data.reduce((acc, item) => acc + Number(item[dataKey]), 0);
- return data.map((value, index) => ({
- value,
+ return data.map((item, index) => ({
+ value: Number(item[dataKey]),
+ category: String(item[categoryKey]),
index,
- percentage: total > 0 ? (value / total) * 100 : 0,
+ percentage: total > 0 ? (Number(item[dataKey]) / total) * 100 : 0,
}));
- }, [data]);
+ }, [data, dataKey, categoryKey]);
// Get theme colors for each segment
const colors = useMemo(() => {
@@ -39,24 +73,117 @@ export const SegmentedBar = ({
return getDistributedColors(palette.colors, Math.max(segments.length, 1));
}, [theme, segments.length]);
+ // Create legend items
+ const legendItems = useMemo((): LegendItem[] => {
+ return segments.map((segment, index) => ({
+ key: `${segment.category}-${index}`,
+ label: segment.category,
+ color: colors[index % colors.length] || "",
+ }));
+ }, [segments, colors]);
+
+ // Create stacked legend items with values
+ const stackedLegendItems = useMemo(
+ (): StackedLegendItem[] =>
+ segments.map((segment, index) => ({
+ key: `${segment.category}-${index}`,
+ label: segment.category,
+ value: segment.value,
+ color: colors[index % colors.length] || "",
+ })),
+ [segments, colors],
+ );
+
+ // Handle legend item hover with tooltip positioning
+ const handleLegendItemHover = useCallback((hoverIndex: number | null) => {
+ setActiveIndex(hoverIndex);
+ if (hoverIndex !== null) {
+ // Try to position tooltip above the hovered segment
+ const segmentEl = wrapperRef.current?.querySelectorAll(
+ ".crayon-segmented-bar-chart-segment",
+ )?.[hoverIndex] as HTMLDivElement | undefined;
+ if (segmentEl) {
+ const rect = segmentEl.getBoundingClientRect();
+ const containerRect = wrapperRef.current?.getBoundingClientRect();
+ if (containerRect) {
+ const relativeX = rect.left + rect.width / 2 - containerRect.left;
+ const relativeY = rect.top - containerRect.top;
+ setTooltipPosition({ x: relativeX, y: relativeY });
+ } else {
+ setTooltipPosition({ x: rect.left + rect.width / 2, y: rect.top });
+ }
+ }
+ } else {
+ setTooltipPosition(null);
+ }
+ }, []);
+
// Segmented progress bar
return (
- <div className={clsx("crayon-segmented-bar-chart", className)} style={style}>
- {segments.map((segment, index) => {
- return (
- <div
- key={`segment-${index}`}
- className={clsx("crayon-segmented-bar-chart-segment", {
- "crayon-segmented-bar-chart-animated": animated,
- })}
- style={{
- width: `${segment.percentage}%`,
- backgroundColor: colors[index % colors.length],
- }}
- title={`${segment.value}`}
+ <div
+ ref={wrapperRef}
+ className={clsx("crayon-segmented-bar-chart-container", className)}
+ style={style}
+ >
+ <div className="crayon-segmented-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,
+ })}
+ style={{
+ width: `${segment.percentage}%`,
+ backgroundColor: colors[index % colors.length],
+ opacity: isActive ? 1 : 0.5,
+ }}
+ onMouseEnter={(e) => {
+ setActiveIndex(index);
+ const rect = (e.currentTarget as HTMLDivElement).getBoundingClientRect();
+ const containerRect = wrapperRef.current?.getBoundingClientRect();
+ if (containerRect) {
+ // Position relative to container so FloatingUIPortal aligns correctly
+ const relativeX = rect.left + rect.width / 2 - containerRect.left;
+ const relativeY = rect.top - containerRect.top;
+ setTooltipPosition({ x: relativeX, y: relativeY });
+ } else {
+ setTooltipPosition({ x: rect.left + rect.width / 2, y: rect.top });
+ }
+ }}
+ onMouseLeave={() => setActiveIndex(null)}
+ >
+ <div className="crayon-segmented-bar-chart-segment-line" />
+ </div>
+ );
+ })}
+ </div>
+ {activeIndex !== null && tooltipPosition && (
+ <FloatingUIPortal position={tooltipPosition} placement="top" offsetDistance={10}>
+ <ToolTip
+ label={legendItems[activeIndex]?.label ?? ""}
+ color={stackedLegendItems[activeIndex]?.color ?? "#000000"}
+ value={stackedLegendItems[activeIndex]?.value ?? 0}
+ percentage={segments[activeIndex]?.percentage ?? 0}
/>
- );
- })}
+ </FloatingUIPortal>
+ )}
+ {legend && legendVariant === "default" && (
+ <DefaultLegend
+ items={legendItems}
+ isExpanded={isLegendExpanded}
+ setIsExpanded={setIsLegendExpanded}
+ containerWidth={containerWidth}
+ />
+ )}
+ {legend && legendVariant === "stacked" && (
+ <StackedLegend
+ items={stackedLegendItems}
+ containerWidth={containerWidth}
+ onLegendItemHover={handleLegendItemHover}
+ />
+ )}
</div>
);
};
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/ToolTip.tsx b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/ToolTip.tsx
new file mode 100644
index 000000000..8cbb31e65
--- /dev/null
+++ b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/ToolTip.tsx
@@ -0,0 +1,44 @@
+import React from "react";
+
+interface ToolTipProps {
+ label: string;
+ color: string;
+ value: number | string;
+ percentage?: number;
+}
+
+export const ToolTip: React.FC<ToolTipProps> = ({ label, color, value, percentage }) => {
+ return (
+ <div className="crayon-chart-tooltip">
+ <div className="crayon-chart-tooltip-label">{label}</div>
+ <div className="crayon-chart-tooltip-content">
+ <div className="crayon-chart-tooltip-content-item">
+ <div
+ className="crayon-chart-tooltip-content-indicator crayon-chart-tooltip-content-indicator--dot"
+ style={{
+ ["--color-bg" as any]: color,
+ ["--color-border" as any]: color,
+ }}
+ />
+ <div className="crayon-chart-tooltip-content-value-wrapper">
+ <div className="crayon-chart-tooltip-content-label">
+ <span>Value</span>
+ </div>
+ <span className="crayon-chart-tooltip-content-value">{value}</span>
+ </div>
+ </div>
+ <div className="crayon-chart-tooltip-content-item-separator" />
+ <div className="crayon-chart-tooltip-content-item">
+ <div className="crayon-chart-tooltip-content-value-wrapper">
+ <div className="crayon-chart-tooltip-content-label">
+ <span>Percentage</span>
+ </div>
+ <span className="crayon-chart-tooltip-content-value percentage">
+ {typeof percentage === "number" ? `${percentage.toFixed(1)}%` : "-"}
+ </span>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/index.ts b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/index.ts
new file mode 100644
index 000000000..f06eadc00
--- /dev/null
+++ b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/components/index.ts
@@ -0,0 +1 @@
+export { ToolTip } from "./ToolTip";
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/segmentedBarChart.scss b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/segmentedBarChart.scss
index 963017d38..8b24abd0d 100644
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/segmentedBarChart.scss
+++ b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/segmentedBarChart.scss
@@ -1,5 +1,14 @@
@use "../../../cssUtils.scss" as cssUtils;
+.crayon-segmented-bar-chart-container {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ justify-content: center;
+ gap: cssUtils.$spacing-m;
+ position: relative;
+}
+
.crayon-segmented-bar-chart {
width: 100%;
height: 20px;
@@ -20,7 +29,33 @@
justify-content: center;
min-width: 2px;
box-shadow: cssUtils.$shadow-s;
- border-radius: cssUtils.$rounded-xs;
+ // border-radius: cssUtils.$rounded-xs;
+ position: relative;
+ overflow: hidden;
+ transition:
+ box-shadow 0.2s ease,
+ filter 0.2s ease,
+ opacity 0.2s ease;
+
+ &:first-child {
+ border-radius: cssUtils.$rounded-xs 0 0 cssUtils.$rounded-xs;
+ }
+
+ &:last-child {
+ border-radius: 0 cssUtils.$rounded-xs cssUtils.$rounded-xs 0;
+ }
+
+ &-line {
+ position: absolute;
+ transform: translateY(-50%);
+ background-color: cssUtils.$stroke-accent-emphasis;
+ top: 50%;
+ left: 5px;
+ width: calc(100% - 10px);
+ height: 1px;
+ z-index: 1;
+ min-width: 2px;
+ }
}
.crayon-segmented-bar-chart-animated {
diff --git a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/stories/SegmentedBarChart.stories.tsx b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/stories/SegmentedBarChart.stories.tsx
index 5540fb670..8460e90a6 100644
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/stories/SegmentedBarChart.stories.tsx
+++ b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/stories/SegmentedBarChart.stories.tsx
@@ -2,7 +2,7 @@ import type { Meta, StoryObj } from "@storybook/react";
import { Card } from "../../../Card";
import { SegmentedBar, SegmentedBarProps } from "../SegmentedBarChart";
-const meta: Meta<SegmentedBarProps> = {
+const meta: Meta<SegmentedBarProps<any>> = {
title: "Components/Charts/SegmentedBar",
component: SegmentedBar,
parameters: {
@@ -10,7 +10,7 @@ const meta: Meta<SegmentedBarProps> = {
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 'SegmentedBar'
// to emphasize its use for showing proportional data, much like a pie chart.
## Installation and Basic Usage
@@ -19,18 +19,36 @@ import { SegmentedBar } from '@crayon-ui/react-ui/Charts/SegmentedBar';
// Basic implementation
<SegmentedBar
- data={[25, 30, 20]}
+ data={[
+ { category: 'A', value: 25 },
+ { category: 'B', value: 30 },
+ { category: 'C', value: 20 }
+ ]}
+ categoryKey="category"
+ dataKey="value"
theme="ocean"
+ legend={true}
/>
\`\`\`
## Data Structure Requirements
-The data should be a simple array of numbers. Each number represents a segment in the bar. The component automatically calculates the proportional width of each segment based on the total sum of values.
+The data should be an array of objects with consistent property names. Use \`categoryKey\` to specify which property contains the segment labels, and \`dataKey\` to specify which property contains the numeric values.
\`\`\`tsx
-// Represents a composition of three values
-const data = [25, 30, 20];
+// Example data structure
+const data = [
+ { category: 'Desktop', value: 45 },
+ { category: 'Mobile', value: 35 },
+ { category: 'Tablet', value: 20 }
+];
+
+// Component usage
+<SegmentedBar
+ data={data}
+ categoryKey="category"
+ dataKey="value"
+/>
\`\`\`
## Key Features
@@ -38,27 +56,54 @@ const data = [25, 30, 20];
- **Segmented Display**: Visualize the composition of a whole.
- **Theming**: Six built-in color palettes.
- **Animation**: Smoothly animates the bar segments.
+- **Legend Support**: Optional legend with category names and colors.
- **Responsive**: Adapts to the width of its container.
`,
},
},
},
- tags: ["!dev", "!autodocs"],
+ tags: ["dev", "autodocs"],
argTypes: {
data: {
description: `
-**Required.** An array of numbers where each number represents a segment's value.
-The component automatically calculates the percentage width for each segment based on the total sum of values.
+**Required.** An array of objects where each object contains category and value information.
+The component extracts values using the specified \`dataKey\` and calculates proportional widths.
-**Example:** \`[20, 30, 15]\` creates three segments showing their proportional relationship.
+**Example:** \`[{ category: 'A', value: 20 }, { category: 'B', value: 30 }, { category: 'C', value: 15 }]\`
`,
control: false,
table: {
- type: { summary: "Array<number>" },
+ type: { summary: "Array<Record<string, string | number>>" },
defaultValue: { summary: "[]" },
category: "πŸ“Š Data Configuration",
},
},
+ categoryKey: {
+ description: `
+**Required.** The property name in each data object that contains the segment labels/categories.
+This value will be displayed in tooltips and used for accessibility.
+
+**Example:** \`'category'\` or \`'name'\`
+`,
+ control: "text",
+ table: {
+ type: { summary: "string" },
+ category: "πŸ“Š Data Configuration",
+ },
+ },
+ dataKey: {
+ description: `
+**Required.** The property name in each data object that contains the numeric values.
+These values determine the proportional width of each segment.
+
+**Example:** \`'value'\` or \`'count'\` or \`'percentage'\`
+`,
+ control: "text",
+ table: {
+ type: { summary: "string" },
+ category: "πŸ“Š Data Configuration",
+ },
+ },
theme: {
description: `
**Color Theme Selection.** Choose from professionally designed color palettes:
@@ -88,6 +133,31 @@ The component automatically calculates the percentage width for each segment bas
category: "🎬 Animation & Interaction",
},
},
+ legend: {
+ description: `
+**Legend Display.** Shows or hides the legend below the segmented bar chart.
+The legend displays category names with their corresponding colors.
+`,
+ control: "boolean",
+ table: {
+ type: { summary: "boolean" },
+ defaultValue: { summary: "false" },
+ category: "πŸ“Š Data Configuration",
+ },
+ },
+ legendVariant: {
+ description: `
+**Legend Variant.** Choose between default (inline) and stacked (vertical) legend.
+Stacked legend appears at the bottom and shows values as percentages.
+`,
+ control: "radio",
+ options: ["default", "stacked"],
+ table: {
+ type: { summary: '"default" | "stacked"' },
+ defaultValue: { summary: "default" },
+ category: "πŸ“± Display Options",
+ },
+ },
},
} satisfies Meta<typeof SegmentedBar>;
@@ -95,15 +165,35 @@ export default meta;
type Story = StoryObj<typeof meta>;
const sampleData = {
- default: [25, 30, 20],
- many: [10, 8, 12, 5, 15, 7, 13, 10],
- full: [25, 25, 25, 25],
+ default: [
+ { category: "A", value: 25 },
+ { category: "B", value: 30 },
+ { category: "C", value: 20 },
+ ],
+ many: [
+ { category: "Item 1", value: 10 },
+ { category: "Item 2", value: 8 },
+ { category: "Item 3", value: 12 },
+ { category: "Item 4", value: 5 },
+ { category: "Item 5", value: 15 },
+ { category: "Item 6", value: 7 },
+ { category: "Item 7", value: 13 },
+ { category: "Item 8", value: 10 },
+ ],
+ full: [
+ { category: "Q1", value: 25 },
+ { category: "Q2", value: 25 },
+ { category: "Q3", value: 25 },
+ { category: "Q4", value: 25 },
+ ],
};
export const DefaultConfiguration: Story = {
name: "πŸ“Š Default Configuration",
args: {
data: sampleData.default,
+ categoryKey: "category",
+ dataKey: "value",
theme: "ocean",
animated: true,
},
@@ -133,7 +223,14 @@ export const DefaultConfiguration: Story = {
export const ThemeShowcase: Story = {
name: "🎨 Theme Showcase",
args: {
- data: [15, 20, 25, 15],
+ data: [
+ { category: "Segment 1", value: 15 },
+ { category: "Segment 2", value: 20 },
+ { category: "Segment 3", value: 25 },
+ { category: "Segment 4", value: 15 },
+ ],
+ categoryKey: "category",
+ dataKey: "value",
animated: true,
},
render: (args: any) => (
@@ -169,6 +266,8 @@ export const ManySegments: Story = {
name: "🧩 Many Segments",
args: {
data: sampleData.many,
+ categoryKey: "category",
+ dataKey: "value",
theme: "vivid",
animated: true,
},
@@ -199,6 +298,8 @@ export const FullComposition: Story = {
name: "βœ… Full Composition",
args: {
data: sampleData.full,
+ categoryKey: "category",
+ dataKey: "value",
theme: "spectrum",
animated: false,
},
@@ -224,3 +325,99 @@ export const FullComposition: Story = {
},
},
};
+
+export const WithLegend: Story = {
+ name: "πŸ“Š With Legend",
+ args: {
+ data: sampleData.default,
+ categoryKey: "category",
+ dataKey: "value",
+ theme: "ocean",
+ animated: true,
+ legend: true,
+ legendVariant: "default",
+ },
+ render: (args: any) => (
+ <Card style={{ width: "500px", padding: "24px" }}>
+ <div style={{ marginBottom: "16px" }}>
+ <h3 style={{ margin: "0 0 8px 0", fontSize: "16px", fontWeight: "600" }}>
+ Category Breakdown with Legend
+ </h3>
+ <p style={{ margin: 0, color: "#666", fontSize: "14px" }}>
+ Visualizing the breakdown with a legend that shows category names and colors. The legend
+ automatically expands when there are many items.
+ </p>
+ </div>
+ <SegmentedBar {...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.",
+ },
+ },
+ },
+};
+
+export const LegendWithManyItems: Story = {
+ name: "🧩 Legend with Many Items",
+ args: {
+ data: sampleData.many,
+ categoryKey: "category",
+ dataKey: "value",
+ theme: "vivid",
+ animated: true,
+ legend: true,
+ legendVariant: "default",
+ },
+ render: (args: any) => (
+ <Card style={{ width: "600px", padding: "24px" }}>
+ <div style={{ marginBottom: "16px" }}>
+ <h3 style={{ margin: "0 0 8px 0", fontSize: "16px", fontWeight: "600" }}>
+ Resource Distribution with Legend
+ </h3>
+ <p style={{ margin: 0, color: "#666", fontSize: "14px" }}>
+ The component handles many segments gracefully with the legend showing category names and
+ values. The legend automatically manages overflow with expand/collapse functionality.
+ </p>
+ </div>
+ <SegmentedBar {...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.",
+ },
+ },
+ },
+};
+
+export const StackedLegendVariant: Story = {
+ name: "πŸ“š Stacked Legend (Bottom)",
+ args: {
+ data: sampleData.many,
+ categoryKey: "category",
+ dataKey: "value",
+ theme: "ocean",
+ animated: true,
+ legend: true,
+ legendVariant: "stacked",
+ },
+ render: (args: any) => (
+ <Card style={{ width: "650px", padding: "24px" }}>
+ <div style={{ marginBottom: "16px" }}>
+ <h3 style={{ margin: "0 0 8px 0", fontSize: "16px", fontWeight: "600" }}>
+ Stacked Legend at Bottom
+ </h3>
+ <p style={{ margin: 0, color: "#666", fontSize: "14px" }}>
+ Displays a vertical legend beneath the bar with percentage values and overflow handling.
+ </p>
+ </div>
+ <SegmentedBar {...args} />
+ </Card>
+ ),
+};
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
index aa0ddfb29..8c584efbb 100644
--- a/js/packages/react-ui/src/components/Charts/SegmentedBarChart/types/index.ts
+++ b/js/packages/react-ui/src/components/Charts/SegmentedBarChart/types/index.ts
@@ -1 +1 @@
-export type SegmentedBarData = Array<number>;
+export type SegmentedBarData = Array<Record<string, string | number>>;
diff --git a/js/packages/react-ui/src/components/Charts/shared/PortalTooltip/FloatingUIPortal.tsx b/js/packages/react-ui/src/components/Charts/shared/PortalTooltip/FloatingUIPortal.tsx
index cb5531bac..43640803b 100644
--- a/js/packages/react-ui/src/components/Charts/shared/PortalTooltip/FloatingUIPortal.tsx
+++ b/js/packages/react-ui/src/components/Charts/shared/PortalTooltip/FloatingUIPortal.tsx
@@ -1,3 +1,4 @@
+import type { Placement } from "@floating-ui/react-dom";
import { autoUpdate, flip, hide, offset, useFloating } from "@floating-ui/react-dom";
import clsx from "clsx";
import React, { useEffect } from "react";
@@ -10,6 +11,8 @@ interface FloatingUIPortalProps {
chartId?: string;
portalContainer?: React.RefObject<HTMLElement | null>;
position?: Partial<{ x: number; y: number }>;
+ placement?: Placement;
+ offsetDistance?: number;
}
export const FloatingUIPortal: React.FC<FloatingUIPortalProps> = ({
@@ -18,10 +21,12 @@ export const FloatingUIPortal: React.FC<FloatingUIPortalProps> = ({
chartId,
portalContainer,
position,
+ placement = "right-start",
+ offsetDistance = 20,
}) => {
const { refs, floatingStyles, update } = useFloating({
- placement: "right-start",
- middleware: [offset(20), flip(), hide()],
+ placement,
+ middleware: [offset(offsetDistance), flip(), hide()],
whileElementsMounted: autoUpdate,
});
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 381766684..17dbc0c67 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,16 +2,10 @@ import { ChevronDown, ChevronUp } from "lucide-react";
import { useEffect, useRef, useState } from "react";
import { Button } from "../../../Button";
import { IconButton } from "../../../IconButton";
-
-interface LegendItem {
- key: string;
- label: string;
- value: number;
- color: string;
-}
+import { StackedLegendItem } from "../../types";
interface StackedLegendProps {
- items: LegendItem[];
+ items: StackedLegendItem[];
onItemHover?: (key: string | null) => void;
activeKey?: string | null;
onLegendItemHover?: (index: number | null) => void;
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 75fd94384..776e84b31 100644
--- a/js/packages/react-ui/src/components/Charts/types/Legend.ts
+++ b/js/packages/react-ui/src/components/Charts/types/Legend.ts
@@ -4,3 +4,10 @@ export interface LegendItem {
color: string;
icon?: React.ComponentType;
}
+
+export interface StackedLegendItem {
+ key: string;
+ label: string;
+ value: number;
+ color: string;
+}