simonw/django-sql-dashboard

Bar charts sort labels alphabetically instead of respecting their order

simonw opened this issue · 7 comments

Here's why:

encoding: {
x: {
field: "bar_label",
title: "Label",
type: "nominal",
axis: { labelAngle: 90 },
},
y: { field: "bar_quantity", title: "Quantity", type: "quantitative" },
},

It's because of type: "nominal".

I am finding it remarkably difficult to figure out how to get Vega-Lite bar charts to respect the initial order (I remember having this same problem with datasette-vega): https://vega.github.io/vega-lite/docs/bar.html

That's not quite what I'm after here - I want to sort in the order originally provided by the results of the query.

So if the data looked like this:

  {
    "values": [
      {"a": "Dog", "b": 28},
      {"a": "Cat", "b": 55},
      {"a": "Chicken", "b": 43},
      {"a": "Dragon", "b": 91}
    ]
  }

I want the bars on the bar chart to be in Dog, Cat, Chicken, Dragon order.

Aha! It looks like "sort": null does the trick. https://vega.github.io/vega-lite/docs/sort.html#no-sorting

I can fix simonw/datasette-vega#19 with this too.