Hello ChartJS community!
I am hoping someone has figured out how to properly sort “datasets” within their clusters in a ChartJS Bar chart. Below is an example of a bar chart in ChartJS with multiple “datasets”, each with their own label that corresponds with a color/item in the legend.
Here is an image of what the original chart looks like (descending in each cluster)
Chart.JS Bar Example PNG
What I want to do is retain the X-Axis (which is sorted by “Reporting Period”), while having the data points within each X-Axis interval or group be sorted either ascending or descending.
Approaches I have attempted:
- restructuring the
data property in each dataset to match the object structure option
- using a singular data object and “parsing” properties in each
dataset in the datasets array
- manipulating the
datasets array to isolate datapoints across numerous dataset objects with the same label
What I have found with the first two attempts is that with those approaches, the position of each bar associated with each “dataset” will always follow the ordering (i.e. the datasetIndex) of each dataset in the datasets array within its group…
I found that the third attempt achieved the desired sorting, but that manipulation causes redundant legend labels.
Here is an image of what the desired sorting looks like (ascending in each cluster)
chart clusters sorted
Here is what the “original” display data looks like
Chart Data
{
"datasets": [
{
"label": "Respondent 1",
"data": [
750,
null,
null,
null
],
"borderRadius": 5,
"backgroundColor": "#1E81D3",
"borderColor": "#ccc",
"spanGaps": true,
"order": 0
},
{
"label": "Respondent 2",
"data": [
null,
33921,
100001,
3999
],
"borderRadius": 5,
"backgroundColor": "#063F84",
"borderColor": "#ccc",
"spanGaps": true,
"order": 1
},
{
"label": "Respondent 3",
"data": [
null,
419,
21583,
488
],
"borderRadius": 5,
"backgroundColor": "#0D5AB7",
"borderColor": "#ccc",
"spanGaps": true,
"order": 2
}
],
"labels": [
"2017 Fiscal Year (Jul-Jun) - H2",
"2022 Fiscal Year (Jan-Dec) - H2",
"2023 Fiscal Year (Jan-Dec) - H1",
"2023 Fiscal Year (Jan-Dec) - H2"
],
"yAxisLabel": "Cubic Meters"
}
here is what the data looks like after resorting (3rd attempt outlined above)
{
"datasets": [
{
"label": "Respondent 1",
"data": [
750,
null,
null,
null
],
"backgroundColor": "#1E81D3",
"borderColor": "#ccc",
"borderRadius": 5
},
{
"label": "Respondent 3",
"data": [
null,
419,
null,
null
],
"backgroundColor": "#0D5AB7",
"borderColor": "#ccc",
"borderRadius": 5
},
{
"label": "Respondent 2",
"data": [
null,
33921,
null,
null
],
"backgroundColor": "#063F84",
"borderColor": "#ccc",
"borderRadius": 5
},
{
"label": "Respondent 3",
"data": [
null,
null,
21583,
null
],
"backgroundColor": "#0D5AB7",
"borderColor": "#ccc",
"borderRadius": 5
},
{
"label": "Respondent 2",
"data": [
null,
null,
100001,
null
],
"backgroundColor": "#063F84",
"borderColor": "#ccc",
"borderRadius": 5
},
{
"label": "Respondent 3",
"data": [
null,
null,
null,
488
],
"backgroundColor": "#0D5AB7",
"borderColor": "#ccc",
"borderRadius": 5
},
{
"label": "Respondent 2",
"data": [
null,
null,
null,
3999
],
"backgroundColor": "#063F84",
"borderColor": "#ccc",
"borderRadius": 5
}
],
"labels": [
"2017 Fiscal Year (Jul-Jun) - H2",
"2022 Fiscal Year (Jan-Dec) - H2",
"2023 Fiscal Year (Jan-Dec) - H1",
"2023 Fiscal Year (Jan-Dec) - H2"
],
"yAxisLabel": "Cubic Meters"
}
After all that, I thought there must be a better way…