
在项目管理、销售业绩追踪或个人目标达成等场景中,我们常常需要清晰地了解当前进展以及距离最终目标还有多远。传统的柱状图可以展示当前值,但如果能直观地将“已完成”与“待完成”以堆叠的形式呈现,将大大提高数据的可读性和决策效率。本文将详细介绍如何使用强大的javascript图表库chart.js来实现这种带有目标进度的堆叠柱状图。
Chart.js的bar类型图表天生支持多数据集的堆叠展示。其核心思想是:
通过这种方式,当一个任务达到目标时,代表“待完成”的柱状部分将消失;而当任务未达目标时,它会以不同的颜色清晰地显示出还需要多少努力。
下面我们将通过具体的代码示例,分步讲解如何构建这样的图表。
首先,您需要在HTML页面中创建一个canvas元素作为图表的渲染区域,并确保已引入Chart.js库。
<!DOCTYPE html>
<html>
<head>
<title>Chart.js 目标进度堆叠柱状图</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body { font-family: sans-serif; }
.chart-container {
width: 600px;
height: 400px;
margin: 20px auto;
border: 1px solid #eee;
box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<div class="chart-container">
<canvas id="progressChart"></canvas>
</div>
<script>
// Chart.js 代码将在这里编写
</script>
</body>
</html>接下来,在JavaScript中定义您的初始数据结构。假设我们有三个类别('Passed', 'Failed', 'In Progress'),以及它们对应的当前值。
(function() {
const ctx = document.getElementById("progressChart").getContext('2d');
const initialData = {
labels: ['通过', '失败', '进行中'],
datasets: [{
label: '当前进度',
data: [10, 5, 80], // 示例数据:当前值
backgroundColor: [
"rgba(75, 192, 192, 0.8)", // 绿色系
"rgba(255, 99, 132, 0.8)", // 红色系
"rgba(255, 205, 86, 0.8)" // 黄色系
],
}]
};
// 后续代码将在此处添加
})();设定一个目标值(例如60),然后遍历当前进度数据,计算每个类别距离目标还差多少。如果当前值已达到或超过目标,则差值为0。
(function() {
const ctx = document.getElementById("progressChart").getContext('2d');
const targetValue = 60; // 设定目标值
const initialData = {
labels: ['通过', '失败', '进行中'],
datasets: [{
label: '当前进度',
data: [10, 5, 80],
backgroundColor: [
"rgba(75, 192, 192, 0.8)",
"rgba(255, 99, 132, 0.8)",
"rgba(255, 205, 86, 0.8)"
],
}]
};
// 计算距离目标还差多少的数据
const remainingData = initialData.datasets[0].data.map(currentValue => {
return currentValue >= targetValue ? 0 : targetValue - currentValue;
});
// 后续代码将在此处添加
})();将计算出的remainingData作为第二个数据集添加到initialData.datasets数组中。这个数据集将使用一种不同的颜色(例如鲜艳的红色)来突出显示“待完成”的部分。
关键一步:为了确保这两个数据集正确堆叠,您需要在图表的options中配置scales下的x轴和y轴的stacked属性为true。
(function() {
const ctx = document.getElementById("progressChart").getContext('2d');
const targetValue = 60; // 设定目标值
const initialData = {
labels: ['通过', '失败', '进行中'],
datasets: [{
label: '当前进度',
data: [10, 5, 80],
backgroundColor: [
"rgba(75, 192, 192, 0.8)",
"rgba(255, 99, 132, 0.8)",
"rgba(255, 205, 86, 0.8)"
],
}]
};
const remainingData = initialData.datasets[0].data.map(currentValue => {
return currentValue >= targetValue ? 0 : targetValue - currentValue;
});
// 添加第二个数据集:距离目标
initialData.datasets.push({
label: '距离目标',
backgroundColor: 'rgba(255, 0, 0, 0.6)', // 使用红色突出显示剩余量
data: remainingData
});
// 初始化Chart.js图表
const progressChart = new Chart(ctx, {
type: 'bar', // 指定图表类型为柱状图
data: initialData,
options: {
responsive: true,
maintainAspectRatio: false, // 允许容器控制宽高
scales: {
x: {
stacked: true, // X轴堆叠
title: {
display: true,
text: '类别'
}
},
y: {
stacked: true, // Y轴堆叠
beginAtZero: true, // Y轴从0开始
max: targetValue, // 设置Y轴最大值为目标值,使图表更聚焦
title: {
display: true,
text: '值'
}
}
},
plugins: {
tooltip: {
mode: 'index',
intersect: false
},
title: {
display: true,
text: '任务进度与目标达成情况 (目标: ' + targetValue + ')'
}
}
}
});
})();将以上所有部分整合,您将得到一个完整的HTML文件,可以直接在浏览器中运行:
<!DOCTYPE html>
<html>
<head>
<title>Chart.js 目标进度堆叠柱状图</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body { font-family: sans-serif; }
.chart-container {
width: 600px;
height: 400px;
margin: 20px auto;
border: 1px solid #eee;
box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
background-color: #fff;
padding: 15px;
border-radius: 8px;
}
</style>
</head>
<body>
<div class="chart-container">
<canvas id="progressChart"></canvas>
</div>
<script>
(function() {
const ctx = document.getElementById("progressChart").getContext('2d');
const targetValue = 60; // 设定目标值
const chartData = {
labels: ['通过', '失败', '进行中'],
datasets: [{
label: '当前进度',
data: [10, 5, 80], // 示例数据:当前值
backgroundColor: [
"rgba(75, 192, 192, 0.8)", // 绿色系
"rgba(255, 99, 132, 0.8)", // 红色系
"rgba(255, 205, 86, 0.8)" // 黄色系
],
borderColor: [
"rgba(75, 192, 192, 1)",
"rgba(255, 99, 132, 1)",
"rgba(255, 205, 86, 1)"
],
borderWidth: 1
}]
};
// 计算距离目标还差多少的数据
const remainingData = chartData.datasets[0].data.map(currentValue => {
return currentValue >= targetValue ? 0 : targetValue - currentValue;
});
// 添加第二个数据集:距离目标
chartData.datasets.push({
label: '距离目标',
backgroundColor: 'rgba(255, 0, 0, 0.6)', // 使用红色突出显示剩余量
borderColor: 'rgba(255, 0, 0, 1)',
borderWidth: 1,
data: remainingData
});
// 初始化Chart.js图表
const progressChart = new Chart(ctx, {
type: 'bar', // 指定图表类型为柱状图
data: chartData,
options: {
responsive: true,
maintainAspectRatio: false, // 允许容器控制宽高
scales: {
x: {
stacked: true, // X轴堆叠
title: {
display: true,
text: '类别'
}
},
y: {
stacked: true, // Y轴堆叠
beginAtZero: true, // Y轴从0开始
max: targetValue, // 设置Y轴最大值为目标值,使图表更聚焦
title: {
display: true,
text: '值'
}
}
},
plugins: {
tooltip: {
mode: 'index',
intersect: false,
callbacks: {
label: function(context) {
let label = context.dataset.label || '';
if (label) {
label += ': ';
}
if (context.parsed.y !== null) {
label += context.parsed.y;
}
return label;
}
}
},
title: {
display: true,
text: '任务进度与目标达成情况 (目标: ' + targetValue + ')',
font: {
size: 16
}
},
legend: {
position: 'top',
}
}
}
});
})();
</script>
</body>
</html>通过数据预处理和Chart.js的多数据集堆叠功能,我们可以轻松创建出直观且富有洞察力的目标进度堆叠柱状图。这种方法不仅清晰地展示了当前成就,还以视觉化的方式突出了距离目标的差距,为决策者提供了有价值的信息。您可以根据具体需求调整数据计算逻辑、颜色方案和图表选项,以构建出最适合您应用场景的自定义图表。
以上就是使用Chart.js创建带目标进度的堆叠柱状图的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号