I am trying to create a bar stack graph using chart.js javascript library. I currently have an array in javascript that contains the following :
0: {labels: "01/01/2020 00:00:00", data: 7433, category: "A"}
1: {labels: "01/01/2020 00:00:00", data: 774, category: "B"}
2: {labels: "01/01/2020 00:00:00", data: 5993, category: "C"}
3: {labels: "30/01/2020 00:00:00", data: 7624, category: "A"}
4: {labels: "30/01/2020 00:00:00", data: 900, category: "B"}
5: {labels: "30/01/2020 00:00:00", data: 5865, category: "C"}
6: {labels: "18/02/2020 00:00:00", data: 7161, category: "A"}
7: {labels: "18/02/2020 00:00:00", data: 1005, category: "B"}
8: {labels: "18/02/2020 00:00:00", data: 5940, category: "C"}
Below is the AJAX request to put the data into an array and now i need to dynamically set the data to output a stack chart
// STACK BAR CHART
var stackBarData = [];
var stackBarLabels = [];
$.ajax({
type: "POST",
async: false,
url: "ExecView.aspx/ReturnStackBarData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var stackbarDatas = data.d;
stackBarData = new Array(stackbarDatas.length);
console.log(stackBarData);
for (i = 0; i < stackbarDatas.length; i++) {
stackBarData[i] = { labels: stackbarDatas[i].label, data: stackbarDatas[i].data, category: stackbarDatas[i].category };
}
console.log(stackBarData); // ARRAY OUTPUT ABOVE
}
});
var BarpopData = {
datasets: [{
data: stackBarData
}],
};
I expect all the data in the array with 01/01/20 to be my first label on the X axis and Category A to be the lowest stack with data 7433. Then I expect category B to be middle stack with data 774. Then i expect category C to be the highest stack with data 5933. The i expect another bar stack with date 30/01/2020 data.
I need this to be dynamic looking at the array as data always changes.
How can I dynamically do this - ensuring min and max Y axis is auto so is shows a good bar stack graph?
I have a JS Fiddle: https://jsfiddle.net/1eq8w0Lx/ of what i expect the outcome to look like. I need the hard coded values to be dynamic from my array. How do i achieve this output?