vega condition test on datum['key'] when value is itself a {key:value} object

51 views Asked by At

Using the Vega-Lite JSON specification of the scatterplot example ( https://vega.github.io/vega-lite/examples/point_2d.html ), and a cars.json altered to add an annotation field:

[
   {
      "annotation": {"selected" : true},
      "Name":"chevrolet chevelle malibu",
      "Miles_per_Gallon":18,
      "Horsepower":130,
   },
   {
      "annotation": {"selected": false},
      "Name":"buick skylark 320",
      "Horsepower": 165
   }
]

I want to do a conditional color encoding with a test on that boolean annotation.selected value . Using javascript dot notation seems straightforward:


{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A scatterplot showing horsepower and miles per gallons for various cars.",
  "data": {"url": "data/cars.json"},
  "mark": "point",
  "encoding": {
        "x": {"field": "Horsepower", "type": "quantitative"},
        "y": {"field": "Miles_per_Gallon", "type": "quantitative"
        color: {
            condition: {
                "test": "datum['annotation'].selected === true",
                value: 'yellow'
            },
            value: 'steelblue'
        }
    }
}

But no joy.

I was hoping the malibu would plot yellow and the skylark blue. But the above test predicate form and variants evaluate to false, and both points plot blue.

Is there a way to do this?

0

There are 0 answers