官網:https://github.com/chartjs/Chart.js
用到的是裡面的chart.js-3.5.0\package\dist\chart.js檔案
編寫測試例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<script src="chart.js"></script>
</head>
<body>
<!--柱狀圖。用div容器包裝,可修改圖表大小、背景顔色-->
<div style="width:400px;height:300px;background-color:#0a073a;display:inline-block;margin-right:50px;">
<canvas id="myChart" width="400" height="300" ></canvas>
</div>
<!--折線圖-->
<div style="width:400px;height:300px;background-color:#0a073a;display:inline-block;">
<canvas id="myChart2" width="400" height="300" ></canvas>
</div>
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["顔值","身材","高冷","貼心","主見","可靠","聰明"],
datasets: [{
label: '特點值',
data: [6.7,6.7,8.0,8.5,6.7,8.6,8.2],
backgroundColor: [
'rgba(255, 99, 132, 0.8)',
'rgba(54, 162, 235, 0.8)',
'rgba(255, 206, 86, 0.8)',
'rgba(75, 192, 192, 0.8)',
'rgba(153, 102, 255, 0.8)',
'rgba(255, 159, 64, 0.8)',
'rgba(208 ,100, 82,0.8)'
],
borderColor: [
'rgba(255,99,132,0.8)',
'rgba(54, 162, 235, 0.8)',
'rgba(255, 206, 86, 0.8)',
'rgba(75, 192, 192, 0.8)',
'rgba(153, 102, 255, 0.8)',
'rgba(255, 159, 64,0.8)',
'rgba(208 ,100, 82,0.8)'
],
borderWidth: 1
}]
},
options: {
//從零開始
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
var ctx = document.getElementById("myChart2").getContext('2d');
var myChart = new Chart(ctx, {
type: "line",
data: {
labels: ["2020.09", "2020.10", "2020.11","2020.12", "2021.01", "2021.02", "2021.03", "2021.04", "2021.05", "2021.06", "2021.07", "2021.08"],
datasets: [{
label: '心動值',
data: [5.1,5.5,6.5,7.3,8.1,8.4,8.7,9.1,9.2,9.3,9.2,9.4],
backgroundColor: [
'rgba(255, 99, 132, 0.8)',
'rgba(54, 162, 235, 0.8)',
'rgba(255, 206, 86, 0.8)',
'rgba(75, 192, 192, 0.8)',
'rgba(153, 102, 255, 0.8)',
'rgba(255, 159, 64, 0.8)',
'rgba(40,177,164,0.8)',
'rgba(206, 51 ,27,0.8)',
'rgba(42 ,187, 60,0.8)',
'rgba(213, 34 ,199,0.8)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)',
'rgba(40 ,177, 164,1)',
'rgba(206, 51 ,27,1)',
'rgba(42 ,187, 60,1)',
'rgba(213, 34 ,199,1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
</script>
</body>
</html>
圖表效果如下:

在學習繪畫圖表時發現的插件,但其實用起來還有點問題,比如圖表大小的設定。