vega-lite

How to use data order in "Order" channel for arc?


May I know how to let arc's order use the original order from data? It doesn't seem to support sort: null. Can I enable sort: null in order channel`?

{
  "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  "description": "A simple pie chart with embedded data.",
  "data": {
    "values": [
      {"category": 2, "value": 6},
      {"category": 1, "value": 4},
      {"category": 3, "value": 10}
    ]
  },
  "mark": "arc",
  "encoding": {
    "theta": {"field": "value", "type": "quantitative"},
    "color": {
      "field": "category",
      "type": "nominal",
      "sort": null,
      "--comment": "sort: null here makes the legend order use the original order in data.values"
    },
    "order": {
      "field": "category",
      "sort": null,
      "--comment": "sort: null is not allowed in order?"
    }
  }
}

VL Editor Example


Solution

  • Add an index via a window.

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
      "description": "A simple pie chart with embedded data.",
      "data": {
        "values": [
          {"category": 2, "value": 6},
          {"category": 1, "value": 4},
          {"category": 3, "value": 10}
        ]
      },
      "transform": [{"window": [{"op": "row_number", "as": "index"}]}],
      "mark": "arc",
      "encoding": {
        "theta": {"field": "value", "type": "quantitative"},
        "color": {
          "field": "category",
          "type": "nominal",
          "sort": {"field": "index", "op": "max", "order": "ascending"}
        },
        "order": {"sort": "ascending", "field": "index"}
      }
    }