I want to output a bar chart using the “grouping” functionality, but also to sort the groups in a correct order.
When I put a “group” parameter and a “sort” parameter in the dhtmlXChart constuctor, only the “group” is taken into account (the groups are display in a random order).
When I use only a “sort” in the constructor and a “Group by” button like in the samples, it works; however is there a way to have the data grouped and sorted when the page is loaded ?
It possible, but a bit tricky, because in sort instruction you need to use names of data fields after grouping
Next is a snippet from working sample
sort:{
by:"#id#",
as:"string",
dir:"asc"
},
group:{
by:"#year#",
map:{
sales:["#sales#","sum"]
}
},
After grouping you have new dataset , which have fields from the map - which is “sales”, and will have group "id ", equal to the grouped value (year)
before grouping
[ { id:1, year:2001, sales:12 },
{ id:1, year:2001, sales:24 }]
after grouping
[ { id:2001, sales:36 } ]
So after grouping, attempt to sort by “year” will be useless, there is no such field in the final dataset, but you can sort by “#id#”
Also, to make things even more complicated , you can add extra field in group command
sort:{
by:"#year#",
as:"string",
dir:"asc"
},
group:{
by:"#year#",
map:{
sales:["#sales#","sum"],
year:["#year#", "any"]
}
},
now we will have “year” field and final dataset and can use it in sorting
Thank you, this works just fine. The only thing is that I had to replace the “any” function in your second example by “min”, but the result is exactly what I expected.