Forum Discussion

DW868990's avatar
DW868990
Helper IV
3 years ago
Solved

Deneb - Vega Lite Color Gradient Per Category

Hi,   Just wondering if and how it is possible to set the color gradient for category within a visual rather than across the full visual.   The below example shows the gradient based all all cate...
  • giammariam's avatar
    giammariam
    3 years ago

    DW868990, oh wow, apologies. It was so similar to an example on the vega-lite page, that I missed what you were actually after. Let me know if this is better. Happy to make additional tweaks if necessary. I took out the text in the boxes to match what you have. Can add it back in though if you like.

     

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "data": {"url": "data/cars.json"},
      "facet": {"row": {"field": "Origin", "title": null}},
      "title": {
        "text": "Mean Horsepower per Cylinder per Origin",
        "orient": "top",
        "anchor": "middle"
      },
      "spec": {
        "encoding": {
          "y": {"field": "Origin", "type": "ordinal", "axis": null},
          "x": {"field": "Cylinders", "type": "ordinal"}
        },
        "layer": [
          {
            "mark": "rect",
            "encoding": {
              "color": {
                "field": "Horsepower",
                "aggregate": "average",
                "type": "quantitative",
                "legend": {
                  "direction": "horizontal",
                  "orient": "right",
                  "gradientLength": 100,
                  "title": null
                }
              }
            }
          }
        ]
      },
      "resolve": {"scale": {"y": "independent", "color": "independent"}},
      "config": {"axis": {"grid": true, "tickBand": "extent"}}
    }