<!doctype html> <html> <head> <title>Line Chart</title> <script src="JS/Chart.min.js"></script> </head> <body> <div> <canvas id="canvas"></canvas> </div> <script> var randomScalingFactor = function () { return Math.round(Math.random() * 100) }; var lineChartData = { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [ { 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: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()] } ] } window.onload = function () { var ctx = document.getElementById("canvas").getContext("2d"); window.myLine = new Chart(ctx).Line(lineChartData, { responsive: true }); } </script> </body> </html>
注意:數據源labels和data是數組,不能拼接成一個字符串放進去,沒效果。
Chart有六種樣式可供選擇,效果挺好的。此外,還可以考慮用HighCharts和ECharts實現圖表展示。