borisko_cp
Posts: 3
Joined: Wed Jun 07, 2023 8:15 pm

Stacked bar chart sort by total

Hi,

I have data of several categories that I am adding to a stacked bar chart. Each category has up to 4 values in 4 distinct series.
I want to show the chart such that it is sorted by the highest total value of all the shown series values.
I actually have a working example of the exact behavior I want but my solution seems little hacky - https://jsfiddle.net/4rwy6qd5/1/

Is there a better way to do it?
Thanks.
jakub.j
Posts: 1091
Joined: Tue Jan 24, 2023 11:14 am

Re: Stacked bar chart sort by total

Welcome to our forum and thanks for contacting us with your question!

I prepared a demo for you, with a custom sorting solution. Simply put you data in the "dataArr" and categories' names in the "names" array and the data will be sorted, no matter how many series or stacks you have. The categories will also be sorted along with the data.

Demo: https://jsfiddle.net/BlackLabel/j7sfya4r/

Let me know if that was what you were looking for!
Best regards
Jakub
borisko_cp
Posts: 3
Joined: Wed Jun 07, 2023 8:15 pm

Re: Stacked bar chart sort by total

Tank you for the quick reply. Your solution does not reflect my requirement. If you check the example I posted you will see that if you hide and show any of the series the chart remains sorted by re-ordering the categories with a nice animation. In your example the categories remain in static order.
jakub.j
Posts: 1091
Joined: Tue Jan 24, 2023 11:14 am

Re: Stacked bar chart sort by total

Hey!

If you want the categories re-ordering with an animation, your solution would be better than mine, I saw you took the idea from the dataSorting demo and it's nothing bad about it.
However, I simplified your code a bit and extracted some of the parts to a reusable functions.

Demo: https://jsfiddle.net/BlackLabel/96rmp4kh/

I hope you will find it useful
Best regards!
Jakub
borisko_cp
Posts: 3
Joined: Wed Jun 07, 2023 8:15 pm

Re: Stacked bar chart sort by total

Great, thank you Jakub for the reassurance!

P.S. This is a new user for my current job but I am actually been using Highcharts since version 4 :)

Return to “Highcharts Usage”