
在现代web应用中,数据可视化是提升用户体验和信息传达效率的关键。堆叠式进度条图表(stacked progress bar chart),作为一种特殊的堆叠条形图,能够直观地展示一个整体由多个部分组成,并且这些部分的累积进度。例如,它可以用于显示一天中机器的不同运行状态(运行、停机、维护),或项目不同阶段的完成情况。为了在html页面中高效且美观地实现这种图表,我们强烈推荐使用chart.js这一强大的javascript图表库。
Chart.js是一个开源的JavaScript图表库,它允许开发者使用HTML5 <canvas> 元素创建各种响应式图表。其主要优势包括:
在开始创建图表之前,您需要将Chart.js库引入到您的HTML页面中。最简单的方式是通过CDN(内容分发网络)引入。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>堆叠式进度条图表</title>
<!-- 引入 Chart.js 库 -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f4f4f4;
margin: 0;
}
.chart-container {
width: 80%;
max-width: 800px;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<div class="chart-container">
<!-- 用于绘制图表的 canvas 元素 -->
<canvas id="stackedProgressBarChart"></canvas>
</div>
<script>
// JavaScript 代码将在这里编写
</script>
</body>
</html>接下来,我们将在 <script> 标签内部编写JavaScript代码来配置和渲染图表。
首先,我们需要获取HTML中 <canvas> 元素的2D渲染上下文。
立即学习“Java免费学习笔记(深入)”;
const ctx = document.getElementById('stackedProgressBarChart').getContext('2d');堆叠式条形图的数据结构是关键。每个“条形”代表一个类别(例如,一天),而每个条形内部的“堆叠部分”则代表该类别下的不同状态或组件。Chart.js通过 datasets 数组来处理这些堆叠部分。每个 dataset 对应一个堆叠层,包含其标签、颜色和对应的数据点。
const data = {
labels: ['机器A', '机器B', '机器C'], // x轴标签,代表不同的机器或日期
datasets: [
{
label: '运行中', // 第一个堆叠层的标签
data: [8, 6, 9], // 对应机器A, B, C的运行小时数
backgroundColor: 'rgba(75, 192, 192, 0.8)', // 运行中的颜色
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
},
{
label: '停机', // 第二个堆叠层的标签
data: [1, 2, 0], // 对应机器A, B, C的停机小时数
backgroundColor: 'rgba(255, 99, 132, 0.8)', // 停机的颜色
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
},
{
label: '维护中', // 第三个堆叠层的标签
data: [0.5, 1.5, 1], // 对应机器A, B, C的维护小时数
backgroundColor: 'rgba(255, 205, 86, 0.8)', // 维护中的颜色
borderColor: 'rgba(255, 205, 86, 1)',
borderWidth: 1
},
{
label: '空闲', // 第四个堆叠层的标签
data: [14.5, 14.5, 14], // 对应机器A, B, C的空闲小时数
backgroundColor: 'rgba(201, 203, 207, 0.8)', // 空闲的颜色
borderColor: 'rgba(201, 203, 207, 1)',
borderWidth: 1
}
]
};在上述示例中,我们模拟了三台机器在一天中(假设24小时)的不同状态。每个 data 数组中的值代表该状态所占用的时间(例如小时数)。请注意,backgroundColor 和 borderColor 可以根据需要重复使用,以表示相同的状态类型。
图表选项用于控制图表的行为和外观。对于堆叠式条形图,最重要的是在 scales 中将 stacked 属性设置为 true。
const options = {
responsive: true, // 使图表响应式
maintainAspectRatio: false, // 允许canvas容器自由调整宽高
plugins: {
title: {
display: true,
text: '机器每日状态概览 (小时)', // 图表标题
font: {
size: 18
}
},
tooltip: {
mode: 'index', // 工具提示模式,显示所有堆叠层的数据
intersect: false // 工具提示不要求精确相交
},
legend: {
display: true, // 显示图例
position: 'top' // 图例位置
}
},
scales: {
x: {
stacked: true, // x轴堆叠
title: {
display: true,
text: '机器名称'
}
},
y: {
stacked: true, // y轴堆叠
beginAtZero: true, // y轴从0开始
title: {
display: true,
text: '小时数'
},
max: 24 // 假设一天24小时,设置y轴最大值
}
}
};最后,使用 new Chart() 构造函数创建图表实例,传入上下文、图表类型、数据和选项。
const myStackedBarChart = new Chart(ctx, {
type: 'bar', // 图表类型为条形图
data: data,
options: options
});将所有代码片段组合起来,完整的HTML文件如下:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>堆叠式进度条图表</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f4f4f4;
margin: 0;
}
.chart-container {
width: 80%;
max-width: 800px;
height: 400px; /* 设置一个固定高度,或通过JS动态调整 */
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
box-sizing: border-box; /* 包含padding和border在内的总宽度/高度 */
}
</style>
</head>
<body>
<div class="chart-container">
<canvas id="stackedProgressBarChart"></canvas>
</div>
<script>
const ctx = document.getElementById('stackedProgressBarChart').getContext('2d');
const data = {
labels: ['机器A', '机器B', '机器C'],
datasets: [
{
label: '运行中',
data: [8, 6, 9],
backgroundColor: 'rgba(75, 192, 192, 0.8)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
},
{
label: '停机',
data: [1, 2, 0],
backgroundColor: 'rgba(255, 99, 132, 0.8)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
},
{
label: '维护中',
data: [0.5, 1.5, 1],
backgroundColor: 'rgba(255, 205, 86, 0.8)',
borderColor: 'rgba(255, 205, 86, 1)',
borderWidth: 1
},
{
label: '空闲',
data: [14.5, 14.5, 14],
backgroundColor: 'rgba(201, 203, 207, 0.8)',
borderColor: 'rgba(201, 203, 207, 1)',
borderWidth: 1
}
]
};
const options = {
responsive: true,
maintainAspectRatio: false,
plugins: {
title: {
display: true,
text: '机器每日状态概览 (小时)',
font: {
size: 18
}
},
tooltip: {
mode: 'index',
intersect: false
},
legend: {
display: true,
position: 'top'
}
},
scales: {
x: {
stacked: true,
title: {
display: true,
text: '机器名称'
}
},
y: {
stacked: true,
beginAtZero: true,
title: {
display: true,
text: '小时数'
},
max: 24 // 假设一天24小时,设置y轴最大值
}
}
};
const myStackedBarChart = new Chart(ctx, {
type: 'bar',
data: data,
options: options
});
</script>
</body>
</html>通过本教程,您应该已经掌握了如何利用Chart.js库在HTML页面中创建和显示堆叠式进度条图表。Chart.js凭借其灵活性和强大的功能,使得复杂的数据可视化变得简单而高效。无论是展示设备运行状态、项目进度,还是其他需要分层展示的数据,堆叠式条形图都是一个极佳的选择。建议您进一步查阅Chart.js的官方文档,探索更多高级配置和自定义选项,以满足您特定的可视化需求。
以上就是JavaScript实现堆叠式进度条图表:Chart.js教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号