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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: hoist common params in stacked chart (#6994)
Myles Scolnickc6b359480420
diff --git a/frontend/package.json b/frontend/package.jsonindex 2376f69c51..3a5c013203 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -170,6 +170,7 @@"use-resize-observer": "^9.1.0","vega-lite": "^6.4.1","vega-loader": "^5.1.0",+ "vega-parser": "^7.1.0","vega-tooltip": "^1.1.0","vscode-jsonrpc": "^8.2.1","vscode-languageserver-protocol": "^3.17.5",diff --git a/frontend/src/components/data-table/charts/chart-spec/altair-generator.ts b/frontend/src/components/data-table/charts/chart-spec/altair-generator.tsindex 2a64c2eeef..2086786aca 100644--- a/frontend/src/components/data-table/charts/chart-spec/altair-generator.ts+++ b/frontend/src/components/data-table/charts/chart-spec/altair-generator.ts@@ -156,11 +156,10 @@ function makeKwargs<T extends Record<string, any>>(obj: T) {for (const [key, value] of Object.entries(obj)) {if (value !== undefined) {// Escape special characters in field names- if (key === "field" && typeof value === "string") {- result[key] = new Literal(escapeFieldName(value));- } else {- result[key] = new Literal(value);- }+ result[key] =+ key === "field" && typeof value === "string"+ ? new Literal(escapeFieldName(value))+ : new Literal(value);}}diff --git a/frontend/src/components/data-table/charts/chart-spec/utils.ts b/frontend/src/components/data-table/charts/chart-spec/utils.tsindex 0180ecb197..22e0d80bc7 100644--- a/frontend/src/components/data-table/charts/chart-spec/utils.ts+++ b/frontend/src/components/data-table/charts/chart-spec/utils.ts@@ -16,8 +16,8 @@ export function escapeFieldName(field: string | undefined): string | undefined {return field;}return field- .replace(/\./g, "\\.")- .replace(/\[/g, "\\[")- .replace(/\]/g, "\\]")- .replace(/:/g, "\\:");+ .replaceAll(".", "\\.")+ .replaceAll("[", "\\[")+ .replaceAll("]", "\\]")+ .replaceAll(":", "\\:");}diff --git a/frontend/src/components/ui/query-param-preserving-link.tsx b/frontend/src/components/ui/query-param-preserving-link.tsxindex 201e87f68d..9336bba33e 100644--- a/frontend/src/components/ui/query-param-preserving-link.tsx+++ b/frontend/src/components/ui/query-param-preserving-link.tsx@@ -1,4 +1,4 @@-/* Copyright 2025 Marimo. All rights reserved. */+/* Copyright 2024 Marimo. All rights reserved. */import React, { useRef } from "react";import { usePress } from "react-aria";diff --git a/frontend/src/plugins/impl/vega/__tests__/__snapshots__/make-selectable.test.ts.snap b/frontend/src/plugins/impl/vega/__tests__/__snapshots__/make-selectable.test.ts.snapindex 729fab95e8..28fbcb8fab 100644--- a/frontend/src/plugins/impl/vega/__tests__/__snapshots__/make-selectable.test.ts.snap+++ b/frontend/src/plugins/impl/vega/__tests__/__snapshots__/make-selectable.test.ts.snap@@ -434,6 +434,1719 @@ exports[`makeSelectable > should add legend selection to composite charts (issue}`;+exports[`makeSelectable > should add legend selection to hconcat specs 1`] = `+{+ "hconcat": [+ {+ "encoding": {+ "color": {+ "field": "series",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_series",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],+ },+ {+ "encoding": {+ "color": {+ "field": "series",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_series",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "line",+ },+ },+ ],+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_series",+ "select": {+ "fields": [+ "series",+ ],+ "type": "point",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should add legend selection to nested vconcat(hconcat(...)) specs 1`] = `+{+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_category",+ "select": {+ "fields": [+ "category",+ ],+ "type": "point",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+ "vconcat": [+ {+ "hconcat": [+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ },+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "nominal",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "bar",+ },+ },+ ],+ },+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "line",+ },+ },+ ],+}+`;++exports[`makeSelectable > should add legend selection to vconcat specs 1`] = `+{+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_category",+ "select": {+ "fields": [+ "category",+ ],+ "type": "point",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+ "vconcat": [+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],+ },+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "nominal",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "bar",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],+ },+ ],+}+`;++exports[`makeSelectable > should collect legend fields from multiple layers with different fields 1`] = `+{+ "layer": [+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "legend_selection_size_field",+ },+ {+ "param": "select_point_0",+ },+ {+ "param": "select_interval_0",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_category",+ "select": {+ "fields": [+ "category",+ ],+ "type": "point",+ },+ },+ {+ "bind": "legend",+ "name": "legend_selection_size_field",+ "select": {+ "fields": [+ "size_field",+ ],+ "type": "point",+ },+ },+ {+ "name": "select_point_0",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval_0",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+ },+ {+ "encoding": {+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "select_point_1",+ },+ {+ "param": "select_interval_1",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "size": {+ "field": "size_field",+ "type": "quantitative",+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ "params": [+ {+ "name": "select_point_1",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval_1",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],+ },+ ],+}+`;++exports[`makeSelectable > should handle concat with only one subspec 1`] = `+{+ "hconcat": [+ {+ "encoding": {+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "select_point",+ },+ {+ "param": "select_interval",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ },+ ],+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should handle empty concat specs gracefully 1`] = `+{+ "hconcat": [],+}+`;++exports[`makeSelectable > should hoist only common legend params when chart selections differ 1`] = `+{+ "hconcat": [+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],+ },+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "nominal",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "bar",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],+ },+ ],+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_category",+ "select": {+ "fields": [+ "category",+ ],+ "type": "point",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should hoist params to top level in hconcat(vconcat(...)) and apply opacity to nested specs 1`] = `+{+ "hconcat": [+ {+ "vconcat": [+ {+ "encoding": {+ "color": {+ "field": "Origin",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_Origin",+ },+ {+ "param": "select_point",+ },+ {+ "param": "select_interval",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "Horsepower",+ "type": "quantitative",+ },+ "y": {+ "field": "Miles_per_Gallon",+ "type": "quantitative",+ },+ },+ "height": 100,+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ },+ {+ "encoding": {+ "color": {+ "field": "Origin",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_Origin",+ },+ {+ "param": "select_point",+ },+ {+ "param": "select_interval",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "Horsepower",+ "type": "quantitative",+ },+ "y": {+ "field": "Miles_per_Gallon",+ "type": "quantitative",+ },+ },+ "height": 100,+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ },+ ],+ },+ {+ "encoding": {+ "color": {+ "field": "Origin",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_Origin",+ },+ {+ "param": "select_point",+ },+ {+ "param": "select_interval",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "Horsepower",+ "type": "quantitative",+ },+ "y": {+ "field": "Miles_per_Gallon",+ "type": "quantitative",+ },+ },+ "height": 100,+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ },+ ],+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_Origin",+ "select": {+ "fields": [+ "Origin",+ ],+ "type": "point",+ },+ },+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should not add bin_coloring when chartSelection is false 1`] = `+{+ "encoding": {+ "x": {+ "bin": true,+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "aggregate": "count",+ "type": "quantitative",+ },+ },+ "mark": "bar",+ "params": [+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should not duplicate legend params when multiple layers have same color field 1`] = `+{+ "layer": [+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "line",+ },+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_category",+ "select": {+ "fields": [+ "category",+ ],+ "type": "point",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+ },+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "select_point_1",+ },+ {+ "param": "select_interval_1",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "size": 100,+ "tooltip": true,+ "type": "point",+ },+ "params": [+ {+ "name": "select_point_1",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval_1",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],+ },+ ],+}+`;++exports[`makeSelectable > should not hoist params when specs have different encodings 1`] = `+{+ "hconcat": [+ {+ "encoding": {+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],+ },+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "theta": {+ "field": "value",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "arc",+ },+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_category",+ "select": {+ "fields": [+ "category",+ ],+ "type": "point",+ },+ },+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "color",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ ],+ },+ ],+ "params": [+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should not hoist params when specs have different selection types 1`] = `+{+ "hconcat": [+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "nominal",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "bar",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],+ },+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "area",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "color",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ ],+ },+ ],+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_category",+ "select": {+ "fields": [+ "category",+ ],+ "type": "point",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should only hoist common params with same select.type and select.encodings 1`] = `+{+ "hconcat": [+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "select_point",+ },+ {+ "param": "select_interval",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ },+ {+ "encoding": {+ "color": {+ "field": "category",+ "type": "nominal",+ },+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "legend_selection_category",+ },+ {+ "param": "select_point",+ },+ {+ "param": "select_interval",+ },+ {+ "param": "pan_zoom",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "field": "y",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ },+ ],+ "params": [+ {+ "bind": "legend",+ "name": "legend_selection_category",+ "select": {+ "fields": [+ "category",+ ],+ "type": "point",+ },+ },+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should prefer point selection for binned charts even when chartSelection is true 1`] = `+{+ "encoding": {+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "bin_coloring",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ "x": {+ "bin": {+ "maxbins": 20,+ },+ "field": "x",+ "type": "quantitative",+ },+ "y": {+ "aggregate": "count",+ "type": "quantitative",+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "bar",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "bin_coloring",+ "select": {+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;+exports[`makeSelectable > should return correctly if existing legend selection 1`] = `{"config": {@@ -869,6 +2582,168 @@ exports[`makeSelectable > should return correctly with multiple encodings 1`] =}`;+exports[`makeSelectable > should return the same spec for not-defined and true 1`] = `+{+ "encoding": {+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "select_point",+ },+ {+ "param": "select_interval",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should return the same spec if mark is boxplot 1`] = `+{+ "mark": "boxplot",+}+`;++exports[`makeSelectable > should return the same spec if mark is errorband 1`] = `+{+ "mark": "errorband",+}+`;++exports[`makeSelectable > should return the same spec if mark is errorbar 1`] = `+{+ "mark": "errorbar",+}+`;++exports[`makeSelectable > should return the same spec if mark is not in spec 1`] = `+{+ "encoding": {+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "select_point",+ },+ {+ "param": "select_interval",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },+ },+ "mark": {+ "cursor": "pointer",+ "tooltip": true,+ "type": "point",+ },+ "params": [+ {+ "name": "select_point",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ {+ "bind": "scales",+ "name": "pan_zoom",+ "select": {+ "on": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "translate": "[mousedown[event.metaKey], window:mouseup] > window:mousemove!",+ "type": "interval",+ "zoom": "wheel![event.metaKey]",+ },+ },+ ],+}+`;++exports[`makeSelectable > should return the same spec if selection is false 1`] = `+{+ "mark": "point",+}+`;+exports[`makeSelectable > should skip field selection if empty or false 1`] = `{"config": {@@ -1208,19 +3083,6 @@ exports[`makeSelectable > should work for layered charts, with existing selectio},"value": "lightgray",},- "opacity": {- "condition": {- "test": {- "and": [- {- "param": "legend_selection_stage",- },- ],- },- "value": 1,- },- "value": 0.2,- },"x": {"field": "Level1","sort": {@@ -1236,22 +3098,10 @@ exports[`makeSelectable > should work for layered charts, with existing selectio},},"mark": {- "cursor": "pointer",- "tooltip": true,"type": "bar",},"name": "view_21","params": [- {- "bind": "legend",- "name": "legend_selection_stage",- "select": {- "fields": [- "stage",- ],- "type": "point",- },- },{"bind": "scales","name": "pan_zoom",diff --git a/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts b/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.tsindex 3d439aa853..33abfcfde3 100644--- a/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts+++ b/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts@@ -1,5 +1,9 @@/* Copyright 2024 Marimo. All rights reserved. */++// @ts-expect-error - vega-parser is not typed+import { parse } from "vega-parser";import { describe, expect, it } from "vitest";+import { invariant } from "@/utils/invariant";import { makeSelectable } from "../make-selectable";import { getSelectionParamNames } from "../params";import type { VegaLiteSpec } from "../types";@@ -31,6 +35,8 @@ describe("makeSelectable", () => {});expect(newSpec).toEqual(spec);expect(getSelectionParamNames(newSpec)).toEqual([]);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();});it("should return the same spec for not-defined and true", () => {@@ -43,6 +49,7 @@ describe("makeSelectable", () => {fieldSelection: true,}),).toEqual(makeSelectable(spec, {}));+ expect(makeSelectable(spec, {})).toMatchSnapshot();});it("should return the same spec if mark is not in spec", () => {@@ -55,6 +62,7 @@ describe("makeSelectable", () => {},} as VegaLiteSpec;expect(makeSelectable(spec1, {})).toEqual(makeSelectable(spec2, {}));+ expect(makeSelectable(spec1, {})).toMatchSnapshot();});it("should return correctly if overlapping encodings", () => {@@ -88,6 +96,7 @@ describe("makeSelectable", () => {fieldSelection: true,});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();expect(getSelectionParamNames(newSpec)).toEqual(["legend_selection_Origin","select_point",@@ -128,6 +137,7 @@ describe("makeSelectable", () => {fieldSelection: false,});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();expect(getSelectionParamNames(newSpec)).toEqual(["select_point","select_interval",@@ -159,6 +169,7 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, {});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();expect(getSelectionParamNames(newSpec)).toEqual(["legend_selection_colorField","legend_selection_sizeField",@@ -213,6 +224,7 @@ describe("makeSelectable", () => {} as VegaLiteSpec;const newSpec = makeSelectable(spec, {});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();expect(getSelectionParamNames(newSpec)).toEqual(["param_1","select_point",@@ -288,6 +300,7 @@ describe("makeSelectable", () => {chartSelection: true,});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();expect(getSelectionParamNames(newSpec)).toEqual(["pan_zoom",@@ -350,6 +363,7 @@ describe("makeSelectable", () => {});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();expect(getSelectionParamNames(newSpec)).toEqual(["select_point_0","select_interval_0",@@ -381,6 +395,7 @@ describe("makeSelectable", () => {} as VegaLiteSpec;const newSpec = makeSelectable(spec, {});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();expect(getSelectionParamNames(newSpec)).toEqual([]);});@@ -465,6 +480,7 @@ describe("makeSelectable", () => {} as VegaLiteSpec;const newSpec = makeSelectable(spec, {});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();expect(getSelectionParamNames(newSpec)).toMatchInlineSnapshot(`["param_22",@@ -554,6 +570,7 @@ describe("makeSelectable", () => {} as VegaLiteSpec;const newSpec = makeSelectable(spec, {});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();expect(getSelectionParamNames(newSpec)).toMatchInlineSnapshot(`["param_22",@@ -570,6 +587,8 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, {});expect(newSpec).toEqual(spec);expect(getSelectionParamNames(newSpec)).toEqual([]);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();},);@@ -609,6 +628,7 @@ describe("makeSelectable", () => {});expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();const paramNames = getSelectionParamNames(newSpec);// Should have legend selection for category fieldexpect(paramNames).toContain("legend_selection_category");@@ -649,6 +669,8 @@ describe("makeSelectable", () => {);expect(legendParams).toHaveLength(1);expect(legendParams[0]).toBe("legend_selection_category");+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();});it("should add bin_coloring param for binned charts", () => {@@ -662,6 +684,7 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, { chartSelection: true });expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();const paramNames = getSelectionParamNames(newSpec);// Should have point selection and bin_coloring param@@ -683,6 +706,7 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, { chartSelection: true });expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();const paramNames = getSelectionParamNames(newSpec);// Should have point selection and bin_coloring param@@ -712,6 +736,7 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, { chartSelection: true });expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();const paramNames = getSelectionParamNames(newSpec);// First layer should have point selection and bin_coloring@@ -731,6 +756,9 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, { chartSelection: true });const paramNames = getSelectionParamNames(newSpec);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+// Should only have point selection for binned charts (not interval)expect(paramNames).toContain("select_point");expect(paramNames).not.toContain("select_interval");@@ -749,6 +777,9 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, { chartSelection: false });const paramNames = getSelectionParamNames(newSpec);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+// Should not have any chart selection paramsexpect(paramNames).not.toContain("select_point");expect(paramNames).not.toContain("bin_coloring");@@ -779,6 +810,9 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, {});const paramNames = getSelectionParamNames(newSpec);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+// Should have legend params for both fieldsexpect(paramNames).toContain("legend_selection_category");expect(paramNames).toContain("legend_selection_size_field");@@ -809,6 +843,9 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, {});const paramNames = getSelectionParamNames(newSpec);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+// Should have legend selection for category fieldexpect(paramNames).toContain("legend_selection_category");});@@ -838,6 +875,9 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, {});const paramNames = getSelectionParamNames(newSpec);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+// Should have legend selection for series fieldexpect(paramNames).toContain("legend_selection_series");});@@ -879,7 +919,278 @@ describe("makeSelectable", () => {const newSpec = makeSelectable(spec, {});const paramNames = getSelectionParamNames(newSpec);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+// Should have legend selection for category fieldexpect(paramNames).toContain("legend_selection_category");});++ it("should hoist params to top level in hconcat(vconcat(...)) and apply opacity to nested specs", () => {+ const spec = {+ hconcat: [+ {+ vconcat: [+ {+ mark: "point",+ encoding: {+ x: { field: "Horsepower", type: "quantitative" },+ y: { field: "Miles_per_Gallon", type: "quantitative" },+ color: { field: "Origin", type: "nominal" },+ },+ height: 100,+ },+ {+ mark: "point",+ encoding: {+ x: { field: "Horsepower", type: "quantitative" },+ y: { field: "Miles_per_Gallon", type: "quantitative" },+ color: { field: "Origin", type: "nominal" },+ },+ height: 100,+ },+ ],+ },+ {+ mark: "point",+ encoding: {+ x: { field: "Horsepower", type: "quantitative" },+ y: { field: "Miles_per_Gallon", type: "quantitative" },+ color: { field: "Origin", type: "nominal" },+ },+ height: 100,+ },+ ],+ } as VegaLiteSpec;++ const newSpec = makeSelectable(spec, {});++ // Params should be hoisted to top level+ expect(newSpec.params).toBeDefined();+ expect(newSpec.params!.length).toBeGreaterThan(0);++ // Should have legend selection for Origin field+ const paramNames = getSelectionParamNames(newSpec);+ expect(paramNames).toContain("legend_selection_Origin");++ // Nested specs should NOT have params (they should be hoisted)+ if ("hconcat" in newSpec) {+ for (const subSpec of newSpec.hconcat) {+ if ("vconcat" in subSpec) {+ invariant("vconcat" in subSpec, "subSpec should have vconcat");+ for (const innerSpec of subSpec.vconcat) {+ expect("params" in innerSpec).toBe(false);+ // But should have opacity encoding+ if ("mark" in innerSpec) {+ expect(innerSpec.encoding?.opacity).toBeDefined();+ }+ }+ } else if ("mark" in subSpec) {+ expect(subSpec.params).toBeUndefined();+ // But should have opacity encoding+ expect(subSpec.encoding?.opacity).toBeDefined();+ }+ }+ }+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+ });++ it("should only hoist common params with same select.type and select.encodings", () => {+ // All point charts with x,y encodings - should hoist point/interval params+ const spec = {+ hconcat: [+ {+ mark: "point",+ encoding: {+ x: { field: "x", type: "quantitative" },+ y: { field: "y", type: "quantitative" },+ color: { field: "category", type: "nominal" },+ },+ },+ {+ mark: "point",+ encoding: {+ x: { field: "x", type: "quantitative" },+ y: { field: "y", type: "quantitative" },+ color: { field: "category", type: "nominal" },+ },+ },+ ],+ } as VegaLiteSpec;++ const newSpec = makeSelectable(spec, {});++ // Common params should be hoisted+ expect(newSpec.params).toBeDefined();+ const topLevelParamNames = getSelectionParamNames(newSpec);++ // Legend param should be hoisted (common across all)+ expect(topLevelParamNames).toContain("legend_selection_category");++ // Point and interval params should be hoisted (same type and encodings)+ expect(topLevelParamNames).toContain("select_point");+ expect(topLevelParamNames).toContain("select_interval");++ // Nested specs should not have params+ if ("hconcat" in newSpec) {+ for (const subSpec of newSpec.hconcat) {+ expect("params" in subSpec).toBe(false);+ }+ }+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+ });++ it("should not hoist params when specs have different selection types", () => {+ // One bar chart (point+interval) and one area chart (point only) - different selection strategies+ const spec = {+ hconcat: [+ {+ mark: "bar",+ encoding: {+ x: { field: "x", type: "nominal" },+ y: { field: "y", type: "quantitative" },+ color: { field: "category", type: "nominal" },+ },+ },+ {+ mark: "area",+ encoding: {+ x: { field: "x", type: "quantitative" },+ y: { field: "y", type: "quantitative" },+ color: { field: "category", type: "nominal" },+ },+ },+ ],+ } as VegaLiteSpec;++ const newSpec = makeSelectable(spec, {});++ // Legend param should be hoisted (common across all)+ const topLevelParamNames = getSelectionParamNames(newSpec);+ expect(topLevelParamNames).toContain("legend_selection_category");++ // But chart selection params should NOT be hoisted (different types)+ // Bar gets point+interval with x encoding, area gets point with color encoding+ if ("hconcat" in newSpec) {+ const subspecParams = newSpec.hconcat.flatMap((subSpec) =>+ "params" in subSpec ? subSpec.params : [],+ );+ expect(subspecParams.length).toBeGreaterThan(0);+ }+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+ });++ it("should not hoist params when specs have different encodings", () => {+ // One chart with x,y selection and one with color selection+ const spec = {+ hconcat: [+ {+ mark: "point",+ encoding: {+ x: { field: "x", type: "quantitative" },+ y: { field: "y", type: "quantitative" },+ },+ },+ {+ mark: "arc",+ encoding: {+ theta: { field: "value", type: "quantitative" },+ color: { field: "category", type: "nominal" },+ },+ },+ ],+ } as VegaLiteSpec;++ const newSpec = makeSelectable(spec, {});++ // Chart selection params should NOT be hoisted (different encodings)+ // Point chart uses x,y encodings, arc chart uses color encoding+ if ("hconcat" in newSpec) {+ const subspecParams = newSpec.hconcat.flatMap((subSpec) =>+ "params" in subSpec ? subSpec.params : [],+ );+ // Both subspecs should have their own params+ expect(subspecParams.length).toBeGreaterThan(0);+ }+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+ });++ it("should hoist only common legend params when chart selections differ", () => {+ const spec = {+ hconcat: [+ {+ mark: "point",+ encoding: {+ x: { field: "x", type: "quantitative" },+ y: { field: "y", type: "quantitative" },+ color: { field: "category", type: "nominal" },+ },+ },+ {+ mark: "bar",+ encoding: {+ x: { field: "x", type: "nominal" },+ y: { field: "y", type: "quantitative" },+ color: { field: "category", type: "nominal" },+ },+ },+ ],+ } as VegaLiteSpec;++ const newSpec = makeSelectable(spec, {});++ // Common legend param should be hoisted+ const topLevelParamNames = getSelectionParamNames(newSpec);+ expect(topLevelParamNames).toContain("legend_selection_category");++ // But chart-specific params may or may not be hoisted depending on if they match+ // The point chart has x,y encodings, bar has x encoding - these differ+ if ("hconcat" in newSpec) {+ const subspecParams = newSpec.hconcat.flatMap((subSpec) =>+ "params" in subSpec ? subSpec.params : [],+ );+ // Each subspec should have its own chart selection params+ expect(subspecParams.length).toBeGreaterThan(0);+ }+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+ });++ it("should handle empty concat specs gracefully", () => {+ const spec = {+ hconcat: [],+ } as VegaLiteSpec;++ const newSpec = makeSelectable(spec, {});+ expect(newSpec).toEqual(spec);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+ });++ it("should handle concat with only one subspec", () => {+ const spec = {+ hconcat: [+ {+ mark: "point",+ encoding: {+ x: { field: "x", type: "quantitative" },+ y: { field: "y", type: "quantitative" },+ },+ },+ ],+ } as VegaLiteSpec;++ const newSpec = makeSelectable(spec, {});++ // Should hoist params even with single subspec+ expect(newSpec.params).toBeDefined();+ const topLevelParamNames = getSelectionParamNames(newSpec);+ expect(topLevelParamNames.length).toBeGreaterThan(0);+ expect(newSpec).toMatchSnapshot();+ expect(parse(newSpec)).toBeDefined();+ });});diff --git a/frontend/src/plugins/impl/vega/make-selectable.ts b/frontend/src/plugins/impl/vega/make-selectable.tsindex 5e2a5129fc..902a7df5a0 100644--- a/frontend/src/plugins/impl/vega/make-selectable.ts+++ b/frontend/src/plugins/impl/vega/make-selectable.ts@@ -1,16 +1,283 @@/* Copyright 2024 Marimo. All rights reserved. */+import type { TopLevelSpec } from "vega-lite";+import type { NonNormalizedSpec } from "vega-lite/types_unstable/spec/index.js";+import type { TopLevelParameter } from "vega-lite/types_unstable/spec/toplevel.js";+import { findEncodedFields, makeEncodingInteractive } from "./encodings";import { Marks } from "./marks";import { getBinnedFields, Params } from "./params";import type {+ Field,GenericVegaSpec,+ LayerSpec,Mark,+ SelectionParameter,SelectionType,+ UnitSpec,VegaLiteSpec,VegaLiteUnitSpec,} from "./types";+/**+ * Creates a unique signature for a param based on its select properties.+ * Params with the same signature are considered "common" candidates for hoisting.+ */+function getParamSignature(param: TParams): string {+ // For non-selection params, use the whole param as signature+ if (!("select" in param) || !param.select) {+ return JSON.stringify(param);+ }++ const select = param.select;++ // Handle string selections (type shortcuts)+ if (typeof select === "string") {+ return JSON.stringify({ type: select, bind: param.bind });+ }++ const signature = {+ type: select.type,+ encodings:+ "encodings" in select && select.encodings+ ? [...select.encodings].sort()+ : undefined,+ fields:+ "fields" in select && select.fields+ ? [...select.fields].sort()+ : undefined,+ bind: param.bind,+ };++ return JSON.stringify(signature);+}++/**+ * Recursively collects all params from nested specs and hoists ONLY common params to the top level.+ * Common params are those with the same select.type, select.encodings, select.fields, and bind+ * that appear in ALL unit specs.+ */+function hoistParamsAndApplyOpacity<T extends VegaLiteSpec>(spec: T): T {+ // Collect params from all unit specs+ const allUnitSpecParams = collectAllUnitSpecParams(spec);++ if (allUnitSpecParams.length === 0) {+ return spec;+ }++ // Find common params (params with same signature that appear in all unit specs)+ const commonParams = findCommonParams(allUnitSpecParams);++ if (commonParams.length === 0) {+ return spec;+ }++ // Remove only common params from nested specs+ const commonParamNames = new Set(commonParams.map((p) => p.name));+ const specWithoutCommonParams = removeSpecificParamsFromNestedSpecs(+ spec,+ commonParamNames,+ );++ // Apply opacity to all nested unit specs based on hoisted params+ const specWithOpacity = applyOpacityToNestedSpecs(+ specWithoutCommonParams,+ commonParams.map((p) => p.name),+ );++ // Add common params to top level+ return {+ ...specWithOpacity,+ params: [...(spec.params || []), ...commonParams],+ } as T;+}++type SpecWithParams =+ | TopLevelSpec+ | LayerSpec<Field>+ | UnitSpec<Field>+ | NonNormalizedSpec;++type TParams = SelectionParameter | TopLevelParameter;++/**+ * Collects params from all unit specs (not concat specs).+ * Returns array of { params } for each unit spec.+ */+function collectAllUnitSpecParams(spec: SpecWithParams): {+ params: TParams[];+}[] {+ const results: { params: TParams[] }[] = [];++ if ("vconcat" in spec && Array.isArray(spec.vconcat)) {+ for (const subSpec of spec.vconcat) {+ results.push(...collectAllUnitSpecParams(subSpec));+ }+ } else if ("hconcat" in spec && Array.isArray(spec.hconcat)) {+ for (const subSpec of spec.hconcat) {+ results.push(...collectAllUnitSpecParams(subSpec));+ }+ } else if ("layer" in spec) {+ // Don't collect from layers, as they handle their own params+ return [];+ } else if (+ "mark" in spec && // This is a unit spec+ "params" in spec &&+ spec.params &&+ spec.params.length > 0+ ) {+ results.push({ params: spec.params });+ }++ return results;+}++/**+ * Finds params that are common across all unit specs.+ * A param is common if it has the same signature and appears in all unit specs.+ */+function findCommonParams(+ allUnitSpecParams: { params: TParams[] }[],+): TParams[] {+ if (allUnitSpecParams.length === 0) {+ return [];+ }++ // Count occurrences of each param signature+ const signatureCounts = new Map<string, { count: number; param: TParams }>();+ const totalUnitSpecs = allUnitSpecParams.length;++ for (const { params } of allUnitSpecParams) {+ const seenSignatures = new Set<string>();++ for (const param of params) {+ const signature = getParamSignature(param);++ // Only count once per unit spec (avoid duplicates within same spec)+ if (!seenSignatures.has(signature)) {+ seenSignatures.add(signature);++ if (!signatureCounts.has(signature)) {+ signatureCounts.set(signature, { count: 0, param });+ }+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion+ signatureCounts.get(signature)!.count++;+ }+ }+ }++ // Return params that appear in ALL unit specs+ const commonParams: TParams[] = [];+ for (const [, { count, param }] of signatureCounts) {+ if (count === totalUnitSpecs) {+ commonParams.push(param);+ }+ }++ return commonParams;+}++/**+ * Recursively removes specific params (by name) from nested specs.+ */+function removeSpecificParamsFromNestedSpecs(+ spec: SpecWithParams,+ paramNamesToRemove: Set<string>,+): SpecWithParams {+ if ("vconcat" in spec && Array.isArray(spec.vconcat)) {+ return {+ ...spec,+ vconcat: spec.vconcat.map((subSpec) =>+ removeSpecificParamsFromNestedSpecs(subSpec, paramNamesToRemove),+ ),+ } as SpecWithParams;+ }++ if ("hconcat" in spec && Array.isArray(spec.hconcat)) {+ return {+ ...spec,+ hconcat: spec.hconcat.map((subSpec) =>+ removeSpecificParamsFromNestedSpecs(subSpec, paramNamesToRemove),+ ),+ } as SpecWithParams;+ }++ if ("mark" in spec && "params" in spec && spec.params) {+ // This is a unit spec, filter out params that should be hoisted+ const currentParams = spec.params;+ const filteredParams: TParams[] = [];++ for (const param of currentParams) {+ if (!param || typeof param !== "object" || !("name" in param)) {+ filteredParams.push(param);+ continue;+ }+ if (!paramNamesToRemove.has(param.name)) {+ filteredParams.push(param);+ }+ }++ if (filteredParams.length === 0) {+ const { params, ...rest } = spec;+ return rest as SpecWithParams;+ }++ return {+ ...spec,+ params: filteredParams,+ } as SpecWithParams;+ }++ return spec;+}++/**+ * Recursively applies opacity encoding to all unit specs based on param names.+ */+function applyOpacityToNestedSpecs<T extends SpecWithParams>(+ spec: T,+ paramNames: string[],+): T {+ if ("vconcat" in spec && Array.isArray(spec.vconcat)) {+ return {+ ...spec,+ vconcat: spec.vconcat.map((subSpec) =>+ applyOpacityToNestedSpecs(subSpec, paramNames),+ ),+ };+ }++ if ("hconcat" in spec && Array.isArray(spec.hconcat)) {+ return {+ ...spec,+ hconcat: spec.hconcat.map((subSpec) =>+ applyOpacityToNestedSpecs(subSpec, paramNames),+ ),+ };+ }++ if ("layer" in spec) {+ // Don't apply to layers, they handle their own opacity+ return spec;+ }++ if ("mark" in spec && Marks.isInteractive(spec.mark)) {+ // This is a unit spec, apply opacity+ return {+ ...spec,+ mark: Marks.makeClickable(spec.mark),+ encoding: makeEncodingInteractive(+ "opacity",+ spec.encoding || {},+ paramNames,+ spec.mark,+ ),+ };+ }++ return spec;+}+export function makeSelectable<T extends VegaLiteSpec>(spec: T,opts: {@@ -45,8 +312,8 @@ export function makeSelectable<T extends VegaLiteSpec>(}): subSpec,);- // No pan/zoom for vconcat- return { ...spec, vconcat: subSpecs };+ // Hoist params to top level and apply opacity to all nested specs+ return hoistParamsAndApplyOpacity({ ...spec, vconcat: subSpecs });}if ("hconcat" in spec) {@@ -58,17 +325,16 @@ export function makeSelectable<T extends VegaLiteSpec>(}): subSpec,);- // No pan/zoom for hconcat- return { ...spec, hconcat: subSpecs };+ // Hoist params to top level and apply opacity to all nested specs+ return hoistParamsAndApplyOpacity({ ...spec, hconcat: subSpecs });}if ("layer" in spec) {- // Check if legend params already exist at the top level- const hasTopLevelLegendParam = spec.params?.some(- (param) => param.bind === "legend",- );+ // Check if has top-level params already (not just legend). If so, we don't add any more as it+ // will cause conflicts.+ const hasTopLevelParams = spec.params && spec.params.length > 0;const shouldAddLegendSelection =- fieldSelection !== false && !hasTopLevelLegendParam;+ fieldSelection !== false && !hasTopLevelParams;// Collect all unique legend fields from all layers to avoid duplicateslet legendFields: string[] = [];@@ -95,7 +361,7 @@ export function makeSelectable<T extends VegaLiteSpec>(}let resolvedSpec = subSpec as VegaLiteUnitSpec;- // Only add legend params to the first layer to avoid duplicates+ // Only add legend params to firstif (idx === 0 && legendFields.length > 0) {const legendParams = legendFields.map((field) => Params.legend(field));resolvedSpec = {@@ -221,10 +487,8 @@ function makeChartSelectable(// 1. The regular selection param (point/interval) - sends signals to backend for filtering// 2. The bin_coloring param - controls opacity/coloring, NO signal listener// This separation allows us to filter on binned ranges while providing visual feedback- if (binnedFields.length > 0) {- if (resolvedChartSelection.includes("point")) {- nextParams.push(Params.binColoring(layerNum));- }+ if (binnedFields.length > 0 && resolvedChartSelection.includes("point")) {+ nextParams.push(Params.binColoring(layerNum));}return {diff --git a/frontend/src/plugins/impl/vega/params.ts b/frontend/src/plugins/impl/vega/params.tsindex 0ea704b31a..779fd4d40c 100644--- a/frontend/src/plugins/impl/vega/params.ts+++ b/frontend/src/plugins/impl/vega/params.ts@@ -1,4 +1,6 @@/* Copyright 2024 Marimo. All rights reserved. */++import { uniq } from "lodash-es";import type { TopLevelSpec } from "vega-lite";import type { NonNormalizedSpec } from "vega-lite/types_unstable/spec/index.js";import { Marks } from "./marks";@@ -167,27 +169,26 @@ export function getSelectionParamNames(): string[] {if ("params" in spec && spec.params && spec.params.length > 0) {const params = spec.params;- return (- params+ const paramNames = params+ // @ts-expect-error TS doesn't know that `param` is an object+ .filter((param) => {+ if (param == null) {+ return false;+ }// @ts-expect-error TS doesn't know that `param` is an object- .filter((param) => {- if (param == null) {- return false;- }- // @ts-expect-error TS doesn't know that `param` is an object- return "select" in param && param.select !== undefined;- })- .map((param) => param.name)- );+ return "select" in param && param.select !== undefined;+ })+ .map((param) => param.name);+ return uniq(paramNames);}if ("layer" in spec) {- return [...new Set(spec.layer.flatMap(getSelectionParamNames))];+ return uniq(spec.layer.flatMap(getSelectionParamNames));}if ("vconcat" in spec) {- return [...new Set(spec.vconcat.flatMap(getSelectionParamNames))];+ return uniq(spec.vconcat.flatMap(getSelectionParamNames));}if ("hconcat" in spec) {- return [...new Set(spec.hconcat.flatMap(getSelectionParamNames))];+ return uniq(spec.hconcat.flatMap(getSelectionParamNames));}return [];}@@ -236,16 +237,16 @@ export function getBinnedFields(spec: VegaLiteUnitSpec): string[] {const fields: string[] = [];for (const channel of Object.values(spec.encoding)) {- if (channel && typeof channel === "object") {- // Check for binning- if (- "bin" in channel &&- channel.bin &&- "field" in channel &&- typeof channel.field === "string"- ) {- fields.push(channel.field);- }+ // Check for binning+ if (+ channel &&+ typeof channel === "object" &&+ "bin" in channel &&+ channel.bin &&+ "field" in channel &&+ typeof channel.field === "string"+ ) {+ fields.push(channel.field);}}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 63413f663d..db69807c0d 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -496,6 +496,9 @@ importers:vega-loader:specifier: ^5.1.0version: 5.1.0+ vega-parser:+ specifier: ^7.1.0+ version: 7.1.0vega-tooltip:specifier: ^1.1.0version: 1.1.0