我正在複製Chart.js中的示例,並且卡住了我正在製作的錯誤。無法使用Chart.js初始化餅圖
這裏是我的代碼(從例子中獲取):
<canvas id="myChart" width="400" height="400"></canvas>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src=" https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js" charset="utf-8"></script>
<script type="text/javascript">
// Get the context of the canvas element we want to select
var ctx = document.getElementById("myChart").getContext("2d");
var myNewChart = new Chart(ctx[0]).Pie(data);
var data = [{
value: 300,
color: "#F7464A",
highlight: "#FF5A5E",
label: "Red"
}, {
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
}, {
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
}]
</script>
此代碼給我一個錯誤:
Uncaught TypeError: Cannot read property 'canvas' of undefined
現在,如果我改變myNewChart
VAR到:
var myNewChart = new Chart(ctx).Pie(data);
我得到沒有錯誤,沒有圖表。
我錯過了什麼?我覺得這件事情很明顯......
感謝
你的作品錯字時,我複製它 – FPcond