I do have this result as a KQL query (single row)
A | B | C |
---|---|---|
5 | 7 | 3 |
And I'm trying to make a pie-chart from that. I tried a bit with narrow(), evaluate(), pivot()... Honestly, I don't understand exactly the data format expected for ADX pie-chart and how to "invert" my table result for it..
Any idea ? thanks :)
As per this MS Doc,
The piechart requires at least two columns in the query result and by default the first column will be considered as color axis.
I have tried pie chart with the same data and it gave me the below result.
let MyTable = datatable(A:int, B:int, C:int)
[
5, 7, 3
];
MyTable
| render piechart
I gave the below error saying it can't find the x-axis.
Then, I tried by converting the first column to string as shown below.
let MyTable = datatable(A:int, B:int, C:int)
[
5, 7, 3
];
MyTable
| extend A=tostring(A),B=toint(B)
| render piechart
Now, you can see it gave the pie chart, but it took only the first column.
Hence, the data format of your result data should be in correct format to obtain the required pie chart. The correct format is the first column should be string, and second column can be any numeric type. Transposing your data might be the best for getting the required pie chart. You can use below code to do that.
let table1 = datatable(A:int, B:int, C:int)
[
5, 7, 3
];
table1
| extend pack_json = pack_all()
| mv-expand pack_json
| extend Category = tostring(split(split(tostring(pack_json),':')[0],'"')[1]),Value= toint(split(split(tostring(pack_json),':')[1],'}')[0])
| project Category,Value
| render piechart
Here, this code used pack_all()
which creates a column pack_json
and stores the JSON of column name and value pairs {"A": 5,"B": 7,"C": 3}
. Then, it uses mv-expand
to expand these objects in 3 rows. After that, by using multiple splits it stores each key and value in new column as show below.
Category Value
A 5
B 7
C 3
Then, it will apply the pie chart on this data.
Result: