Skip to content
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

Bar Chart sorting does not appear to work #30238

Closed
1 of 3 tasks
HJelinkova opened this issue Sep 11, 2024 · 8 comments
Closed
1 of 3 tasks

Bar Chart sorting does not appear to work #30238

HJelinkova opened this issue Sep 11, 2024 · 8 comments
Assignees
Labels
#bug:cant-reproduce Bugs that cannot be reproduced validation:required A committer should validate the issue viz:charts:bar Related to the Bar chart

Comments

@HJelinkova
Copy link

HJelinkova commented Sep 11, 2024

Bug description

It is imposible to change the layout of the bar chart by using the "Sort by" function.
The ASC/DESC checkbox changes the query but the chart stays unaffected.

Same issues previously reported: #15260, #15318

How to reproduce the bug

  1. Create a bar chart.
  2. In the "Data" tab find the "Sort by" section.
  3. Choose the column and the aggregate by which you want to sort the data by.
  4. Tick/Untick the "Sort descending" checkbox.

Screenshots/recordings

Bar chart bug (1)
Bar chart bug (2)
Bar chart bug - query (1)
Bar chart bug - query (2)

Superset version

4.0.2

Python version

Python 3.10.14

Node version

NODE version 20

Browser

Chrome

Additional context

Note: Superset installed using docker.

Checklist

  • I have searched Superset docs and Slack and didn't find a solution to my problem.
  • I have searched the GitHub issue tracker and didn't find a similar bug report.
  • I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.

This comment was marked as off-topic.

@dosubot dosubot bot added the viz:charts:bar Related to the Bar chart label Sep 11, 2024
@sfirke
Copy link
Member

sfirke commented Sep 12, 2024

If you read the mouseover tooltip for SORT BY you'll see that's for something else. That should probably be labeled as "DATA LIMIT SORT BY". What you need is X-AXIS SORT BY, which switches to Y-AXIS SORT BY when a bar chart is switched to horizontal orientation as it appears yours is. I don't know why you don't see that option in your screenshot; are you positive you're running Superset 4.0.2?

Here's what it looks like for me. With the default sorting:
image

Sorting by a specific metric changes the bars' order:
image

@sfirke sfirke self-assigned this Sep 12, 2024
@sfirke sfirke added #bug:cant-reproduce Bugs that cannot be reproduced validation:required A committer should validate the issue labels Sep 12, 2024
@HJelinkova
Copy link
Author

Thank you for your reply. I have now figured out what the problem was. The X-AXIS SORT BY (Y-AXIS SORT BY) didn't appear because of the data type of the column. It only works for STRING data type whilst mine is VAR_STRING.

@mahesh-babu-drivex
Copy link

Thank you for your reply. I have now figured out what the problem was. The X-AXIS SORT BY (Y-AXIS SORT BY) didn't appear because of the data type of the column. It only works for STRING data type whilst mine is VAR_STRING.

How can you change the column type to string? @HJelinkova

@pedrosubei
Copy link

Thank you for your reply. I have now figured out what the problem was. The X-AXIS SORT BY (Y-AXIS SORT BY) didn't appear because of the data type of the column. It only works for STRING data type whilst mine is VAR_STRING.

How can you change the column type to string? @HJelinkova

Did you find how? (i am using version 4.0.2 and that option doesn't apear)

@rbosnjak
Copy link

rbosnjak commented Jan 23, 2025

Hello!

I'm dealing with the same issue. While I'm still not able to find out how to sort x-axis of bar chart based on some metric (by not showing it on the chart) I found a way how to create STRING column from VAR_STRING.

To do this, you should open Edit dataset screen, and select "CALCULATED COLUMNS" tab. There you need to enter SQL expression, which will probably be just the name of the varchar column you want to convert. This will create a new column in your dataset, which is marked as String.

I'm attaching 2 screenshots where this beahviour is visible.

Image

Image

Now, Sorting option really appears on the screen, as mentioned in the comments above.

Image

Image

The next thing I would like to achieve is custom sorting, based on a column which is not shown neither on x-axis or inside bar chart values (metrics field).

In my scenario, I have x-axis which is formed out of dates and Identification Numbers of subjects I am analyzing. The date is in the following format: %d.%m.%Y. - for example 31.12.2024.. The identification number is 11 digit number. So in the end I get x-axis value like 31.12.2024. 12345678901.

I would want to sort these on x-axis, but based on YYYY-mm-dd date format, since string sort does not work correctly on my time format. Note that I already track some metric for each date-subject, and I cannot add any new metric to the barchart to be visible.

Image

Does anyone know how I could achieve this?

@rusackas
Copy link
Member

@rbosnjak I may be misunderstanding, but in your bottom pic, the X axis DOES look like it's sorting string labels correctly... but if you want them in order chronologically, you'd want a temporal column, not a string column.

@rbosnjak
Copy link

rbosnjak commented Jan 23, 2025

[EDIT]
Hello, and thanks for answering!

I see what you're saying—probably I should have explained it better from the start.

The goal is to present stacked bar chart that shows Duration of each Activity for each Subject ID for each day. Subject ID filter is multi-select, meaning that I need to show multiple subjects at once.

Initially I started by dragging Date field to x-axis, Activity, and Subject ID to dimensions, and Duration to metrics. This worked almost as expected. Everything was good, except the coloring in scenario when I have multiple drivers selected in the filter. This happens because Superset does coloring based on values from dimensions group. Hence, the same activity for distinct subjects will be colored differently. The same color would be defined for Activity-Subject pair, which is not something I wanted.

To resolve that, I realized my x-axis can't be a temporal column, but a string created using CONCAT(date_in_d_m_y_format, '\n', subject_id).

Then I changed dimension to leave only Activity in it.

Here's a screenshot that shows how the x-axis values actually look in one example
Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#bug:cant-reproduce Bugs that cannot be reproduced validation:required A committer should validate the issue viz:charts:bar Related to the Bar chart
Projects
None yet
Development

No branches or pull requests

6 participants