I have a stacked bar chart that I built using the Chart.js library JavaScript
, and I would like it to be ordered as follows: elements with the highest value at the top of the bar, elements with the lowest value at the bottom of the bar. bottom, that is, in descending order.
I guess it can be done using some of the plugins Chart.js
, but I still can't figure it out.
The following is the graph that I am generating and it is generated out of order:
The following is the code fragment with which I am building the graph:
myNewChart = new Chart(ctx, {
type: 'horizontalBar',
data: { labels, datasets },
options: {
maintainAspectRatio: false,
showAllTooltips: true,
scales: {
xAxes: [
{
stacked: true,
display: true,
ticks: {
beginAtZero: true,
}
}
],
yAxes: [
{
stacked: true,
display: true,
reverse: false,
ticks: { }
}
]
},
legend: {
position: 'bottom',
padding: 5,
labels: {
pointStyle: 'circle',
usePointStyle: true
}
},
plugins: {
datalabels: {
color: "#333333",
font: {
weight: 'bold',
size: 16,
}
}
},
},
});
Version: Chart.js
v2.8.0
Update:
I add objects datasets
anddatasetsObject
const datasetsObject = data.DashboardTicketList.reduce((obj, item) => {
obj[item.TicketsClasificationType] = obj[item.TicketsClasificationType] || {};
obj[item.TicketsClasificationType][item.TicketsAsignedTo] = item.TicketsCount;
return obj;
}, {});
const datasets = Object.keys(datasetsObject).map((name) => ({
label: name,
backgroundColor: colors[name],
data: labels.map((l) => datasetsObject[name][l])
}));
Update 2:
The values of thedatasets
const colors = {
"ABIERTO": "#F76363",
"ASIGNADO": "#F7A65C",
"EN PROCESO": "#F2CB5F",
"EN ESPERA USUARIO": "#B283ED",
"TERMINADO": "#4285f4",
"CERRADO": "#6CE5CE",
};
Update 3:
When executing the ordering algorithm sort
as indicated in one of the answers, it no longer represents any data in the graph
const datasets = Object.keys(datasetsObject).map((name) => ({
label: name,
backgroundColor: colors[name],
data: labels.map((l) => datasetsObject[name][l]).sort
}));
I see you're using the logic I gave you a while back to generate the
datasets
andlabels
. Well, taking the same snippet that I gave you at the time, I am going to vary it to order themlabels
from highest to lowest, taking into account the sum of their values. To do so, one of the solutions is that instead of generating them in the previous way, an object could be generated with the totals bylabel
, create aarray
with said object, organize thatarray
from highest to lowest and then extract thoselabels
from thatarray
and thus they will be ordered from highest to lowest taking into account the sum of all its values. I'll explain it step by step:Given the following
array
with the query:1 - Create a totals object by
label
:The above will return an object like the following:
2 - Create an array of
labels
y totals:Which will return a
array
like the following:3 - Order the
array
from highest to lowest taking into account the propertyvalue
:Which will return the
array
ordered as follows:4 - Extract those
labels
from thearray
ordered:What it will return:
5 - Use this
array
fromlabels
to generate the chart:Sort Algorithm
The sort algorithm: which is implemented in java script arrays, allows you to organize values automatically.
Example:
In this case our variables are:
GroupValores
smallest to largest amount of data:A:
[[2,1],[2,5,3],[10,3,6,2]]
GroupValores
:A:
[[2,3,5],[1,2],[2,3,6,10]]
Where you build the dataset you must order the values of each one with the Sort() function of the Javascript Arrays
With that the value of the array
data
should be sorted from smallest to largest