Chart.js
是一个流行的开源 JavaScript 图表库,用于在网页上创建各种类型的图表(如折线图、柱状图、饼图等)。它简单易用,支持高度定制化,并且兼容现代浏览器。
以下是 Chart.js
的应用实例,展示如何创建不同类型的图表。
1. 引入 Chart.js
首先,需要在 HTML 文件中引入 Chart.js
库。可以通过 CDN 或本地文件引入。
使用 CDN
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
使用本地文件
<script src="path/to/chart.js"></script>
2. 创建画布
在 HTML 中创建一个 <canvas>
元素,用于渲染图表。
<canvas id="myChart" width="400" height="200"></canvas>
3. 创建图表
使用 JavaScript 初始化图表,并配置数据和选项。
4. 示例:折线图
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chart.js 折线图示例</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="lineChart" width="400" height="200"></canvas>
<script>
const ctx = document.getElementById('lineChart').getContext('2d');
const lineChart = new Chart(ctx, {
type: 'line', // 图表类型
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], // X 轴标签
datasets: [{
label: 'My First Dataset', // 数据集标签
data: [65, 59, 80, 81, 56, 55, 40], // 数据
backgroundColor: 'rgba(75, 192, 192, 0.2)', // 背景颜色
borderColor: 'rgba(75, 192, 192, 1)', // 边框颜色
borderWidth: 1 // 边框宽度
}]
},
options: {
scales: {
y: {
beginAtZero: true // Y 轴从 0 开始
}
}
}
});
</script>
</body>
</html>
5. 示例:柱状图
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chart.js 柱状图示例</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="barChart" width="400" height="200"></canvas>
<script>
const ctx = document.getElementById('barChart').getContext('2d');
const barChart = new Chart(ctx, {
type: 'bar', // 图表类型
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'], // X 轴标签
datasets: [{
label: 'My First Dataset', // 数据集标签
data: [12, 19, 3, 5, 2, 3], // 数据
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
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)'
],
borderWidth: 1 // 边框宽度
}]
},
options: {
scales: {
y: {
beginAtZero: true // Y 轴从 0 开始
}
}
}
});
</script>
</body>
</html>
6. 示例:饼图
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chart.js 饼图示例</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="pieChart" width="400" height="200"></canvas>
<script>
const ctx = document.getElementById('pieChart').getContext('2d');
const pieChart = new Chart(ctx, {
type: 'pie', // 图表类型
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'], // 标签
datasets: [{
label: 'My First Dataset', // 数据集标签
data: [12, 19, 3, 5, 2, 3], // 数据
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
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)'
],
borderWidth: 1 // 边框宽度
}]
},
options: {
responsive: true, // 图表自适应
plugins: {
legend: {
position: 'top', // 图例位置
},
title: {
display: true,
text: 'Pie Chart Example' // 图表标题
}
}
}
});
</script>
</body>
</html>
7. 示例:动态更新图表
可以通过更新数据动态刷新图表。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chart.js 动态更新示例</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="dynamicChart" width="400" height="200"></canvas>
<button id="updateButton">更新数据</button>
<script>
const ctx = document.getElementById('dynamicChart').getContext('2d');
const dynamicChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'My First Dataset',
data: [65, 59, 80, 81, 56, 55, 40],
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
// 更新数据
document.getElementById('updateButton').addEventListener('click', () => {
const newData = [Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100];
dynamicChart.data.datasets[0].data = newData;
dynamicChart.update(); // 更新图表
});
</script>
</body>
</html>
8. 总结
-
Chart.js
是一个功能强大且易于使用的图表库,支持多种图表类型。 -
通过简单的配置,可以创建折线图、柱状图、饼图等。
-
支持动态更新数据,适用于实时数据展示场景。
通过以上示例,你可以快速上手 Chart.js
并在项目中应用。