Pie Chart sort legend label (not by value)

Hi all,
I’m on Grafana 9.2 with a simple Pie Chart.

sum by (ProductVersionYear, ProductUpdateLevel)(sql_server_version{job="$job", ProductVersionYear=~"$sqlversion", exported_instance=~"$dbinstance"})

I would like to know if…is it possible sort legend by label?
From PromQL naturally is not possible… I tried from the grafana sorting but for this type of panel it seems that there is no setting that affects

Like this way:
2019 CU14
2019 CU8
2017 CU22
2017 CU10
2016 CU7

Now is…

Thanks ALEN

we have here the same problem and want to sort by Label and not by the Value.
Is there any news here yet?

HI Markus,
no… it is the value that does the sorting
if you’ll find the way let me know ; )

You might have to go with regex-ing your label so that, if all labels have CU, you just get the numeric value of the labels such as
Because what you have is string,
2019 C14 comes before 2019 C8

1 Like


What have you tried as far as regex with PromQL?

1 Like

No, honestly it’s more logical that it’s sorted by value… I’ve thought about it. I put a table in another panel with the sorting to my liking. However I tried to see your indication, but I found it difficult to use the regexp … and to the logic of how to apply it.

I used to feel the same way, time to learn it. It is a lifesaver.

Use regex101.com

1 Like

I will try in WE…
Thanks @yosiasz


Check this out

Hi @yosiasz,

thanks for your suggestion… regex101 it’s a great site, I use it too… but maybe I explained myself badly… also because I reply to these posts late at night and many times I’m tired, by the way, my problem wasn’t so much finding a pattern for the match but it was how I then had to insert it in the transformations in the grafana panel to change the sorting from value.

I’m using prometheus as data source… and data is a timeseries…

As you can see here… this is the perfect match on my string…
but from this… regex101: build, test, and debug regex

How do I manage the thing in the Grafana panel to have the desired sorting, therefore not following the numerical values ​​there are by default?
This is my problem… 'cause there is always a series to choose in the sort combo selection, sorry if I was not clear before.


Hello, have you found the solution to sort the legend not by the value default? or even grouped the pie portion?

What problem are you trying to solve in your case?

I have this pie chart and still wondering to group it into blue at the right side and red on the left side with that shown portion. Only for a beauty viz, sir.

As far as I know, the pie portion order is Descending Clock wise, but i am not so sure.

1 Like

HI yosiasz,
I’m having the same problem as the author, and I want to sort the legend in order of Critical, Hight, Medium and Low, can you guide me in detail?

1 Like

what is your data source?

I mapped the high, medium, low and critical statuses to numeric values and sorted on that numeric value

select a.* from (
select 'A' metric ,84 value,'High' as status union
select 'B',10,'Low' union
select 'C',6,'Critical' union
select 'D',40,'Medium'
) a 
join (
select 10 ste, 'High' as status union
select 20 ste, 'Medium' as status union 
select 30 ste, 'Low' as status union 
select 40 ste, 'Critical' as status  
) ste on a.status = ste.status
order by ste.ste asc

HI yosiasz,
I use PostgreSQL as a data source, and here is my query that retrieves my data:
WHEN point >90 AND point <=100 THEN ‘Critical’
WHEN point >70 AND point <=90 THEN ‘High’
WHEN point >50 AND point <=70 THEN ‘Medium’
WHEN point >= 0 AND point <= 50 THEN ‘Low’
END AS level,
COUNT(*) AS record_count
FROM object o
WHERE point >=0 and point <=100
GROUP BY level

My users requested sorting of the pie chart labels by severity. I was unable to do it with the pie chart so I built my own legend using Dynamic Text panel. It works visually, but lacks the interactivity of a real solution.

What happened in the out of the box pie chart?

My problem is exactly the same as yours tantranncs. I wanted it to show “critical” first, not the most popular pie slice first.