Search code examples
vega-litevega

Are multiple conditions possible in vega-lite?


I would like to build a chart that supports clicking on a specific bar, as well as brushing to select multiple bars. Looking at the condition documentation, it's not clear if I can do something like:

"fillOpacity": {
  "condition": [
    {
      "param": "select",
      "value": 1
    },
    {
      "param": "brush",
      "value": 1
    }
  ],
  "value": 0.3
}

The params I am using are:

"params": [
  {
    "name": "brush",
    "select": {
      "type": "interval",
      "encodings": ["x"]
    }
  },
  {"name": "select", "select": "point"}
]

I don't see any errors, but the dual selection behavior does not happen.

Thanks for any help.


Solution

  • You can have multiple conditions as shown in this spec. If you require further trouble shooting, you will need to provide a full spec to look at.

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "description": "A bar chart with highlighting on hover and selecting on click. (Inspired by Tableau's interaction style.)",
      "data": {
        "values": [
          {"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43},
          {"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53},
          {"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52}
        ]
      },
      "params": [
        {
          "name": "highlight",
          "select": {"type": "point", "on": "mouseover"}
        },
        {"name": "select", "select": "point"}
      ],
      "mark": {
        "type": "bar",
        "fill": "#4C78A8",
        "stroke": "black",
        "cursor": "pointer"
      },
      "encoding": {
        "x": {"field": "a", "type": "ordinal"},
        "y": {"field": "b", "type": "quantitative"},
        "fillOpacity": {
          "condition": {"param": "select", "value": 1},
          "value": 0.3
        },
        "strokeWidth": {
          "condition": [
            {
              "param": "select",
              "empty": false,
              "value": 2
            },
            {
              "param": "highlight",
              "empty": false,
              "value": 1
            }
          ],
          "value": 0
        }
      },
      "config": {
        "scale": {
          "bandPaddingInner": 0.2
        }
      }
    }