Struggling to create a dynamic dataset for my Chart.js project, specifically with the data obtained from getpromorevenuechart.php
. Here's an example of the dataset:
[{"mmyy":"2019-12","promocode":"promo1","amount":"2776"},{"mmyy":"2020-01","promocode":"promo1","amount":"1245"},{"mmyy":"2020-01","promocode":"promo2","amount":"179"}
]
This is my current code snippet:
function getPromoRChartData() {
var city = document.getElementById("cityselect").value;
$.ajax({
type: 'GET',
url: 'getpromorevenuechart.php',
dataType: 'json',
data: { city:city, },
success: function(response) {
// Code here
}
});
. . .
Desiring the graph to display data in this format:
data: {
labels: ["2019-12", "2020-01"],
datasets: [{
label: 'promo 1',
data: [2776, 1245]
},
{
label: 'promo 2',
data: [0, 179]
}
]
},
Need assistance on iterating through and formatting the data properly for the chart. Any guidance would be appreciated.
Edit:
. . .If I remove the [{}] around dyndatasets
I get the error:
TypeError: Attempted to assign to readonly property.
and if I remove the brackets from dynlabels
, this happens:
https://i.sstatic.net/ei2JQ.png
Oddly enough... copying directly from the console and pasting works fine, so could it be a formatting issue?