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.

fix: Update opacity for interactive selection to be a ratio of user defined opacity (#350)

Myles Scolnick898e151c87fd
diff --git a/frontend/src/plugins/impl/vega/__tests__/encodings.test.ts b/frontend/src/plugins/impl/vega/__tests__/encodings.test.ts
index 6f5ddda65f..e790d03927 100644
--- a/frontend/src/plugins/impl/vega/__tests__/encodings.test.ts
+++ b/frontend/src/plugins/impl/vega/__tests__/encodings.test.ts
@@ -70,19 +70,6 @@ describe("findEncodedFields", () => {
});
describe("makeEncodingInteractive", () => {
- it("should return the original encodings when key is not in the allowed list", () => {
- const encodings: Encodings = {
- x: {
- field: "someField",
- type: "quantitative",
- },
- };
-
- expect(makeEncodingInteractive("x", encodings, ["param1"])).toEqual(
- encodings
- );
- });
-
it.each([
"color",
"fill",
@@ -111,9 +98,45 @@ describe("makeEncodingInteractive", () => {
},
};
- expect(makeEncodingInteractive(type, encodings, ["param1"])).toEqual(
- expected
- );
+ expect(
+ makeEncodingInteractive("opacity", encodings, ["param1"], "point")
+ ).toEqual(expected);
+ });
+
+ it("should use a factor of the given opacity", () => {
+ const encodings: Encodings = {
+ x: {
+ field: "someField",
+ type: "quantitative",
+ },
+ };
+
+ expect(
+ makeEncodingInteractive("opacity", encodings, ["param1"], {
+ type: "point",
+ opacity: 0.6,
+ })
+ ).toMatchInlineSnapshot(`
+ {
+ "opacity": {
+ "condition": {
+ "test": {
+ "and": [
+ {
+ "param": "param1",
+ },
+ ],
+ },
+ "value": 0.6,
+ },
+ "value": 0.12,
+ },
+ "x": {
+ "field": "someField",
+ "type": "quantitative",
+ },
+ }
+ `);
});
it("should return updated encodings for multiple parameters", () => {
@@ -138,7 +161,12 @@ describe("makeEncodingInteractive", () => {
};
expect(
- makeEncodingInteractive("color", encodings, ["param1", "param2"])
+ makeEncodingInteractive(
+ "opacity",
+ encodings,
+ ["param1", "param2"],
+ "point"
+ )
).toEqual(expected);
});
});
diff --git a/frontend/src/plugins/impl/vega/encodings.ts b/frontend/src/plugins/impl/vega/encodings.ts
index 8548fc731c..b98458e5cf 100644
--- a/frontend/src/plugins/impl/vega/encodings.ts
+++ b/frontend/src/plugins/impl/vega/encodings.ts
@@ -1,8 +1,11 @@
/* Copyright 2023 Marimo. All rights reserved. */
+import { Marks } from "./marks";
import {
+ AnyMark,
EncodingType,
Encodings,
Field,
+ MarkDef,
SharedCompositeEncoding,
VegaLiteSpec,
} from "./types";
@@ -76,9 +79,10 @@ const ALLOWED_ENCODING_TYPES = new Set<EncodingType>([
]);
export function makeEncodingInteractive(
- key: EncodingType,
+ key: "opacity",
encodings: SharedCompositeEncoding<Field>,
- paramNames: string[]
+ paramNames: string[],
+ mark: AnyMark | undefined
): SharedCompositeEncoding<Field> {
const test = {
and: paramNames.map((paramName) => ({
@@ -87,23 +91,20 @@ export function makeEncodingInteractive(
};
switch (key) {
- // For most encodings, we want to update the opacity of the mark
- case "color":
- case "fill":
- case "fillOpacity":
- case "opacity":
- case "shape":
- case "size":
+ // As of now, we only update opacity to signal selection
+ case "opacity": {
+ const initialOpacity = Marks.getOpacity(mark as MarkDef) || 1;
return {
...encodings,
opacity: {
condition: {
test: test,
- value: 1,
+ value: initialOpacity,
},
- value: 0.2,
+ value: initialOpacity / 5, // 20% opacity
},
};
+ }
default:
return encodings;
}
diff --git a/frontend/src/plugins/impl/vega/make-selectable.ts b/frontend/src/plugins/impl/vega/make-selectable.ts
index 9263c955eb..a0ca9bf317 100644
--- a/frontend/src/plugins/impl/vega/make-selectable.ts
+++ b/frontend/src/plugins/impl/vega/make-selectable.ts
@@ -127,7 +127,8 @@ function makeChartInteractive<T extends GenericVegaSpec>(spec: T): T {
encoding: makeEncodingInteractive(
"opacity",
prevEncodings || {},
- paramNames
+ paramNames,
+ spec.mark
),
};
}
diff --git a/frontend/src/plugins/impl/vega/marks.ts b/frontend/src/plugins/impl/vega/marks.ts
index e39b9b1dab..5c2e3bf841 100644
--- a/frontend/src/plugins/impl/vega/marks.ts
+++ b/frontend/src/plugins/impl/vega/marks.ts
@@ -18,4 +18,13 @@ export const Marks = {
}
return mark;
},
+ getOpacity(mark: AnyMark): number | null {
+ if (typeof mark === "string") {
+ return null;
+ }
+ if ("opacity" in mark && typeof mark.opacity === "number") {
+ return mark.opacity;
+ }
+ return null;
+ },
};
diff --git a/marimo/_smoke_tests/altair_charts.py b/marimo/_smoke_tests/altair_charts.py
index efddb78d7b..c9d918d1ed 100644
--- a/marimo/_smoke_tests/altair_charts.py
+++ b/marimo/_smoke_tests/altair_charts.py
@@ -149,7 +149,7 @@ def __(alt, chart_selection_value, iris, legend_selection_value, mo):
_chart = (
alt.Chart(iris)
- .mark_circle()
+ .mark_circle(opacity=0.7)
.encode(
alt.X("sepalLength", scale=alt.Scale(zero=False)),
alt.Y("sepalWidth", scale=alt.Scale(zero=False, padding=1)),