Chartjs-How to insert dynamic data from sql into chartjs stacked bar chart javascript

1👍

The issue is that what you are constructing is a String, but the data you need to pass to chart.js should be a JavaScript object. See, by taking e.g. the string ‘{‘ and concatenating it with other values, what you get is another string, which chart.js can’t work with.

Instead, what you need to do is construct your data as an object, something like this:

var inchartdatasets = [];
for (var i = 0; i < leninchart; i++) {
    var data = {
        label: inchartlebals[i],
        data: inchart[i],
        backgroundColor: color
    };
    inchartdatasets.push(data);
}

This MDN page might also be a helpful resource to get a better understanding of working with JavaScript objects.

Leave a comment