3

I came across this pie chart vega lite visualization:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A simple pie chart with labels.",
  "data": {
    "values": [
      {"category": "a", "value": 4},
      {"category": "b", "value": 6},
      {"category": "c", "value": 10},
      {"category": "d", "value": 3},
      {"category": "e", "value": 7},
      {"category": "f", "value": 8}
    ]
  },
  "encoding": {
    "theta": {"field": "value", "type": "quantitative", "stack": true},
    "color": {"field": "category", "type": "nominal", "legend": null}
  },
  "layer": [{
    "mark": {"type": "arc", "outerRadius": 80}
  }, {
    "mark": {"type": "text", "radius": 90},
    "encoding": {
      "text": {"field": "category", "type": "nominal"}
    }
  }]
}

It renders as follows:

enter image description here

My data contains color key:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A simple pie chart with labels.",
  "data": {
    "values": [
      {"category": "a", "value": 4, "color": "rgb(121, 199, 227)"},
      {"category": "b", "value": 6, "color": "rgb(140, 129, 22)"},
      {"category": "c", "value": 10, "color": "rgb(96, 43, 199)"},
      {"category": "d", "value": 3, "color": "rgb(196, 143, 99)"},
      {"category": "e", "value": 7, "color": "rgb(12, 103, 19)"},
      {"category": "f", "value": 8, "color": "rgb(196, 243, 129)"}
    ]
  },
  "encoding": {
    "theta": {"field": "value", "type": "quantitative", "stack": true},
    "color": {"field": "color", "type": "nominal", "legend": null}
  },
  "layer": [{
    "mark": {"type": "arc", "outerRadius": 80}
  }, {
    "mark": {"type": "text", "radius": 90},
    "encoding": {
      "text": {"field": "category", "type": "nominal"}
    }
  }]
}

I want to use the rgb() color value specified in this color key's value to color individual pie. I tried specifying this field in color channel: "field": "color".

"color": {"field": "color", "type": "nominal", "legend": null}

However, no use. It still renders the same as above. How can use color value specified in field's value as actual color?

PS: Link to above visualization.

MsA
  • 2,599
  • 3
  • 22
  • 47

1 Answers1

2

The documentation says:

To directly encode the data value, the scale property can be set to null.

So you need to set the scale to null.

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A simple pie chart with labels.",
  "data": {
    "values": [
      {"category": "a", "value": 4, "color": "rgb(121, 199, 227)"},
      {"category": "b", "value": 6, "color": "rgb(140, 129, 22)"},
      {"category": "c", "value": 10, "color": "rgb(96, 43, 199)"},
      {"category": "d", "value": 3, "color": "rgb(196, 143, 99)"},
      {"category": "e", "value": 7, "color": "rgb(12, 103, 19)"},
      {"category": "f", "value": 8, "color": "rgb(196, 243, 129)"}
    ]
  },
  "encoding": {
    "theta": {"field": "value", "type": "quantitative", "stack": true},
    "color": {"field": "color", "type": "nominal", "legend": null, "scale":null}
  },
  "layer": [
    {"mark": {"type": "arc", "outerRadius": 80}},
    {
      "mark": {"type": "text", "radius": 90},
      "encoding": {"text": {"field": "category", "type": "nominal"}}
    }
  ]
}

This outputs:

enter image description here

MsA
  • 2,599
  • 3
  • 22
  • 47
Davide Bacci
  • 16,647
  • 3
  • 10
  • 36
  • ohh!! can you please elaborate what exactly happens when you set `"scale": null`. Just want to get WHY we have to do it. Might help future readers too... May be am missing some basic understanding here.. – MsA Jun 25 '22 at 14:56
  • 1
    You can read about it here. https://vega.github.io/vega-lite/docs/scale.html#disable Vega-Lite tries to infer a scale for you unless you disable it. Don't forget to mark as solved for future readers. – Davide Bacci Jun 25 '22 at 15:08