Skip to content

Commit

Permalink
fix(#9401): conditional opacity should respect default value (#9402)
Browse files Browse the repository at this point in the history
## PR Description

Conditional opacity should respect default value


Before:

<img width="854" alt="image"
src="https://github.com/user-attachments/assets/07f590c3-a293-4e08-9d0d-d3b1dd2e15ef">

`config.point.opacity = 0.5` is ignored

After: 

<img width="794" alt="image"
src="https://github.com/user-attachments/assets/78e5b3ad-aa0f-40a2-8fe7-58f68a551fe7">

---------

Co-authored-by: GitHub Actions Bot <vega-actions-bot@users.noreply.github.com>
  • Loading branch information
kanitw and GitHub Actions Bot committed Jul 31, 2024
1 parent 8bad9d2 commit b4c7e14
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 3 deletions.
Binary file added examples/compiled/point_conditional_opacity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/compiled/point_conditional_opacity.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 113 additions & 0 deletions examples/compiled/point_conditional_opacity.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"background": "white",
"padding": 5,
"width": 200,
"height": 200,
"style": "cell",
"data": [
{
"name": "source_0",
"url": "data/cars.json",
"format": {"type": "json"},
"transform": [
{
"type": "filter",
"expr": "isValid(datum[\"Horsepower\"]) && isFinite(+datum[\"Horsepower\"]) && isValid(datum[\"Miles_per_Gallon\"]) && isFinite(+datum[\"Miles_per_Gallon\"])"
}
]
}
],
"marks": [
{
"name": "marks",
"type": "symbol",
"style": ["point"],
"from": {"data": "source_0"},
"encode": {
"update": {
"fill": {"value": "transparent"},
"stroke": {"value": "#4c78a8"},
"opacity": [
{"test": "datum.Horsepower > 100", "value": 0.25},
{"value": 0.5}
],
"ariaRoleDescription": {"value": "point"},
"description": {
"signal": "\"Horsepower: \" + (format(datum[\"Horsepower\"], \"\")) + \"; Miles_per_Gallon: \" + (format(datum[\"Miles_per_Gallon\"], \"\"))"
},
"x": {"scale": "x", "field": "Horsepower"},
"y": {"scale": "y", "field": "Miles_per_Gallon"}
}
}
}
],
"scales": [
{
"name": "x",
"type": "linear",
"domain": {"data": "source_0", "field": "Horsepower"},
"range": [0, {"signal": "width"}],
"nice": true,
"zero": true
},
{
"name": "y",
"type": "linear",
"domain": {"data": "source_0", "field": "Miles_per_Gallon"},
"range": [{"signal": "height"}, 0],
"nice": true,
"zero": true
}
],
"axes": [
{
"scale": "x",
"orient": "bottom",
"gridScale": "y",
"grid": true,
"tickCount": {"signal": "ceil(width/40)"},
"domain": false,
"labels": false,
"aria": false,
"maxExtent": 0,
"minExtent": 0,
"ticks": false,
"zindex": 0
},
{
"scale": "y",
"orient": "left",
"gridScale": "x",
"grid": true,
"tickCount": {"signal": "ceil(height/40)"},
"domain": false,
"labels": false,
"aria": false,
"maxExtent": 0,
"minExtent": 0,
"ticks": false,
"zindex": 0
},
{
"scale": "x",
"orient": "bottom",
"grid": false,
"title": "Horsepower",
"labelFlush": true,
"labelOverlap": true,
"tickCount": {"signal": "ceil(width/40)"},
"zindex": 0
},
{
"scale": "y",
"orient": "left",
"grid": false,
"title": "Miles_per_Gallon",
"labelOverlap": true,
"tickCount": {"signal": "ceil(height/40)"},
"zindex": 0
}
],
"config": {"style": {"point": {"opacity": 0.5}}}
}
13 changes: 13 additions & 0 deletions examples/specs/point_conditional_opacity.vl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {"url": "data/cars.json"},
"mark": "point",
"encoding": {
"x": {"field": "Horsepower", "type": "quantitative"},
"y": {"field": "Miles_per_Gallon", "type": "quantitative"},
"opacity": {
"condition": [{"test": "datum.Horsepower > 100", "value": 0.25}]
}
},
"config": {"point": {"opacity": 0.5}}
}
12 changes: 9 additions & 3 deletions src/compile/mark/encode/nonposition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {SignalRef} from 'vega';
import {NonPositionScaleChannel} from '../../../channel';
import {Value} from '../../../channeldef';
import {Value, isConditionalDef} from '../../../channeldef';
import {VgEncodeChannel, VgEncodeEntry, VgValueRef} from '../../../vega.schema';
import {getMarkPropOrConfig, signalOrValueRef} from '../../common';
import {UnitModel} from '../../unit';
Expand All @@ -24,16 +24,22 @@ export function nonPosition(
const {vgChannel} = opt;
let {defaultRef, defaultValue} = opt;

const channelDef = encoding[channel];

if (defaultRef === undefined) {
// prettier-ignore
defaultValue ??= getMarkPropOrConfig(channel, markDef, config, {vgChannel, ignoreVgConfig: true});
defaultValue ??= getMarkPropOrConfig(channel, markDef, config, {
vgChannel,
// If there is no conditonal def, we ignore vgConfig so the output spec is concise.
// However, if there is a conditional def, we must include vgConfig so the default is respected.
ignoreVgConfig: !isConditionalDef(channelDef)
});

if (defaultValue !== undefined) {
defaultRef = signalOrValueRef(defaultValue);
}
}

const channelDef = encoding[channel];
const commonProps = {
markDef,
config,
Expand Down

0 comments on commit b4c7e14

Please sign in to comment.