<script type="text/javascript">
$(function() {
$.ajax({
type: "POST",
url: "BillnAmount",
cache: false,
dataType: 'json',
success: function(data) {
console.log(data);
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}
]
};
var ctx = $("#myChart").get(0).getContext("2d");
var myNewChart = new Chart(ctx);
var myLineChart = new Chart(ctx).Line(data);
}
});
});
</script>
我正在使用ajax調用url並以json格式獲取其響應。使用json響應在chart.js中繪製折線圖
在ajax調用我正在繪製與Chart.js
正在工作的折線圖。
現在我想改變上面的圖表值與JSON響應數據的json響應值
{"billDetails":
[{"invoiceDate":"2014-07-24T00:00:00","totalAmount":1031.00,"totalBills":1},
{"invoiceDate":"2014-07-15T00:00:00","totalAmount":7281.00,"totalBills":3},
{"invoiceDate":"2014-07-12T00:00:00","totalAmount":14841.00,"totalBills":7},
{"invoiceDate":"2014-07-11T00:00:00","totalAmount":1294.00,"totalBills":3},
{"invoiceDate":"2014-07-10T00:00:00","totalAmount":674.00,"totalBills":3},
{"invoiceDate":"2014-07-09T00:00:00","totalAmount":2.00,"totalBills":1},
{"totalAmount":114.00,"totalBills":10}]}
我應該做什麼樣的變化,所以必須從JSON響應顯示數據
編輯: 我試過這個
var data1;
$.each(data.billDetails, function(i, value) {
data1 = {
labels: data.billDetails[i].invoiceDate,
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: data.billDetails[i].totalBills
},
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: data.billDetails[i].totalAmount
}
]
};
});
在控制檯上顯示如下
個Uncaught TypeError: Cannot read property 'x' of undefined
我的格式數據
2014-07-24T00:00:00 1 1031
2014-07-15T00:00:00 3 7281
2014-07-12T00:00:00 7 14841
2014-07-11T00:00:00 3 1294
2014-07-10T00:00:00 3 674
2014-07-09T00:00:00 11 116
它是隻顯示如下圖
這不是一個代碼寫入服務。你有什麼嘗試?你的問題在哪裏? – RoToRa
@RoToRa請參閱我的更新 – xrcwrn
錯誤意味着你有一個名爲'x'的變量/屬性。你需要找出錯誤發生在哪一行。 – RoToRa