I want to get information from database and draw a chart. I have this code:
$.post("ajax.php?param=grp", {grp: grp}, function (rply) {
var densityCanvas = document.getElementById("densityChart");
var R = rply;
new Chart(densityCanvas, R);
});
Variable rply contains this:
{
type: "bar",
data: {
labels: ["pitanje 1", "pitanje 2", "Pitanje 3"],
datasets: [
{
label: "Pozitivni",
data: [5, 4, 7],
backgroundColor: "rgba(0, 99, 132, 0.6)",
borderColor: "rgba(0, 99, 132, 1)"
}
,
{
label: "Negativni",
data: [3,1, 5],
backgroundColor: "rgba(99, 132, 0, 0.6)",
borderColor: "rgba(99, 132, 0, 1)"
}
]
},
options: {scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0,
beginAtZero: true
}
}]
}}}
"new Chart" doesn't draw new chart unless this returned JSON is hardcoded. If it's not hardcoded browser gives an error: "Cannot create property 'data' on string...".
If I do: var R = JSON.parse(rply);
It gives me this error: "Unexpected token t in JSON at position 4".