Search code examples
vegavega-lite

Sorting for unioned domains in vega-lite?


Playing with layers, I can't find a reliable way to sort values y dimension by x ("b" field value).

I tried to configure "sort" field for "y" encoding, I get domainSortDropped error. That's OK, but maybe there's a way to avoid it somehow by "pre-sorting" values before channel creation?

If I pass sorted dataset manually, specifying "sort": null for "y" encoding, vega still sorts rows by "x" alphabetically. Am I doing something wrong? How can I get "descending bars" aka "waterfall" chart?

{
  "width": 500,
  "height": 300,
  "data": {
        "values": [
            {
                "a": "A",
                "b": 70
            },
            {
                "a": "Z",
                "b": 50
            },
            {
                "a": "B",
                "b": 34
            }
        ]
    },
    "layer": [
        {
            "mark": "bar",
            "encoding": {
                "y": {
                    "field": "a",
                    "type": "ordinal",
                    "sort": null
                },
                "x": {
                    "field": "b",
                    "type": "quantitative"
                },
                "color": {
                    "field": "a",
                    "type": "nominal"
                }
            }
        },
        {
            "mark": "text",
            "encoding": {
                "y": {
                    "field": "a",
                    "type": "ordinal",
                    "sort": null
                },
                "text": {
                    "field": "b",
                    "type": "quantitative"
                }
            }
        }
    ]
}

Solution

  • This looks like a bug to me. I'm looking into it at https://github.com/vega/vega-lite/issues/3215.