signalsvisualizationfieldaggregationvega

How do I use a drop down to change field in Vega visualization


In Vega or Vega lite, I want to create a stacked area chart where I can change the field used to color the visualization. Here is an example visualization. In this example, I would like to be able to switch between grouping by the "Cylinders", "Horsepower", and "Acceleration". This is a simplified version of what am trying to do but shows the point. I tried setting the field to a signal attached to a dropdown but that does not seem to work:

"fill": {
  "scale": "color_by_cylinders",
  "field": {"signal": "Group_By"}
},

Even if it did work, I would still need to change the color scale based on the drop down. I would also need to change the field we aggregate by. Is there a way to do this in Vega or vega lite?

{
  "$schema": "https://vega.github.io/schema/vega/v6.json",
  "autosize": {"type": "fit-x", "contains": "padding"},
  "background": "white",
  "padding": 5,
  "height": 250,
  "style": "cell",
  "data": [
    {"name": "Group_By_store"},
    {
      "name": "source_0",
      "url": "data/cars.json",
      "format": {"type": "json"},
      "transform": [
        {
          "type": "aggregate",
          "groupby": ["Origin", "Cylinders"],
          "ops": ["count"],
          "fields": [null],
          "as": ["__count"]
        },
        {
          "type": "stack",
          "groupby": ["Origin"],
          "field": "__count",
          "sort": {"field": ["Cylinders"], "order": ["descending"]},
          "as": ["__count_start", "__count_end"],
          "offset": "zero"
        }
      ]
    }
  ],
  "signals": [
    {
      "name": "width",
      "init": "isFinite(containerSize()[0]) ? containerSize()[0] : 300",
      "on": [
        {
          "update": "isFinite(containerSize()[0]) ? containerSize()[0] : 300",
          "events": "window:resize"
        }
      ]
    },
    {
      "name": "unit",
      "value": {},
      "on": [
        {"events": "pointermove", "update": "isTuple(group()) ? group() : unit"}
      ]
    },
    {
      "name": "Group_By",
      "value": null,
      "bind": {
        "input": "select",
        "options": ["Cylinders", "Horsepower", "Acceleration"],
        "labels": ["Cylinders", "Horsepower", "Acceleration"]
      }
    }
  ],
  "marks": [
    {
      "name": "marks",
      "type": "rect",
      "style": ["bar"],
      "interactive": true,
      "from": {"data": "source_0"},
      "encode": {
        "update": {
          "fill": {
            "scale": "color_by_cylinders",
            "field": {"signal": "Group_By"}
          },
          "ariaRoleDescription": {"value": "bar"},
          "description": {
            "signal": "\"Origin: \" + (isValid(datum[\"Origin\"]) ? datum[\"Origin\"] : \"\"+datum[\"Origin\"]) + \"; Number of Cars: \" + (format(datum[\"__count\"], \"\")) + \"; Cylinders: \" + (isValid(datum[\"Cylinders\"]) ? datum[\"Cylinders\"] : \"\"+datum[\"Cylinders\"])"
          },
          "x": {"scale": "x", "field": "Origin"},
          "width": {"signal": "max(0.25, bandwidth('x'))"},
          "y": {"scale": "y", "field": "__count_end"},
          "y2": {"scale": "y", "field": "__count_start"}
        }
      }
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "band",
      "domain": {"data": "source_0", "field": "Origin", "sort": true},
      "range": [0, {"signal": "width"}],
      "paddingInner": 0.1,
      "paddingOuter": 0.05
    },
    {
      "name": "y",
      "type": "linear",
      "domain": {
        "data": "source_0",
        "fields": ["__count_start", "__count_end"]
      },
      "range": [{"signal": "height"}, 0],
      "nice": true,
      "zero": true
    },
    {
      "name": "color_by_horsepower",
      "type": "ordinal",
      "domain": {"data": "source_0", "field": "Cylinders", "sort": true},
      "range": "category"
    },
    {
      "name": "color_by_acceleration",
      "type": "ordinal",
      "domain": {"data": "source_0", "field": "Cylinders", "sort": true},
      "range": "category"
    },
    {
      "name": "color_by_cylinders",
      "type": "ordinal",
      "domain": {"data": "source_0", "field": "Cylinders", "sort": true},
      "range": "category"
    }
  ],
  "axes": [
    {
      "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": "Origin",
      "labelAlign": "right",
      "labelAngle": 270,
      "labelBaseline": "middle",
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "grid": false,
      "title": "Number of Cars",
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "zindex": 0
    }
  ],
  "legends": [
    {"fill": "color_by_cylinders", "symbolType": "square", "title": "Cylinders"}
  ],
  "config": {}
}

Solution

  • Because of this issue, the legend is a bit problematic. https://github.com/vega/vega/issues/3488

    There is a way round it though by doing this:

    Link

    {
      "$schema": "https://vega.github.io/schema/vega/v6.json",
      "autosize": {"type": "fit-x", "contains": "padding"},
      "background": "white",
      "padding": 25,
      "height": 250,
      "style": "cell",
      "data": [
        {
          "name": "a",
          "url": "data/cars.json",
          "format": {"type": "json"},
          "transform": [
            {
              "type": "aggregate",
              "groupby": ["Origin", "Cylinders"],
              "ops": ["count"],
              "fields": [null],
              "as": ["__count"]
            },
            {
              "type": "stack",
              "groupby": ["Origin"],
              "field": "__count",
              "sort": {"field": ["Cylinders"], "order": ["descending"]},
              "as": ["__count_start", "__count_end"],
              "offset": "zero"
            },
            {"type": "formula", "as": "type", "expr": "'Cylinders'"}
          ]
        },
        {
          "name": "b",
          "url": "data/cars.json",
          "format": {"type": "json"},
          "transform": [
            {
              "type": "aggregate",
              "groupby": ["Origin", "Acceleration"],
              "ops": ["count"],
              "fields": [null],
              "as": ["__count"]
            },
            {
              "type": "stack",
              "groupby": ["Origin"],
              "field": "__count",
              "sort": {"field": ["Acceleration"], "order": ["descending"]},
              "as": ["__count_start", "__count_end"],
              "offset": "zero"
            },
            {"type": "formula", "as": "type", "expr": "'Acceleration'"}
          ]
        },
        {
          "name": "c",
          "url": "data/cars.json",
          "format": {"type": "json"},
          "transform": [
            {
              "type": "aggregate",
              "groupby": ["Origin", "Horsepower"],
              "ops": ["count"],
              "fields": [null],
              "as": ["__count"]
            },
            {
              "type": "stack",
              "groupby": ["Origin"],
              "field": "__count",
              "sort": {"field": ["Horsepower"], "order": ["descending"]},
              "as": ["__count_start", "__count_end"],
              "offset": "zero"
            },
            {"type": "formula", "as": "type", "expr": "'Horsepower'"}
          ]
        },
        {
          "name": "d",
          "source": ["a", "b", "c"],
          "transform": [
            {
              "type": "filter",
              "expr": "Group_By==null?datum.type=='Cylinders':datum.type==Group_By"
            }
          ]
        }
      ],
      "signals": [
        {
          "name": "width",
          "init": "isFinite(containerSize()[0]) ? containerSize()[0] : 300",
          "on": [
            {
              "update": "isFinite(containerSize()[0]) ? containerSize()[0] : 300",
              "events": "window:resize"
            }
          ]
        },
        {
          "name": "Group_By",
          "value": null,
          "bind": {
            "input": "select",
            "options": ["Cylinders", "Horsepower", "Acceleration"],
            "labels": ["Cylinders", "Horsepower", "Acceleration"]
          }
        }
      ],
      "marks": [
        {
          "name": "marks",
          "type": "rect",
          "style": ["bar"],
          "interactive": true,
          "from": {"data": "d"},
          "encode": {
            "update": {
              "fill": {
                "signal": "Group_By==null||Group_By=='Cylinders'?scale('dynamicScale',datum.Cylinders):Group_By=='Acceleration'?scale('dynamicScale',datum.Acceleration):scale('dynamicScale',datum.Horsepower)"
              },
              "x": {"scale": "x", "field": "Origin"},
              "width": {"signal": "max(0.25, bandwidth('x'))"},
              "y": {"scale": "y", "field": "__count_end"},
              "y2": {"scale": "y", "field": "__count_start"}
            }
          }
        }
      ],
      "scales": [
        {
          "name": "x",
          "type": "band",
          "domain": {"data": "a", "field": "Origin", "sort": true},
          "range": [0, {"signal": "width"}],
          "paddingInner": 0.1,
          "paddingOuter": 0.05
        },
        {
          "name": "y",
          "type": "linear",
          "domain": {"data": "a", "fields": ["__count_start", "__count_end"]},
          "range": [{"signal": "height"}, 0],
          "nice": true,
          "zero": true
        },
        {
          "name": "Horsepower",
          "type": "ordinal",
          "domain": {"data": "d", "field": "Horsepower", "sort": true},
          "range": "category"
        },
        {
          "name": "Acceleration",
          "type": "ordinal",
          "domain": {"data": "d", "field": "Acceleration", "sort": true},
          "range": "category"
        },
        {
          "name": "dynamicScale",
          "type": "ordinal",
          "domain": {"signal": "Group_By==null||Group_By=='Cylinders'?   sort(pluck(data('d'),'Cylinders')):Group_By=='Acceleration'?   sort(pluck(data('d'),'Acceleration')):   sort(pluck(data('d'),'Horsepower')) "},
          "range": "category"
        }
      ],
      "axes": [
        {
          "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": "Origin",
          "labelAlign": "right",
          "labelAngle": 270,
          "labelBaseline": "middle",
          "zindex": 0
        },
        {
          "scale": "y",
          "orient": "left",
          "grid": false,
          "title": "Number of Cars",
          "labelOverlap": true,
          "tickCount": {"signal": "ceil(height/40)"},
          "zindex": 0
        }
      ],
      "legends": [
        {"fill": "dynamicScale", "symbolType": "square"}
      ],
      "config": {}
    }