饼图与环形图
饼图和环形图用于展示各部分占整体的比例关系。通过使用 Chart.js,可以轻松定制扇形颜色和图表样式,实现清晰的视觉效果。
基础饼图示例
下面示例展示了如何创建一个饼图,突出各部分比例。
(function () {
const ctx = document.getElementById("pieChart").getContext("2d");
const pieChart = new Chart(ctx, {
type: "pie",
data: {
labels: ["红", "蓝", "黄", "绿", "紫"],
datasets: [
{
label: "样本数据",
data: [12, 19, 3, 5, 2],
backgroundColor: [
"rgba(255, 99, 132, 0.7)",
"rgba(54, 162, 235, 0.7)",
"rgba(255, 206, 86, 0.7)",
"rgba(75, 192, 192, 0.7)",
"rgba(153, 102, 255, 0.7)",
],
},
],
},
options: {
responsive: true,
plugins: {
legend: { display: true },
tooltip: { enabled: true },
},
},
});
})();
基础环形图示例
环形图是饼图的变体,中心空心,视觉更轻盈。
(function () {
const ctx = document.getElementById("doughnutChart").getContext("2d");
const doughnutChart = new Chart(ctx, {
type: "doughnut",
data: {
labels: ["红", "蓝", "黄", "绿", "紫"],
datasets: [
{
label: "样本数据",
data: [12, 19, 3, 5, 2],
backgroundColor: [
"rgba(255, 99, 132, 0.7)",
"rgba(54, 162, 235, 0.7)",
"rgba(255, 206, 86, 0.7)",
"rgba(75, 192, 192, 0.7)",
"rgba(153, 102, 255, 0.7)",
],
},
],
},
options: {
responsive: true,
plugins: {
legend: { display: true },
tooltip: { enabled: true },
},
},
});
})();
源码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>Chart.js 饼图与环形图示例</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
max-width: 700px;
margin: 2rem auto;
font-family: Arial, sans-serif;
background: #fafafa;
padding: 1rem;
display: flex;
gap: 2rem;
flex-wrap: wrap;
justify-content: center;
}
canvas {
background: #fff;
border: 1px solid #ccc;
display: block;
}
</style>
</head>
<body>
<canvas id="pieChart" width="350" height="350"></canvas>
<canvas id="doughnutChart" width="350" height="350"></canvas>
<script>
(function () {
const pieCtx = document.getElementById(‘pieChart’).getContext(‘2d’);
new Chart(pieCtx, {
type: ‘pie’,
data: {
labels: [‘红’, ‘蓝’, ‘黄’, ‘绿’, ‘紫’],
datasets: [{
label: ‘样本数据’,
data: [12, 19, 3, 5, 2],
backgroundColor: [
‘rgba(255, 99, 132, 0.7)’,
‘rgba(54, 162, 235, 0.7)’,
‘rgba(255, 206, 86, 0.7)’,
‘rgba(75, 192, 192, 0.7)’,
‘rgba(153, 102, 255, 0.7)’
]
}]
},
options: {
responsive: true,
plugins: {
legend: { display: true },
tooltip: { enabled: true }
}
}
});
const doughnutCtx = document.getElementById('doughnutChart').getContext('2d');
new Chart(doughnutCtx, {
type: 'doughnut',
data: {
labels: ['红', '蓝', '黄', '绿', '紫'],
datasets: [{
label: '样本数据',
data: [12, 19, 3, 5, 2],
backgroundColor: [
'rgba(255, 99, 132, 0.7)',
'rgba(54, 162, 235, 0.7)',
'rgba(255, 206, 86, 0.7)',
'rgba(75, 192, 192, 0.7)',
'rgba(153, 102, 255, 0.7)'
]
}]
},
options: {
responsive: true,
plugins: {
legend: { display: true },
tooltip: { enabled: true }
}
}
});
})();
</script>
</body>
</html>
相关内容
- 什么是 Chart.js? Chart.js 是一个简单易用且功能强大的 JavaScript 图表库,支持多种常见图表类型,帮助开发者快速创建美观且响应式的数据可视化效果,适合初学者和专...
- 安装与引入 介绍如何在项目中安装和引入 Chart.js,包括通过 CDN 快速使用和通过 npm 安装到前端构建环境的多种方式,帮助快速搭建图表开发环境。
- 基本结构与初始化 讲解如何创建图表的基本结构与初始化流程,包括配置画布、准备数据和实例化图表的步骤,帮助快速搭建图表展示。
- 示例:折线图 通过示例讲解如何创建折线图,涵盖数据配置、样式设置和基本交互,帮助快速掌握折线图的绘制技巧。
- 折线图 Line 介绍如何使用 Chart.js 创建折线图,详细讲解数据配置、样式调整及交互功能,帮助快速制作趋势图表。
- 柱状图 Bar 介绍如何使用 Chart.js 创建柱状图,详细讲解数据配置、颜色设置及图表样式,帮助快速绘制直观的条形数据展示。