I'm currently working on generating a bar stack graph using the chart.js JavaScript library. My JavaScript array contains the following data:
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"}
The AJAX request retrieves this data and now I need to dynamically set it in order to create 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 want the data array with date 01/01/20 to be shown as the first label on the X-axis, with Category A displayed at the bottom of the stack with data 7433, Category B in the middle with data 774, and Category C at the top with data 5933. This pattern should continue for the next sets of data.
I aim for a dynamic approach that adjusts automatically based on the changing data. The Y-axis should adjust its minimum and maximum values accordingly for a clear representation of the bar stack graph.
You can view an example of the expected outcome in this JS Fiddle: https://jsfiddle.net/1eq8w0Lx/. I need to replace the hardcoded values with dynamic ones from my array. How can I achieve this?