堆疊進度條圖表是一種高效的數(shù)據(jù)可視化工具,它能夠在一個條形中展示多個組成部分的比例或貢獻,從而清晰地呈現(xiàn)整體的構(gòu)成和各部分的相對大小。例如,在工業(yè)場景中,可以使用此類圖表來表示一臺機器在一天內(nèi)不同運行狀態(tài)(運行、停機、維護、空閑)所占用的時間比例;在項目管理中,則可以展示不同任務(wù)階段的完成進度。其主要優(yōu)勢在于直觀性強、信息密度高,且能夠靈活地通過顏色區(qū)分不同狀態(tài),即使顏色重復(fù)使用也能保持良好的可讀性。
為了在網(wǎng)頁中實現(xiàn)這種圖表,我們通常會借助成熟的JavaScript圖表庫。在眾多選擇中,Chart.js以其輕量級、易用性和強大的功能集脫穎而出,特別適合創(chuàng)建各類交互式圖表,包括我們所需的堆疊條形圖。
在開始編寫代碼之前,我們需要將Chart.js庫引入到HTML頁面中。最簡單的方式是通過CDN(內(nèi)容分發(fā)網(wǎng)絡(luò))引入。將以下<script>標(biāo)簽添加到HTML文件的<head>或<body>底部:
<!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> /* 可選:為圖表容器添加一些樣式 */ .chart-container { width: 80%; margin: 50px auto; } </style> </head> <body> <div class="chart-container"> <canvas id="stackedProgressBarChart"></canvas> </div> <script> // JavaScript 代碼將在這里編寫 </script> </body> </html>
在HTML中,我們需要一個<canvas>元素作為Chart.js渲染圖表的畫布。為這個畫布指定一個唯一的id,以便在JavaScript中引用它。
立即學(xué)習(xí)“Java免費學(xué)習(xí)筆記(深入)”;
<div class="chart-container"> <canvas id="stackedProgressBarChart"></canvas> </div>
接下來,我們將使用JavaScript來定義圖表的數(shù)據(jù)和配置選項。一個堆疊條形圖的核心在于其data結(jié)構(gòu)和options中的scales配置。
為了模擬堆疊進度條,我們將創(chuàng)建一個包含多個數(shù)據(jù)集(datasets)的data對象。每個數(shù)據(jù)集代表堆疊條形圖中的一個“層”或一個狀態(tài)。例如,我們可以模擬一臺機器在一天中的不同狀態(tài)時長。
const ctx = document.getElementById('stackedProgressBarChart').getContext('2d'); const chartData = { labels: ['機器A'], // 標(biāo)簽可以代表不同的機器、日期或其他分類 datasets: [ { label: '運行中', data: [10], // 機器A運行了10小時 backgroundColor: 'rgba(75, 192, 192, 0.8)', // 運行狀態(tài)的顏色 borderColor: 'rgba(75, 192, 192, 1)', borderWidth: 1 }, { label: '停機', data: [5], // 機器A停機5小時 backgroundColor: 'rgba(255, 99, 132, 0.8)', // 停機狀態(tài)的顏色 borderColor: 'rgba(255, 99, 132, 1)', borderWidth: 1 }, { label: '維護中', data: [3], // 機器A維護3小時 backgroundColor: 'rgba(255, 205, 86, 0.8)', // 維護狀態(tài)的顏色 borderColor: 'rgba(255, 205, 86, 1)', borderWidth: 1 }, { label: '空閑', data: [6], // 機器A空閑6小時 backgroundColor: 'rgba(201, 203, 207, 0.8)', // 空閑狀態(tài)的顏色 borderColor: 'rgba(201, 203, 207, 1)', borderWidth: 1 } ] };
說明:
為了使條形圖堆疊顯示,我們需要在options中特別配置scales屬性。
const chartOptions = { responsive: true, // 使圖表響應(yīng)式調(diào)整大小 maintainAspectRatio: false, // 允許圖表在響應(yīng)式時改變寬高比 indexAxis: 'y', // 將條形圖的方向設(shè)置為水平(Y軸為索引軸) plugins: { title: { display: true, text: '機器A日狀態(tài)堆疊進度條', font: { size: 18 } }, tooltip: { mode: 'index', intersect: false } }, scales: { x: { stacked: true, // X軸堆疊 title: { display: true, text: '時間 (小時)' }, beginAtZero: true // X軸從0開始 }, y: { stacked: true, // Y軸堆疊 title: { display: true, text: '機器' } } } };
說明:
將數(shù)據(jù)和選項組合,創(chuàng)建新的Chart實例。
new Chart(ctx, { type: 'bar', // 指定圖表類型為條形圖 data: chartData, options: chartOptions });
將上述JavaScript代碼片段整合到HTML文件的<script>標(biāo)簽中,即可生成一個堆疊進度條圖表。
<!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: 90%; /* 調(diào)整寬度以適應(yīng)內(nèi)容 */ max-width: 800px; /* 最大寬度 */ background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); } canvas { height: 200px !important; /* 設(shè)置畫布高度,!important 確保覆蓋默認(rèn)樣式 */ } </style> </head> <body> <div class="chart-container"> <canvas id="stackedProgressBarChart"></canvas> </div> <script> document.addEventListener('DOMContentLoaded', function() { const ctx = document.getElementById('stackedProgressBarChart').getContext('2d'); const chartData = { labels: ['機器A'], // 標(biāo)簽可以代表不同的機器、日期或其他分類 datasets: [ { label: '運行中', data: [10], // 機器A運行了10小時 backgroundColor: 'rgba(75, 192, 192, 0.8)', // 運行狀態(tài)的顏色 borderColor: 'rgba(75, 192, 192, 1)', borderWidth: 1 }, { label: '停機', data: [5], // 機器A停機5小時 backgroundColor: 'rgba(255, 99, 132, 0.8)', // 停機狀態(tài)的顏色 borderColor: 'rgba(255, 99, 132, 1)', borderWidth: 1 }, { label: '維護中', data: [3], // 機器A維護3小時 backgroundColor: 'rgba(255, 205, 86, 0.8)', // 維護狀態(tài)的顏色 borderColor: 'rgba(255, 205, 86, 1)', borderWidth: 1 }, { label: '空閑', data: [6], // 機器A空閑6小時 backgroundColor: 'rgba(201, 203, 207, 0.8)', // 空閑狀態(tài)的顏色 borderColor: 'rgba(201, 203, 207, 1)', borderWidth: 1 } ] }; const chartOptions = { responsive: true, maintainAspectRatio: false, indexAxis: 'y', // 使條形圖水平顯示 plugins: { title: { display: true, text: '機器A日狀態(tài)堆疊進度條', font: { size: 18 } }, tooltip: { mode: 'index', intersect: false }, legend: { position: 'bottom', // 將圖例放在底部 } }, scales: { x: { stacked: true, // X軸堆疊 title: { display: true, text: '時間 (小時)' }, beginAtZero: true, max: 24 // 假設(shè)總時長為24小時 }, y: { stacked: true, // Y軸堆疊 title: { display: true, text: '機器' }, grid: { display: false // 隱藏Y軸網(wǎng)格線 } } } }; new Chart(ctx, { type: 'bar', data: chartData, options: chartOptions }); }); </script> </body> </html>
// 示例:多條堆疊進度條 labels: ['機器A', '機器B', '機器C'], datasets: [ { label: '運行中', data: [10, 8, 12], backgroundColor: 'green' }, { label: '停機', data: [5, 7, 3], backgroundColor: 'red' }, // ... 其他數(shù)據(jù)集 ]
通過本教程,您已經(jīng)掌握了如何使用Chart.js庫在HTML頁面中創(chuàng)建具有堆疊效果的進度條圖表。關(guān)鍵在于正確配置data中的datasets來表示堆疊的各個部分,以及在options.scales中將stacked屬性設(shè)置為true。這種圖表形式在展示比例、構(gòu)成和狀態(tài)變化方面具有極高的視覺效率,是數(shù)據(jù)可視化工具箱中不可或缺的一部分。結(jié)合Chart.js強大的定制能力,您可以根據(jù)具體需求創(chuàng)建出滿足各種業(yè)務(wù)場景的專業(yè)級圖表。
以上就是使用JavaScript和Chart.js創(chuàng)建堆疊進度條圖表教程的詳細內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號