diff --git a/go/jsruntime/uikit/Chart.tsx b/go/jsruntime/uikit/Chart.tsx index 2c9e505b..75f4fbb3 100644 --- a/go/jsruntime/uikit/Chart.tsx +++ b/go/jsruntime/uikit/Chart.tsx @@ -24,6 +24,30 @@ import { createMemo, createSignal, For, Show, onMount, onCleanup, JSXElement } f export type ChartKind = "line" | "area" | "bar" | "pie" | "donut"; +interface HorizontalLegend { + orientation: "horizontal"; + position: + | "top-left" + | "top-center" + | "top-right" + | "bottom-left" + | "bottom-center" + | "bottom-right"; +} + +interface VerticalLegend { + orientation: "vertical"; + position: + | "top-left" + | "center-left" + | "bottom-left" + | "top-right" + | "center-right" + | "bottom-right"; +} + +type ChartLegend = HorizontalLegend | VerticalLegend; + export interface ChartSeries { name: string; data: number[]; @@ -67,7 +91,7 @@ export interface ChartProps { // Format a value for the value-axis ticks and the tooltip. Defaults to en-US grouping. valueFormat?: (v: number) => string; - legend?: boolean; // default: true when there is more than one series (or a pie). + legend?: boolean | ChartLegend; // default: true when there is more than one series (or a pie). grid?: boolean; // cartesian only; default true. axes?: boolean; // cartesian only; default true. tooltip?: boolean; // default true. @@ -277,7 +301,28 @@ export function Chart(props: ChartProps): JSXElement { const colorOf = (i: number) => props.series[i]?.color ?? props.palette?.[i % (props.palette.length || 1)] ?? CHART_TOKENS[i % CHART_TOKENS.length]; const fmt = (v: number) => (props.valueFormat ?? defaultFormat)(v); - const showLegend = () => props.legend ?? (isRadial() || props.series.length > 1); + + const legendVisible = () => { + if (props.legend === false) return false; + if (props.legend === true || props.legend === undefined) return isRadial() || props.series.length > 1; + return true; // an explicit ChartLegend object always shows the legend + }; + // `true`/undefined carries no orientation or position — null tells the layout below to + // keep the original placement (a left-aligned wrapping row under the plot) untouched. + // Only a caller-supplied ChartLegend object turns on the orientation/position logic. + const legendConfig = (): ChartLegend | null => + props.legend && typeof props.legend === "object" ? props.legend : null; + // Horizontal legends stack above/below the plot; vertical legends sit beside it. + const legendBefore = () => { + const cfg = legendConfig(); + if (!cfg) return false; // default position is "after", matching the original layout + return cfg.orientation === "horizontal" ? cfg.position.startsWith("top") : cfg.position.endsWith("left"); + }; + const containerClass = () => { + const cfg = legendConfig(); + if (!cfg) return "w-full"; + return cfg.orientation === "vertical" ? "flex w-full items-stretch gap-4" : "flex flex-col w-full gap-3"; + }; // Clicking a legend key hides its series (cartesian) or slice (radial); the domain, // layout and marks recompute from what's left. A Set of the hidden indices — the index @@ -290,22 +335,27 @@ export function Chart(props: ChartProps): JSXElement { }); return ( -