I have a Vega-Lite map, and I’m confused by Vega-Lite’s behavior coloring areas with a conditional statement. Some input would be greatly appreciated. Thanks in advance.
Goals: In my data, some of the neighborhoods’ values are suppressed; I’m trying to display these suppressed values as gray (#808080
in the spec) and keep the tooltip the same as on the non-suppressed neighborhoods. For the map’s color encoding, I have a condition, testing for isValid(datum.Value)
, and if that’s true, applying the color scale, and if it’s false, applying #808080
.
Question: In Vega 5.30.0, one change from 5.29.0 makes vega format null
as “null.” As a result, my conditional statement that tests for null data and styles accordingly doesn’t work – instead of treating these as nulls, it treats these as 0 values. Using Vega 5.30.0, how can I keep null data as null, so that conditionals checking for it can accurately find and style null data how I want to?
Further background: a spec is here – one of the neighborhoods to be suppressed is the northern neighborhood on the south-western island (“Northern SI”). For invalid data (where datum.Value == null
), it’s not applying #808080
, the value in the else part of the statement – it’s just treating those nulls as 0s and applying the lowest part of the color scale in the test part of the condition. I’ve tried "test": "isValid(datum.Value)"
(isValid should return true if value is not null, undefined, or NaN, and false otherwise), but it colors neighborhoods where Value == null
. The same goes for "test": "datum.Value != null"
.
I’ve tried a handful of other things, too (including adding a separate true/false Suppressed
field), but each approach seems to involve Vega-Lite treating nulls like 0s, or struggling with a conditional test involving null data. While experimenting, if I reverse the logic (eg with "test": "datum.Suppressed == true"
), the ‘else’ works as expected but styles for satisfying the test condition do not.
(Curiously – though this may be a separate issue – when I use "test": "isValid(datum.DisplayValue"
it colors neighborhoods where datum.DisplayValue
is alphanumeric; there’s something happening that I do not understand.)
Input is greatly appreciated here! Again, thanks in advance.
mpmm-vis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Try this.
"color": {
"condition": {
"test": "datum.Value!='null'",
"bin": false,
"field": "Value",
"type": "quantitative",
"scale": {"scheme": {"name": "reds", "extent": [0.125, 1.25]}}
},
"value": "#808080"
},