我的圖表在同一頁面上運行良好,但是當我放在不同的頁面上時,只有第一個工作正常。Chartjs.org圖表只顯示在一個頁面中
例如: Page1.html
<div class="wrapper">
<canvas id="pieChart" width="200px" height="200px"></canvas>
</div>
Page2.html
<div class="wrapper">
<canvas id="lineChart" width="200px" height="200px"></canvas>
</div>
JS
//page1.html //piechart var pieVar = { type: 'pie', data: { labels: ["Yes", "No"], datasets: [ { data: [60, 40], backgroundColor: [ "#FF6384", "#36A2EB" ], hoverBackgroundColor: [ "#FF6384", "#36A2EB" ] } ] }, options: { scales: { xAxes: [{ display: true }] } } } //page2.html //line chart var lineVar = { type: 'line', data: { labels: ["January", "February", "March", "April", "May", "June", "July"], datasets: [ { label: "My First dataset", fill: true, lineTension: 0.2, backgroundColor: "rgba(75,192,192,0.4)", borderColor: "rgba(75,192,192,1)", borderCapStyle: 'butt', borderDash: [], borderDashOffset: 0.0, borderJoinStyle: 'miter', pointBorderColor: "rgba(75,192,192,1)", pointBackgroundColor: "#fff", pointBorderWidth: 1, pointHoverRadius: 10, pointHoverBackgroundColor: "rgba(255,0,0,1)", pointHoverBorderColor: "rgba(255,0,0,1)", pointHoverBorderWidth: 2, pointRadius: 1, pointHitRadius: 10, data: [65, 59, 80, 81, 56, 55, 40], spanGaps: false, } ] }, options: { scales: { xAxes: [{ display: true }] } } } window.onload = function(){ //piechart var pieCtx = document.getElementById("pieChart"); var myPieChart = new Chart(pieCtx, pieVar); //linechart var lineCtx = document.getElementById("lineChart"); var myLineChart = new Chart(lineCtx, lineVar); };
在這種codepen工作得很好,因爲它是在同一頁.. CODEPEN
這兩個頁面是否執行相同的JavaScript?你如何在每個頁面上加載/執行你的javascript? – jordanwillis
是的,它裝載得很好。我在控制檯btw中得到了這個錯誤.. '未捕獲的類型錯誤:無法在Object.acquireContext(Chart.min.js:14)處讀取屬性'length'爲null (在新的t.Controller中爲 )(Chart.min.js :11) at new t(Chart.min.js:12) at window.onload(theme.min.js?ver = 0.5.6:3)' –