Search code examples
vega-litevega

How to highlight the zoomed in bar and know the details of that bar in vega-lite?


I am able to create the overview details bar graph and zoomed-in nicely when I select the range in the bottom graph. But I am having difficulty to highlight the bars in the zoomed-in graph and also know what bars have been selected.

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "actions": false,
  "data": {
    "values": [
      { "created": 1595053277243 },
      { "created": 1595053277244 },
      { "created": 1595055277243 },
      { "created": 1594880606860 },
      { "created": 1594880604261 }
    ]
  },
  "vconcat": [{
        "width": 1500,
        "height": 300,
        "selection": {
          "highlight": {"type": "single", "empty": "none", "on": "mouseover"},
          "select": {"type": "multi"}
        },
        "mark": {
          "type": "bar",
          "fill": "#4C78A8",
          "stroke": "black",
          "cursor": "pointer"
        },
        "encoding": {
          "x": {
            "field": "created",
            "type": "temporal",
            "scale": {"domain": {"selection": "brush"}},
            "axis": {"title": ""},
            "timeUnit": "utcyearmonthdatehoursminutes",
            "update": {
              "fillOpacity": {
                "condition": {"selection": "select", "value": 1},
                "value": 0.3
              },
              "strokeWidth": {
                "condition": [
                  {
                    "test": {
                      "and": [
                        {"selection": "select"},
                        "length(data(\"select_store\"))"
                      ]
                    },
                    "value": 2
                  },
                  {"selection": "highlight", "value": 1}
                ],
                "value": 0
              }
            }
          },
          "y": {
            "field": "created",
            "type": "quantitative",
            "aggregate": "count"
          }
        },
        "config": {
          "scale": {
            "bandPaddingInner": 0.2
          }
        }
      }, {
        "width": 1500,
        "height": 100,
        "padding": 10,
        "mark": "bar",
        "selection": {
          "brush": {"type": "interval", "encodings": ["x"]}
        },
        "encoding": {
          "x": {
            "field": "created",
            "type": "temporal",
            "timeUnit": "utcyearmonthdatehours"
          },
          "y": {
            "field": "created",
            "type": "quantitative",
            "aggregate": "count"
          }
        }
      }]
}

I tried to put the fill-opacity and stroke-width in encoding but doesn't seem to work. I also tried to patch the compiled vega in vega-embed to listen to the bar click event but it doesn't listen to the top (zoomed in) graph.

Example of what I am trying to do


Solution

  • Vega-Lite encodings have no update property. You can specify the features directly in the encoding mapping:

    
          "encoding": {
            "x": {
              "field": "created",
              "type": "temporal",
              "scale": {"domain": {"selection": "brush"}},
              "axis": {"title": ""},
              "timeUnit": "utcyearmonthdatehoursminutes"
            },
            "fillOpacity": {
              "condition": {"selection": "select", "value": 1},
              "value": 0.3
            },
            "strokeWidth": {
              "condition": [
                {
                  "test": {
                    "and": [
                      {"selection": "select"},
                      "length(data(\"select_store\"))"
                    ]
                  },
                  "value": 2
                },
                {"selection": "highlight", "value": 1}
              ],
              "value": 0
            },
            "y": {"field": "created", "type": "quantitative", "aggregate": "count"}
          }
    

    Open the Chart in the Vega Editor