Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat(plotly): add reactive area chart selection support (#9046)

axssel78489d4621dc
diff --git a/examples/third_party/plotly/area_chart.py b/examples/third_party/plotly/area_chart.py
index 8ef321a327..dc5bb6b065 100644
--- a/examples/third_party/plotly/area_chart.py
+++ b/examples/third_party/plotly/area_chart.py
@@ -1,5 +1,5 @@
# /// script
-# requires-python = ">=3.9"
+# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# "pandas==2.3.3",
diff --git a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
index 419351900d..e65bc1f081 100644
--- a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
+++ b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
@@ -19,6 +19,7 @@ import {
extractPoints,
extractSunburstPoints,
extractTreemapPoints,
+ hasAreaTrace,
hasPureLineTrace,
lineSelectionButtons,
type ModeBarButton,
@@ -113,7 +114,8 @@ export const PlotlyComponent = memo(
const configMemo = useDeepCompareMemoize(config);
const plotlyConfig = useMemo((): Partial<Plotly.Config> => {
- const hasPureLine = hasPureLineTrace(figure.data);
+ const hasLineOrAreaTrace =
+ hasPureLineTrace(figure.data) || hasAreaTrace(figure.data);
const defaultButtons: ModeBarButton[] = [
// Custom button to reset the state
{
@@ -130,7 +132,7 @@ export const PlotlyComponent = memo(
click: handleResetWithClear,
},
];
- if (hasPureLine) {
+ if (hasLineOrAreaTrace) {
defaultButtons.push(...lineSelectionButtons(handleSetDragmode));
}
diff --git a/frontend/src/plugins/impl/plotly/__tests__/selection.test.ts b/frontend/src/plugins/impl/plotly/__tests__/selection.test.ts
index e4867b321f..f9cdb3f02f 100644
--- a/frontend/src/plugins/impl/plotly/__tests__/selection.test.ts
+++ b/frontend/src/plugins/impl/plotly/__tests__/selection.test.ts
@@ -5,6 +5,7 @@ import { describe, expect, it, vi } from "vitest";
import {
extractIndices,
extractPoints,
+ hasAreaTrace,
hasPureLineTrace,
lineSelectionButtons,
type ModeBarButton,
@@ -227,3 +228,67 @@ describe("extractPoints", () => {
]);
});
});
+
+describe("hasAreaTrace", () => {
+ it("detects scatter trace with tozeroy fill", () => {
+ expect(
+ hasAreaTrace([createTrace({ type: "scatter", fill: "tozeroy" })]),
+ ).toBe(true);
+ });
+
+ it("detects scatter trace with tonexty fill", () => {
+ expect(
+ hasAreaTrace([createTrace({ type: "scatter", fill: "tonexty" })]),
+ ).toBe(true);
+ });
+
+ it("detects scatter trace with stackgroup (px.area pattern)", () => {
+ expect(
+ hasAreaTrace([
+ createTrace({ type: "scatter", mode: "lines", stackgroup: "one" }),
+ ]),
+ ).toBe(true);
+ });
+
+ it("detects area traces with mode=none (fill-only, no visible line)", () => {
+ expect(
+ hasAreaTrace([
+ createTrace({ type: "scatter", fill: "tozeroy", mode: "none" }),
+ ]),
+ ).toBe(true);
+ });
+
+ it("ignores scatter traces with no fill and no stackgroup", () => {
+ expect(
+ hasAreaTrace([
+ createTrace({ type: "scatter", mode: "lines" }),
+ createTrace({ type: "scatter", mode: "markers" }),
+ ]),
+ ).toBe(false);
+ });
+
+ it("ignores scatter traces with fill=none", () => {
+ expect(hasAreaTrace([createTrace({ type: "scatter", fill: "none" })])).toBe(
+ false,
+ );
+ });
+
+ it("ignores scatter traces with fill=empty string", () => {
+ expect(
+ hasAreaTrace([createTrace({ type: "scatter", fill: "" as "none" })]),
+ ).toBe(false);
+ });
+
+ it("ignores non-scatter traces", () => {
+ expect(
+ hasAreaTrace([
+ createTrace({ type: "bar" }),
+ createTrace({ type: "heatmap" }),
+ ]),
+ ).toBe(false);
+ });
+
+ it("returns false for undefined data", () => {
+ expect(hasAreaTrace(undefined)).toBe(false);
+ });
+});
diff --git a/frontend/src/plugins/impl/plotly/selection.ts b/frontend/src/plugins/impl/plotly/selection.ts
index 1757b31906..853e583a09 100644
--- a/frontend/src/plugins/impl/plotly/selection.ts
+++ b/frontend/src/plugins/impl/plotly/selection.ts
@@ -141,13 +141,44 @@ export function hasPureLineTrace(
}
return data.some((trace) => {
- const traceType = (trace as { type?: unknown }).type;
+ const t = trace as Record<string, unknown>;
const isScatterLike =
- traceType === undefined || LINE_CLICK_TRACE_TYPES.has(String(traceType));
+ t.type === undefined || LINE_CLICK_TRACE_TYPES.has(String(t.type));
if (!isScatterLike) {
return false;
}
- return isPureLineMode((trace as { mode?: unknown }).mode);
+ return isPureLineMode(t.mode);
+ });
+}
+
+/**
+ * Return true when any scatter/scattergl trace has a non-empty fill or a
+ * stackgroup, i.e. it is an area chart.
+ *
+ * Area traces built with `mode="none"` have no visible line or markers, so
+ * `hasPureLineTrace` returns false for them even though they need select/lasso
+ * buttons just as much as `mode="lines"` area charts. This function covers
+ * that gap and is OR-ed with `hasPureLineTrace` in the config builder.
+ */
+export function hasAreaTrace(
+ data: readonly Plotly.Data[] | undefined,
+): boolean {
+ if (!data) {
+ return false;
+ }
+
+ return data.some((trace) => {
+ const t = trace as Record<string, unknown>;
+ // Only scatter/scattergl can be area traces.
+ if (t.type !== undefined && !LINE_CLICK_TRACE_TYPES.has(String(t.type))) {
+ return false;
+ }
+ // A trace is an area trace when fill is a non-empty string other than
+ // "none", OR it belongs to a stackgroup (px.area always sets stackgroup).
+ return (
+ (typeof t.fill === "string" && t.fill !== "" && t.fill !== "none") ||
+ t.stackgroup != null
+ );
});
}