1
我的Charts.js折線圖中出現了一個奇怪的問題。頁面加載時的Charts.js呈現問題
有沒有其他人遇到這個問題和知道的方法來解決它?
我的圖表的JavaScript代碼如下。請讓我知道,如果我需要改變它以任何方式解決這個問題。
function drawLineChart() {
var dates = [];
var clientCosts = [];
$.ajax({
url: '../inc/wip-data.php',
dataType: 'json'
}).done(function(data) {
data.forEach(function(item) {
dates.push(item.date);
clientCosts.push(item.clientCostsTotal);
});
});
console.log(dates, clientCosts);
var chartData = {
labels: dates,
datasets: [{
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointHighlightStroke: "rgba(151,187,205,1)",
data: clientCosts
}]
};
// Get the context of the canvas element we want to select
var ctx = document.getElementById("myChart").getContext("2d");
// Instantiate a new chart
var myChart = new Chart(ctx, {
responsive: true,
type: 'line',
data: chartData,
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
drawLineChart();
This issue is not replicating in JS Bin.的JS斌和我的地方共建的唯一區別是AJAX調用的數據,這樣使我相信AJAX調用是什麼原因造成的問題。任何指導什麼可能被稱爲這個問題,將不勝感激。
現在的腳本是:'$阿賈克斯({ 網址: '../inc/wip-data.php', 數據類型: 'JSON' })。完成(功能(數據){ data.forEach(function(item){ dates.push(item.date); clientCosts.push(item.clientCostsTotal); myChart.update(); }); });' 它按預期工作。 – Liz
酷!很高興我能幫上忙! – Sergiu