Skip to content

Bar charts sort labels alphabetically instead of respecting their order #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
simonw opened this issue May 10, 2021 · 7 comments
Closed
Labels
bug Something isn't working dashboard-widgets
Milestone

Comments

@simonw
Copy link
Owner

simonw commented May 10, 2021

SQL__select__tag__as_bar_label__count____as_bar_quantity_from__select___from_blog_entry_tags_join_blog_tag_on_blog_tag_id___blog_entry_tags_tag_id__as_results_group_by__tag__order_by_bar_quantity_desc

Pointed out here: https://twitter.com/zellyn/status/1391786830333464579

@simonw simonw added bug Something isn't working dashboard-widgets labels May 10, 2021
@simonw
Copy link
Owner Author

simonw commented May 10, 2021

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".

@simonw
Copy link
Owner Author

simonw commented May 10, 2021

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

@simonw
Copy link
Owner Author

simonw commented May 14, 2021

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.

@simonw
Copy link
Owner Author

simonw commented May 14, 2021

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

@simonw simonw closed this as completed in 1ec286d May 14, 2021
simonw added a commit to simonw/simonwillisonblog that referenced this issue May 14, 2021
@simonw
Copy link
Owner Author

simonw commented May 14, 2021

https://simonwillison.net/dashboard/bar-chart-for-issue-93/ before this fix:

Bar_chart_for_issue_93

After the fix:

Bar_chart_for_issue_93

@simonw
Copy link
Owner Author

simonw commented May 14, 2021

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

@simonw simonw added this to the 0.14 milestone May 16, 2021
simonw added a commit that referenced this issue May 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dashboard-widgets
Projects
None yet
Development

No branches or pull requests

2 participants