Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(charts): harden compact and edge-case rendering

Louis Beaumontd8dc2be0a4ff
diff --git a/apps/screenpipe-app-tauri/components/chat/charts/chart-marks.tsx b/apps/screenpipe-app-tauri/components/chat/charts/chart-marks.tsx
index d9fcf6a69..99f8900e5 100644
--- a/apps/screenpipe-app-tauri/components/chat/charts/chart-marks.tsx
+++ b/apps/screenpipe-app-tauri/components/chat/charts/chart-marks.tsx
@@ -71,16 +71,19 @@ export function StatChart({ spec, palette }: MarkProps<StatChartSpec>) {
/>
}
>
- <dl className="flex flex-wrap">
+ <dl
+ className="grid gap-x-3 gap-y-3"
+ style={{ gridTemplateColumns: "repeat(auto-fit, minmax(7rem, 1fr))" }}
+ >
{spec.items.map((item, index) => (
<div
key={`${item.label}-${index}`}
- className="min-w-[7rem] flex-1 border-border pr-4 [&:not(:first-child)]:border-l [&:not(:first-child)]:pl-4"
+ className="min-w-0 border-l border-border pl-3"
>
<dt className="truncate text-xs lowercase text-muted-foreground">
{item.label}
</dt>
- <dd className="mt-0.5 text-xl leading-tight text-foreground">
+ <dd className="mt-0.5 truncate whitespace-nowrap text-lg leading-tight text-foreground">
{formatChartValue(item.value, item.unit)}
</dd>
{item.note && (
@@ -99,12 +102,29 @@ export function StatChart({ spec, palette }: MarkProps<StatChartSpec>) {
// bar — horizontal, because app and activity names are long
// ---------------------------------------------------------------------------
+function divergingGeometry(values: number[]) {
+ const minimum = Math.min(0, ...values);
+ const maximum = Math.max(0, ...values);
+ const spread = maximum - minimum;
+ const zero = spread > 0 ? ((0 - minimum) / spread) * 100 : 50;
+ return {
+ minimum,
+ maximum,
+ zero,
+ mark(value: number) {
+ const end = spread > 0 ? ((value - minimum) / spread) * 100 : zero;
+ return {
+ left: Math.min(zero, end),
+ width: Math.abs(end - zero),
+ };
+ },
+ };
+}
+
export function BarChart({ spec, palette }: MarkProps<BarChartSpec>) {
const { tooltip, activeKey, show, hide } = useChartHover();
- const maximum = Math.max(
- ...spec.items.map((item) => Math.abs(item.value)),
- 0,
- );
+ const geometry = divergingGeometry(spec.items.map((item) => item.value));
+ const hasSignedDomain = geometry.minimum < 0 && geometry.maximum > 0;
return (
<ChartFrame
@@ -122,16 +142,15 @@ export function BarChart({ spec, palette }: MarkProps<BarChartSpec>) {
}
>
<ChartTooltip state={tooltip} />
- <div className="space-y-2">
+ <div className="space-y-2.5">
{spec.items.map((item, index) => {
const key = String(index);
- const width =
- maximum > 0 ? (Math.abs(item.value) / maximum) * 100 : 0;
+ const mark = geometry.mark(item.value);
return (
<div
key={`${item.label}-${index}`}
- className={`grid grid-cols-[${LABEL_COL}_1fr_auto] items-center gap-2.5`}
- style={{ gridTemplateColumns: `${LABEL_COL} 1fr auto` }}
+ data-chart-bar-row
+ className="min-w-0"
onPointerMove={(event) =>
show(
event,
@@ -141,25 +160,40 @@ export function BarChart({ spec, palette }: MarkProps<BarChartSpec>) {
}
onPointerLeave={hide}
>
- <span className="truncate text-xs text-muted-foreground">
- {item.label}
+ <span className="mb-1 flex min-w-0 items-baseline justify-between gap-3 text-xs">
+ <span className="truncate text-muted-foreground">
+ {item.label}
+ </span>
+ <span className="shrink-0 tabular-nums text-foreground">
+ {formatChartValue(item.value, spec.unit)}
+ </span>
</span>
<span
- className="block h-2 w-full"
+ data-chart-bar-track
+ className="relative block h-2 w-full overflow-hidden"
style={{ backgroundColor: palette.track }}
>
<span
- className="block h-full transition-colors duration-150"
+ data-chart-bar-fill
+ className="absolute inset-y-0 transition-colors duration-150"
style={{
- width: `${width}%`,
+ left: `${mark.left}%`,
+ width: `${mark.width}%`,
backgroundColor:
activeKey === key ? palette.focus : palette.single,
- minWidth: width > 0 ? 2 : 0,
+ minWidth: mark.width > 0 ? 2 : 0,
}}
/>
- </span>
- <span className="shrink-0 text-xs tabular-nums text-foreground">
- {formatChartValue(item.value, spec.unit)}
+ {hasSignedDomain ? (
+ <span
+ data-chart-zero-line
+ className="absolute inset-y-0 w-px"
+ style={{
+ left: `${geometry.zero}%`,
+ backgroundColor: palette.grid,
+ }}
+ />
+ ) : null}
</span>
</div>
);
@@ -182,13 +216,13 @@ export function LineChart({ spec, palette }: MarkProps<LineChartSpec>) {
const values = spec.items.map((item) => item.value);
const minimum = Math.min(...values);
const maximum = Math.max(...values);
- const spread = maximum > minimum ? maximum - minimum : 1;
+ const spread = maximum - minimum;
const coordinates = spec.items.map((item, index) => ({
x:
spec.items.length <= 1
? VIEWBOX.width / 2
: (index / (spec.items.length - 1)) * VIEWBOX.width,
- y: 92 - ((item.value - minimum) / spread) * 84,
+ y: spread > 0 ? 92 - ((item.value - minimum) / spread) * 84 : 50,
}));
return {
minimum,
@@ -203,6 +237,9 @@ export function LineChart({ spec, palette }: MarkProps<LineChartSpec>) {
const activePoint = activeIndex === null ? null : spec.items[activeIndex];
const activeCoordinate =
activeIndex === null ? null : geometry.coordinates[activeIndex];
+ const visibleCoordinate =
+ activeCoordinate ??
+ (spec.items.length === 1 ? geometry.coordinates[0] : null);
// Resting state names the span the line covers, so the x axis is readable
// before the reader hovers anything.
@@ -297,14 +334,16 @@ export function LineChart({ spec, palette }: MarkProps<LineChartSpec>) {
/>
) : null}
</svg>
- {activeCoordinate ? (
+ {visibleCoordinate ? (
<span
+ data-chart-line-point
aria-hidden="true"
className="pointer-events-none absolute h-2 w-2 -translate-x-1/2 -translate-y-1/2 border"
style={{
- left: `${activeCoordinate.x}%`,
- top: `${activeCoordinate.y}%`,
- backgroundColor: palette.focus,
+ left: `${visibleCoordinate.x}%`,
+ top: `${visibleCoordinate.y}%`,
+ backgroundColor:
+ activeCoordinate === null ? palette.single : palette.focus,
borderColor: palette.surface,
}}
/>
@@ -323,10 +362,10 @@ export function GroupedBarChart({
palette,
}: MarkProps<GroupedBarChartSpec>) {
const { tooltip, activeKey, show, hide } = useChartHover();
- const maximum = Math.max(
- ...spec.series.flatMap((series) => series.values.map(Math.abs)),
- 0,
+ const geometry = divergingGeometry(
+ spec.series.flatMap((series) => series.values),
);
+ const hasSignedDomain = geometry.minimum < 0 && geometry.maximum > 0;
return (
<ChartFrame
@@ -361,12 +400,11 @@ export function GroupedBarChart({
{spec.series.map((series, seriesIndex) => {
const value = series.values[categoryIndex];
const key = `${categoryIndex}-${seriesIndex}`;
- const width =
- maximum > 0 ? (Math.abs(value) / maximum) * 100 : 0;
+ const mark = geometry.mark(value);
return (
<span
key={key}
- className="block h-1.5 w-full"
+ className="relative block h-1.5 w-full overflow-hidden"
style={{ backgroundColor: palette.track }}
onPointerMove={(event) =>
show(
@@ -381,9 +419,11 @@ export function GroupedBarChart({
onPointerLeave={hide}
>
<span
- className="block h-full transition-colors duration-150"
+ data-chart-grouped-fill
+ className="absolute inset-y-0 transition-colors duration-150"
style={{
- width: `${width}%`,
+ left: `${mark.left}%`,
+ width: `${mark.width}%`,
backgroundColor:
activeKey === key
? palette.focus
@@ -392,9 +432,19 @@ export function GroupedBarChart({
seriesIndex,
spec.series.length,
),
- minWidth: width > 0 ? 2 : 0,
+ minWidth: mark.width > 0 ? 2 : 0,
}}
/>
+ {hasSignedDomain ? (
+ <span
+ data-chart-zero-line
+ className="absolute inset-y-0 w-px"
+ style={{
+ left: `${geometry.zero}%`,
+ backgroundColor: palette.grid,
+ }}
+ />
+ ) : null}
</span>
);
})}
@@ -965,8 +1015,8 @@ export function FunnelChart({ spec, palette }: MarkProps<FunnelChartSpec>) {
return (
<div
key={`${item.label}-${index}`}
- className="grid items-center gap-x-2.5"
- style={{ gridTemplateColumns: `1.5rem ${LABEL_COL} 1fr auto` }}
+ className="grid min-w-0 gap-x-2.5"
+ style={{ gridTemplateColumns: "1.5rem minmax(0, 1fr)" }}
onPointerMove={(event) =>
show(
event,
@@ -979,31 +1029,38 @@ export function FunnelChart({ spec, palette }: MarkProps<FunnelChartSpec>) {
<span className="text-[10px] tabular-nums text-muted-foreground">
{String(index + 1).padStart(2, "0")}
</span>
- <span className="truncate text-xs text-muted-foreground">
- {item.label}
- </span>
- <span
- className="block h-1.5 w-full"
- style={{ backgroundColor: palette.track }}
- >
- <span
- className="block h-full transition-colors duration-150"
- style={{
- width: `${percent}%`,
- minWidth: item.value > 0 ? 2 : 0,
- backgroundColor:
- activeKey === key
- ? palette.focus
- : seriesColor(palette, index, spec.items.length),
- }}
- />
- </span>
- <span className="flex min-w-[7.5rem] items-baseline justify-end gap-2 text-right tabular-nums">
- <span className="text-xs text-foreground">
- {formatChartValue(item.value, spec.unit)}
+ <span className="min-w-0">
+ <span className="mb-1 flex min-w-0 items-baseline justify-between gap-2">
+ <span className="truncate text-xs text-muted-foreground">
+ {item.label}
+ </span>
+ <span className="flex shrink-0 items-baseline gap-2 text-right tabular-nums">
+ <span className="text-xs text-foreground">
+ {formatChartValue(item.value, spec.unit)}
+ </span>
+ <span className="w-14 text-[10px] text-muted-foreground">
+ {index === 0
+ ? "start"
+ : `${Math.round(priorPercent)}% prior`}
+ </span>
+ </span>
</span>
- <span className="w-14 text-[10px] text-muted-foreground">
- {index === 0 ? "start" : `${Math.round(priorPercent)}% prior`}
+ <span
+ data-chart-funnel-track
+ className="block h-1.5 w-full"
+ style={{ backgroundColor: palette.track }}
+ >
+ <span
+ className="block h-full transition-colors duration-150"
+ style={{
+ width: `${percent}%`,
+ minWidth: item.value > 0 ? 2 : 0,
+ backgroundColor:
+ activeKey === key
+ ? palette.focus
+ : seriesColor(palette, index, spec.items.length),
+ }}
+ />
</span>
</span>
</div>
@@ -1202,8 +1259,9 @@ export function RangeChart({ spec, palette }: MarkProps<RangeChartSpec>) {
const { tooltip, activeKey, show, hide } = useChartHover();
const minimum = Math.min(...spec.items.map((item) => item.min));
const maximum = Math.max(...spec.items.map((item) => item.max));
- const spread = maximum > minimum ? maximum - minimum : 1;
- const position = (value: number) => ((value - minimum) / spread) * 100;
+ const spread = maximum - minimum;
+ const position = (value: number) =>
+ spread > 0 ? ((value - minimum) / spread) * 100 : 50;
return (
<ChartFrame
@@ -1234,15 +1292,24 @@ export function RangeChart({ spec, palette }: MarkProps<RangeChartSpec>) {
return (
<div
key={`${item.label}-${index}`}
- className="grid items-center gap-2.5"
- style={{ gridTemplateColumns: `${LABEL_COL} 1fr 6rem` }}
+ className="min-w-0"
onPointerMove={(event) => show(event, text, key)}
onPointerLeave={hide}
>
- <span className="truncate text-xs text-muted-foreground">
- {item.label}
+ <span className="mb-1 flex min-w-0 items-baseline justify-between gap-3">
+ <span className="truncate text-xs text-muted-foreground">
+ {item.label}
+ </span>
+ <span className="shrink-0 text-xs tabular-nums text-foreground">
+ {item.mid === null
+ ? formatChartValue(item.max, spec.unit)
+ : formatChartValue(item.mid, spec.unit)}
+ </span>
</span>
- <span className="relative block h-3">
+ <span
+ data-chart-range-track
+ className="relative block h-3 w-full"
+ >
<span className="absolute left-0 right-0 top-1/2 border-t border-border" />
<span
className="absolute top-1/2 h-0.5 -translate-y-1/2 transition-colors duration-150"
@@ -1254,10 +1321,12 @@ export function RangeChart({ spec, palette }: MarkProps<RangeChartSpec>) {
}}
/>
<span
+ data-chart-range-start
className="absolute top-1/2 h-2 w-0.5 -translate-x-1/2 -translate-y-1/2"
style={{ left: `${left}%`, backgroundColor: palette.single }}
/>
<span
+ data-chart-range-end
className="absolute top-1/2 h-2 w-0.5 -translate-x-1/2 -translate-y-1/2"
style={{
left: `${left + width}%`,
@@ -1266,6 +1335,7 @@ export function RangeChart({ spec, palette }: MarkProps<RangeChartSpec>) {
/>
{item.mid !== null ? (
<span
+ data-chart-range-mid
className="absolute top-1/2 h-2.5 w-2.5 -translate-x-1/2 -translate-y-1/2 border"
style={{
left: `${position(item.mid)}%`,
@@ -1278,16 +1348,9 @@ export function RangeChart({ spec, palette }: MarkProps<RangeChartSpec>) {
/>
) : null}
</span>
- <span className="flex min-w-[7rem] flex-col text-right tabular-nums">
- <span className="text-xs text-foreground">
- {item.mid === null
- ? formatChartValue(item.max, spec.unit)
- : formatChartValue(item.mid, spec.unit)}
- </span>
- <span className="text-[10px] text-muted-foreground">
- {formatChartValue(item.min, spec.unit)}–
- {formatChartValue(item.max, spec.unit)}
- </span>
+ <span className="flex justify-between text-[10px] tabular-nums text-muted-foreground">
+ <span>{formatChartValue(item.min, spec.unit)}</span>
+ <span>{formatChartValue(item.max, spec.unit)}</span>
</span>
</div>
);
@@ -1310,12 +1373,31 @@ export function ScatterChart({ spec, palette }: MarkProps<ScatterChartSpec>) {
const maxX = Math.max(...xs);
const minY = Math.min(...ys);
const maxY = Math.max(...ys);
- const spreadX = maxX > minX ? maxX - minX : 1;
- const spreadY = maxY > minY ? maxY - minY : 1;
- const coordinates = spec.items.map((item) => ({
- x: 6 + ((item.x - minX) / spreadX) * 88,
- y: 94 - ((item.y - minY) / spreadY) * 88,
+ const spreadX = maxX - minX;
+ const spreadY = maxY - minY;
+ const baseCoordinates = spec.items.map((item) => ({
+ x: spreadX > 0 ? 6 + ((item.x - minX) / spreadX) * 88 : 50,
+ y: spreadY > 0 ? 94 - ((item.y - minY) / spreadY) * 88 : 50,
}));
+ const coincident = new Map<string, number[]>();
+ spec.items.forEach((item, index) => {
+ const key = `${item.x}\u0000${item.y}`;
+ const group = coincident.get(key) ?? [];
+ group.push(index);
+ coincident.set(key, group);
+ });
+ // Exact duplicate measurements remain truthful in the table, while a tiny
+ // deterministic ring keeps every marker inspectable in the plot.
+ const coordinates = baseCoordinates.map((point, index) => {
+ const item = spec.items[index];
+ const group = coincident.get(`${item.x}\u0000${item.y}`) ?? [index];
+ if (group.length === 1) return point;
+ const angle = (group.indexOf(index) / group.length) * Math.PI * 2;
+ return {
+ x: Math.min(94, Math.max(6, point.x + Math.cos(angle) * 4)),
+ y: Math.min(94, Math.max(6, point.y + Math.sin(angle) * 4)),
+ };
+ });
return { minX, maxX, minY, maxY, spreadX, spreadY, coordinates };
}, [spec.items]);
const active = activeIndex === null ? null : spec.items[activeIndex];
@@ -1446,6 +1528,7 @@ export function ScatterChart({ spec, palette }: MarkProps<ScatterChartSpec>) {
{geometry.coordinates.map((point, index) => (
<span
key={`${spec.items[index].label}-${index}`}
+ data-chart-scatter-point
aria-hidden="true"
className="pointer-events-none absolute flex h-3 w-3 -translate-x-1/2 -translate-y-1/2 items-center justify-center border text-[8px] tabular-nums transition-colors duration-150"
style={{
diff --git a/apps/screenpipe-app-tauri/components/chat/charts/chart-prompt-contract.test.ts b/apps/screenpipe-app-tauri/components/chat/charts/chart-prompt-contract.test.ts
index 9406d7b2e..4fa3dd5a1 100644
--- a/apps/screenpipe-app-tauri/components/chat/charts/chart-prompt-contract.test.ts
+++ b/apps/screenpipe-app-tauri/components/chat/charts/chart-prompt-contract.test.ts
@@ -94,6 +94,13 @@ describe("chart system prompt contract", () => {
expect(section.toLowerCase()).toContain("never send colors");
});
+ it("documents renderer safety constraints", () => {
+ expect(section).toContain("Funnel/stacked values: non-negative");
+ expect(section).toContain(
+ `calendar: unique dates within ${CHART_LIMITS.calendarItems} days`,
+ );
+ });
+
it("stays small enough to earn a permanent seat in context", () => {
// Guard against the section growing back into a per-type example dump.
expect(section.length).toBeLessThan(2900);
diff --git a/apps/screenpipe-app-tauri/components/chat/charts/chart-spec.test.ts b/apps/screenpipe-app-tauri/components/chat/charts/chart-spec.test.ts
index 224e3fabb..8d104aca3 100644
--- a/apps/screenpipe-app-tauri/components/chat/charts/chart-spec.test.ts
+++ b/apps/screenpipe-app-tauri/components/chat/charts/chart-spec.test.ts
@@ -179,6 +179,14 @@ describe("parseChartSpec — stacked_bar", () => {
parseChartSpec('{"type":"stacked_bar","categories":["a"],"series":[]}'),
).toBeNull();
});
+
+ it("rejects negative values that cannot be represented as parts of a whole", () => {
+ expect(
+ parseChartSpec(
+ '{"type":"stacked_bar","categories":["mon"],"series":[{"name":"work","values":[-1]}]}',
+ ),
+ ).toBeNull();
+ });
});
describe("parseChartSpec — heatmap", () => {
@@ -348,6 +356,13 @@ describe("parseChartSpec — grouped_bar", () => {
),
).toBeNull();
});
+
+ it("keeps signed values for diverging comparisons", () => {
+ const spec = parseChartSpec(
+ '{"type":"grouped_bar","categories":["change"],"series":[{"name":"current","values":[-2]},{"name":"previous","values":[3]}]}',
+ );
+ expect(spec?.type).toBe("grouped_bar");
+ });
});
describe("parseChartSpec — calendar", () => {
@@ -379,6 +394,19 @@ describe("parseChartSpec — calendar", () => {
),
).toBeNull();
});
+
+ it("rejects sparse inputs spanning more days than the renderer can bound", () => {
+ expect(
+ parseChartSpec(
+ '{"type":"calendar","items":[{"date":"2026-01-01","value":1},{"date":"2026-03-25","value":1}]}',
+ ),
+ ).not.toBeNull();
+ expect(
+ parseChartSpec(
+ '{"type":"calendar","items":[{"date":"2026-01-01","value":1},{"date":"2026-03-26","value":1}]}',
+ ),
+ ).toBeNull();
+ });
});
describe("parseChartSpec — funnel", () => {
diff --git a/apps/screenpipe-app-tauri/components/chat/charts/chart-spec.ts b/apps/screenpipe-app-tauri/components/chat/charts/chart-spec.ts
index b873b30e9..73635e0e5 100644
--- a/apps/screenpipe-app-tauri/components/chat/charts/chart-spec.ts
+++ b/apps/screenpipe-app-tauri/components/chat/charts/chart-spec.ts
@@ -394,6 +394,10 @@ function parseCalendar(
out.push({ date, value });
}
out.sort((a, b) => a.date.localeCompare(b.date));
+ const firstDay = Date.parse(`${out[0].date}T00:00:00Z`);
+ const lastDay = Date.parse(`${out[out.length - 1].date}T00:00:00Z`);
+ const inclusiveDays = (lastDay - firstDay) / 86_400_000 + 1;
+ if (inclusiveDays > CHART_LIMITS.calendarItems) return null;
return {
...base,
type: "calendar",
@@ -516,6 +520,9 @@ function parseCategorySeries(
if (entry.values.length !== (raw.categories as unknown[]).length) return null;
const values = readNumberArray(entry.values, categories.length);
if (values === null || values.length !== categories.length) return null;
+ if (type === "stacked_bar" && values.some((value) => value < 0)) {
+ return null;
+ }
series.push({ name, values });
}
diff --git a/apps/screenpipe-app-tauri/components/chat/charts/chat-chart.test.tsx b/apps/screenpipe-app-tauri/components/chat/charts/chat-chart.test.tsx
index c00d1c1c2..3237a702f 100644
--- a/apps/screenpipe-app-tauri/components/chat/charts/chat-chart.test.tsx
+++ b/apps/screenpipe-app-tauri/components/chat/charts/chat-chart.test.tsx
@@ -161,6 +161,61 @@ describe("ChatChart — bar", () => {
);
expect(screen.getByText(/showing first 20 of 25 points/)).toBeInTheDocument();
});
+
+ it("uses one shared diverging scale for signed values", () => {
+ const { container } = render(
+ <ChatChart
+ spec={specFrom({
+ type: "bar",
+ items: [
+ { label: "gain", value: 10 },
+ { label: "loss", value: -10 },
+ ],
+ })}
+ />,
+ );
+ const fills = Array.from(
+ container.querySelectorAll<HTMLElement>("[data-chart-bar-fill]"),
+ );
+ expect(fills).toHaveLength(2);
+ expect(fills[0].style.left).toBe("50%");
+ expect(fills[1].style.left).toBe("0%");
+ expect(fills[0].style.width).toBe(fills[1].style.width);
+ expect(container.querySelectorAll("[data-chart-zero-line]")).toHaveLength(
+ 2,
+ );
+ });
+
+ it("highlights the bar under the pointer", () => {
+ const { container } = render(
+ <ChatChart
+ spec={specFrom({
+ type: "bar",
+ items: [
+ { label: "first", value: 10 },
+ { label: "second", value: 5 },
+ ],
+ })}
+ />,
+ );
+ const rows = container.querySelectorAll<HTMLElement>(
+ "[data-chart-bar-row]",
+ );
+ fireEvent(
+ rows[0],
+ new MouseEvent("pointermove", {
+ bubbles: true,
+ clientX: 8,
+ clientY: 8,
+ }),
+ );
+ const fills = container.querySelectorAll<HTMLElement>(
+ "[data-chart-bar-fill]",
+ );
+ expect(fills[0].style.backgroundColor).not.toBe(
+ fills[1].style.backgroundColor,
+ );
+ });
});
describe("ChatChart — stacked_bar", () => {
@@ -205,6 +260,23 @@ describe("ChatChart — line", () => {
);
expect(screen.getByText("2 h – 5 h")).toBeInTheDocument();
});
+
+ it("centers and paints a single point instead of leaving a blank plot", () => {
+ const { container } = render(
+ <ChatChart
+ spec={specFrom({
+ type: "line",
+ items: [{ label: "now", value: 7 }],
+ })}
+ />,
+ );
+ const point = container.querySelector<HTMLElement>(
+ "[data-chart-line-point]",
+ );
+ expect(point).not.toBeNull();
+ expect(point?.style.left).toBe("50%");
+ expect(point?.style.top).toBe("50%");
+ });
});
describe("ChatChart — heatmap", () => {
@@ -420,6 +492,25 @@ describe("ChatChart — new mark types", () => {
expect(screen.getAllByText("last week").length).toBeGreaterThan(0);
});
+ it("renders grouped negative values on the opposite side of zero", () => {
+ const { container } = render(
+ <ChatChart
+ spec={specFrom({
+ type: "grouped_bar",
+ categories: ["change"],
+ series: [
+ { name: "gain", values: [4] },
+ { name: "loss", values: [-4] },
+ ],
+ })}
+ />,
+ );
+ const fills = Array.from(
+ container.querySelectorAll<HTMLElement>("[data-chart-grouped-fill]"),
+ );
+ expect(fills.map((fill) => fill.style.left)).toEqual(["50%", "0%"]);
+ });
+
it("renders a calendar grid with its date span", () => {
render(
<ChatChart
@@ -496,6 +587,26 @@ describe("ChatChart — new mark types", () => {
expect(screen.getAllByText("31 min")).toHaveLength(2);
});
+ it("centers a flat range domain", () => {
+ const { container } = render(
+ <ChatChart
+ spec={specFrom({
+ type: "range",
+ items: [{ label: "fixed", min: 4, mid: 4, max: 4 }],
+ })}
+ />,
+ );
+ for (const selector of [
+ "[data-chart-range-start]",
+ "[data-chart-range-end]",
+ "[data-chart-range-mid]",
+ ]) {
+ expect(container.querySelector<HTMLElement>(selector)?.style.left).toBe(
+ "50%",
+ );
+ }
+ });
+
it("renders scatter axes and every labeled point in the data table", () => {
render(
<ChatChart
@@ -516,4 +627,24 @@ describe("ChatChart — new mark types", () => {
expect(screen.getByText("4 h · 9")).toBeInTheDocument();
expect(screen.getByText("2 points")).toBeInTheDocument();
});
+
+ it("separates coincident scatter markers around the truthful coordinate", () => {
+ const { container } = render(
+ <ChatChart
+ spec={specFrom({
+ type: "scatter",
+ items: [
+ { label: "one", x: 5, y: 5 },
+ { label: "two", x: 5, y: 5 },
+ { label: "three", x: 5, y: 5 },
+ ],
+ })}
+ />,
+ );
+ const positions = Array.from(
+ container.querySelectorAll<HTMLElement>("[data-chart-scatter-point]"),
+ (point) => `${point.style.left}/${point.style.top}`,
+ );
+ expect(new Set(positions).size).toBe(3);
+ });
});
diff --git a/apps/screenpipe-app-tauri/e2e/specs/chat-inline-charts.spec.ts b/apps/screenpipe-app-tauri/e2e/specs/chat-inline-charts.spec.ts
index 632191aa7..9ccdba832 100644
--- a/apps/screenpipe-app-tauri/e2e/specs/chat-inline-charts.spec.ts
+++ b/apps/screenpipe-app-tauri/e2e/specs/chat-inline-charts.spec.ts
@@ -19,6 +19,7 @@
* 5. no chart uses a rounded corner (DESIGN.md: sharp corners always)
* 6. every chat chart exposes the Live View prompt handoff menu
* 7. the action-menu wrapper preserves the chart's full message width
+ * 8. bar, funnel, and range tracks remain useful in a 300 px chat column
*
* No model run and no network: the assistant message is seeded directly.
*
@@ -202,9 +203,8 @@ describe("Inline charts in chat", function () {
const widths = await browser.execute(() => {
const bar = document.querySelector('[data-chart-type="bar"]');
if (!bar) return null;
- // Each row's fill is the inner span carrying an explicit width.
const fills = Array.from(
- bar.querySelectorAll<HTMLElement>("span > span[style*='width']"),
+ bar.querySelectorAll<HTMLElement>("[data-chart-bar-fill]"),
).map((el) => el.getBoundingClientRect().width);
return fills.filter((width) => width > 0);
});
@@ -241,16 +241,31 @@ describe("Inline charts in chat", function () {
expect(Math.abs(layout!.chartRight - layout!.messageRight)).toBeLessThan(2);
});
- it("shows a tooltip and highlights the mark on hover", async () => {
+ it("shows a tooltip for pointer movement over a mark", async () => {
const before = await browser.execute(() => {
const bar = document.querySelector('[data-chart-type="bar"]');
return bar?.querySelector('[role="presentation"]') ? "tooltip" : "none";
});
expect(before).toBe("none");
- const row = await $('[data-chart-type="bar"] [data-chart-surface] > div > div');
+ await browser.execute(() => {
+ const row = document.querySelector<HTMLElement>(
+ '[data-chart-type="bar"] [data-chart-bar-row]',
+ );
+ if (!row) return;
+ row.scrollIntoView({ block: "center" });
+ const bounds = row.getBoundingClientRect();
+ row.dispatchEvent(
+ new PointerEvent("pointermove", {
+ bubbles: true,
+ clientX: bounds.left + 8,
+ clientY: bounds.top + 8,
+ pointerType: "mouse",
+ }),
+ );
+ });
+ const row = await $('[data-chart-type="bar"] [data-chart-bar-row]');
await row.waitForExist({ timeout: t(8_000) });
- await row.moveTo();
await browser.waitUntil(
async () =>
@@ -266,18 +281,13 @@ describe("Inline charts in chat", function () {
timeoutMsg: "hover tooltip never showed the hovered bar",
},
);
-
- // The hovered fill switches to the neutral focus signal.
- const focused = await browser.execute(() => {
- const fills = Array.from(
- document.querySelectorAll<HTMLElement>(
- '[data-chart-type="bar"] span > span[style*="width"]',
- ),
- );
- return fills.map((el) => getComputedStyle(el).backgroundColor);
- });
- const distinct = new Set(focused as string[]);
- expect(distinct.size).toBeGreaterThan(1);
+ const fillCount = await browser.execute(
+ () =>
+ document.querySelectorAll(
+ '[data-chart-type="bar"] [data-chart-bar-fill]',
+ ).length,
+ );
+ expect(fillCount).toBe(3);
});
it("falls back to a readable code block for the malformed fence", async () => {
@@ -302,13 +312,56 @@ describe("Inline charts in chat", function () {
const triggers = await $$('button[aria-label="chart actions"]');
expect(triggers).toHaveLength(7);
- await triggers[0].click();
- const action = await $('[role="menuitem"]');
- await action.waitForDisplayed({ timeout: t(8_000) });
- expect((await action.getText()).toLowerCase()).toContain("add to live view");
+ await browser.execute(() => {
+ const trigger = document.querySelector<HTMLElement>(
+ 'button[aria-label="chart actions"]',
+ );
+ if (!trigger) return;
+ trigger.scrollIntoView({ block: "center" });
+ trigger.focus();
+ trigger.dispatchEvent(
+ new KeyboardEvent("keydown", {
+ key: "ArrowDown",
+ code: "ArrowDown",
+ bubbles: true,
+ }),
+ );
+ });
+ const actionState = await browser.waitUntil(
+ () =>
+ browser.execute(() => {
+ const action = document.querySelector<HTMLElement>('[role="menuitem"]');
+ if (!action) return null;
+ const bounds = action.getBoundingClientRect();
+ const style = getComputedStyle(action);
+ return {
+ text: action.textContent ?? "",
+ rendered:
+ bounds.width > 0 &&
+ bounds.height > 0 &&
+ style.display !== "none" &&
+ style.visibility !== "hidden" &&
+ Number(style.opacity || "1") > 0,
+ };
+ }),
+ {
+ timeout: t(8_000),
+ interval: 100,
+ timeoutMsg: "Live View menu item never rendered",
+ },
+ );
+ expect(actionState?.rendered).toBe(true);
+ expect(actionState?.text.toLowerCase()).toContain("add to live view");
- await browser.keys(["Escape"]);
- await action.waitForDisplayed({ reverse: true, timeout: t(8_000) });
+ await browser.execute(() => {
+ (document.activeElement ?? document).dispatchEvent(
+ new KeyboardEvent("keydown", {
+ key: "Escape",
+ code: "Escape",
+ bubbles: true,
+ }),
+ );
+ });
});
it("uses sharp corners everywhere, per DESIGN.md", async () => {
@@ -328,6 +381,45 @@ describe("Inline charts in chat", function () {
expect(radii).toEqual([]);
});
+ it("keeps comparison tracks useful in a 300 px chat column", async () => {
+ const layout = await browser.execute(() => {
+ const bar = document.querySelector<HTMLElement>('[data-chart-type="bar"]');
+ const message = bar?.closest<HTMLElement>(".prose");
+ if (!bar || !message) return null;
+ const previousWidth = message.style.width;
+ const previousMaxWidth = message.style.maxWidth;
+ message.style.width = "300px";
+ message.style.maxWidth = "300px";
+
+ const trackWidth = (selector: string) =>
+ document.querySelector<HTMLElement>(selector)?.getBoundingClientRect()
+ .width ?? 0;
+ const types = ["bar", "funnel", "range"];
+ const result = {
+ bar: trackWidth('[data-chart-type="bar"] [data-chart-bar-track]'),
+ funnel: trackWidth(
+ '[data-chart-type="funnel"] [data-chart-funnel-track]',
+ ),
+ range: trackWidth('[data-chart-type="range"] [data-chart-range-track]'),
+ noOverflow: types.every((type) => {
+ const chart = document.querySelector<HTMLElement>(
+ `[data-chart-type="${type}"]`,
+ );
+ return Boolean(chart && chart.scrollWidth <= chart.clientWidth + 1);
+ }),
+ };
+ message.style.width = previousWidth;
+ message.style.maxWidth = previousMaxWidth;
+ return result;
+ });
+
+ expect(layout).not.toBeNull();
+ expect(layout!.bar).toBeGreaterThan(150);
+ expect(layout!.funnel).toBeGreaterThan(150);
+ expect(layout!.range).toBeGreaterThan(150);
+ expect(layout!.noOverflow).toBe(true);
+ });
+
it("exposes each chart's data as a table for screen readers", async () => {
const tables = await browser.execute(() => {
const charts = Array.from(
diff --git a/apps/screenpipe-app-tauri/lib/chat/system-prompt.ts b/apps/screenpipe-app-tauri/lib/chat/system-prompt.ts
index b39d50fe4..dd8953caa 100644
--- a/apps/screenpipe-app-tauri/lib/chat/system-prompt.ts
+++ b/apps/screenpipe-app-tauri/lib/chat/system-prompt.ts
@@ -130,7 +130,7 @@ Reach for one when the answer is a comparison, trend, split, sequence, range, re
| bar | one amount per label, sorted high first | items[{label, value}] (20) |
| line | one measure over time — "is X trending?" | items[{label, value}] (60) |
| grouped_bar | series side by side | categories (12) + series[{name, values}] (5) |
-| stacked_bar | part-to-whole per category | same as grouped_bar |
+| stacked_bar | non-negative parts per category | same as grouped_bar |
| proportion | how one total splits up, non-negative | items[{label, value}] (5) |
| heatmap | two dimensions at once, e.g. daypart × weekday — prefer this over bar whenever the data has a row AND a column axis | x (24) + y (14) + values, one row per y |
| timeline | when things happened across a day | items[{label, start, end}] hours 0–24 (24) |
@@ -146,7 +146,7 @@ RULES:
- Values are real numbers, never strings or null
- \`title\` and \`unit\` are optional; unit is a short suffix like "min", "h", "%"
- Never send colors
-- Funnel values must be non-negative and never increase; calendar dates must be unique; range \`mid\` must sit between \`min\` and \`max\`
+- Funnel/stacked values: non-negative; funnel: never increase; calendar: unique dates within 84 days; range \`mid\`: between \`min\` and \`max\`
- Waterfall = start + signed changes; scatter = x and y per label. Don't use bar for either
- Each \`values\` array must be exactly as long as \`categories\`; each heatmap row as long as \`x\`
- A single number is a \`stat\`, never a one-bar bar chart