我在Chartjs上工作并从ajax中获取json:
var myData = [];
$.ajax({
method: "GET",
cache: false,
url: "charts/applieschart.php",
success: function (data) {
myData = data;
}
});
接着在2个不同的数组上解析它们:
var chartjsLabel = [];
var chartjsData = [];
for (var i = 0; i < myData.length; i++) {
chartjsData.push(myData[i].count);
chartjsLabel.push(myData[i].date);
}
而不是我把它放在标签和数据中:
var barChartData = {
labels: chartjsLabel,
datasets: [{
fillColor: "rgba(220,280,220,0.5)",
strokeColor: "rgba(220,220,220,1)",
data: chartjsData
}]
};
看起来不错,但它不起作用。我的json数据看起来像:
[{"date":"12.2014","count":"62"},{"date":"11.2014","count":"58"}]
如果我将myData
变量中的clear json替换为ajax:
var myData = [{"date":"12.2014","count":"62"},{"date":"11.2014","count":"58"}];
它完美无缺。我做错了什么?
答案 0 :(得分:0)
您无法从
asynchronous
本质上回复callback
,使用barChartData
函数或在ajax成功处理程序中初始化$.ajax({ method: "GET", cache: false, url: "charts/applieschart.php", success: function(myData) { var chartjsLabel = []; var chartjsData = []; for (var i = 0; i < myData.length; i++) { chartjsData.push(myData[i].count); chartjsLabel.push(myData[i].date); } var barChartData = { labels: chartjsLabel, datasets: [{ fillColor: "rgba(220,280,220,0.5)", strokeColor: "rgba(220,220,220,1)", data: chartjsData }] }; } });
。
试试这个:
User, 1,2,3,4,5,6,7,8,9
Joe, 34,3,32,1,3
John, 32,2
Tom, 98,34,23
Dave, 56, 53, 32,1,22,6,5