如何在chartJs中传递值,其中我有一个来自我的data.php文件的json结果
我的Json
[
{
"Login_Funnel":"6",
"Playlist_Funnel_1":"1",
"Logout_Funnel_2":"0"
}
]
在ChartJS脚本中就像
$(document).ready(function() {
new Chart($("#chart"), {
type: 'bar',
data: {
labels: ["Beta Value", "Charlie Value", "Delta Value"],
datasets: [{
label: 'Awesome Dataset',
data: [ 302, 175, 50],
backgroundColor: "rgba(75, 192, 192, 1)"
}]
},
}
}
我需要将值替换为
标签:[“Login_Funnel”,“Playlist_Funnel_1”,“Logout_Funnel_2”]
和
数据:[6,1,0]
由于我是chartjs的新手,我们将不胜感激
提前谢谢
答案 0 :(得分:0)
你可以通过data.php遍历json返回并在options对象上设置数据,请检查jsfiddle
$(document).ready(function() {
var options = {
type: 'bar',
data: {
labels: ["Beta Value", "Charlie Value", "Delta Value"],
datasets: [{
label: 'Awesome Dataset',
data: [ 302, 175, 50],
backgroundColor: "rgba(75, 192, 192, 1)"
}]
}
};
var chart = new Chart($("#chart"), options);
var data = [
{
"Login_Funnel":"6",
"Playlist_Funnel_1":"1",
"Logout_Funnel_2":"0"
}
];
options.data.labels = []; //reset the labels
options.data.datasets = [{
label: 'Awesome Dataset',
data: [],
backgroundColor: "rgba(75, 192, 192, 1)"
}]; // reset the previous data
for (var i in data) { // iterate the data array
for (var y in data[i]) { //iterate the object
if (i == 0) {
options.data.labels.push(y);
}
options.data.datasets[i].data.push(data[i][y]);
}
}
chart.update(); // update the chart with the new data
});