萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 實作長條圖表:純 CSS 打造資料視覺化(進階版)

核心概念

利用容器固定寬度作為 100% 基準,內部區塊以百分比寬度呈現數值比例,配合邊框與背景色形成長條圖。本文為進階版,補充動畫、多組資料、垂直圖表與無障礙增強。

基礎 HTML 結構

<div class="graph">
  <strong class="bar" style="width: 24%;">24%</strong>
</div>

基礎 CSS 樣式

.graph {
  position: relative;
  width: 200px;
  border: 1px solid #B1D632;
  padding: 2px;
  margin-bottom: 1rem;
}

.graph .bar {
  display: block;
  position: relative;
  background: #B1D632;
  text-align: center;
  color: #333;
  height: 2em;
  line-height: 2em;
  transition: width 0.6s ease-out; /* 載入動畫 */
}

.graph .bar span { position: absolute; left: 1em; }

進階一:多組資料橫向長條圖

<div class="graph-group">
  <div class="graph"><strong class="bar" style="width: 24%;">24%</strong></div>
  <div class="graph"><strong class="bar" style="width: 60%;">60%</strong></div>
  <div class="graph"><strong class="bar" style="width: 85%;">85%</strong></div>
</div>

.graph-group .graph { margin-bottom: 0.5rem; }

進階二:垂直長條圖

<div class="graph-vertical" style="height: 200px;">
  <div class="bar-vertical" style="height: 60%;">60%</div>
</div>

.graph-vertical {
  position: relative;
  width: 40px;
  height: 200px;
  border: 1px solid #B1D632;
  border-top: none;
  display: inline-block;
  vertical-align: bottom;
  margin: 0 10px;
}
.bar-vertical {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background: #B1D632;
  text-align: center;
  color: #333;
  line-height: 2em;
  transition: height 0.6s ease-out;
}

進階三:無障礙增強

<div class="graph" role="img" aria-label="完成度 60%">
  <div class="bar" style="width: 60%;" aria-hidden="true"></div>
  <span class="sr-only">完成度 60%</span>
</div>

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

完整單頁範例

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>CSS Bar Chart 進階範例</title>
<style>
/* 基礎樣式 */
.graph { position: relative; width: 300px; border: 1px solid #B1D632; padding: 2px; margin-bottom: 1rem; }
.graph .bar { display: block; position: relative; background: #B1D632; text-align: center; color: #fff; height: 2em; line-height: 2em; transition: width 0.6s ease-out; }
.graph .bar::before { content: attr(data-label); position: absolute; left: 0.5em; } /* 使用 data 屬性放標籤 */

/* 垂直圖 */
.graph-v { display: inline-flex; align-items: flex-end; height: 200px; width: 50px; border: 1px solid #B1D632; border-top: none; margin: 0 10px; vertical-align: bottom; }
.bar-v { width: 100%; background: #B1D632; color: #fff; text-align: center; line-height: 2em; transition: height 0.6s ease-out; }

/* 無障礙隱藏文字 */
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
</style>
</head>
<body>
  <h3>橫向長條圖</h3>
  <div class="graph" role="img" aria-label="專案進度 24%">
    <div class="bar" style="width: 24%;" data-label="24%" aria-hidden="true"></div>
  </div>
  <div class="graph" role="img" aria-label="專案進度 60%">
    <div class="bar" style="width: 60%;" data-label="60%" aria-hidden="true"></div>
  </div>

  <h3>垂直長條圖群組</h3>
  <div style="display: flex; align-items: flex-end; height: 220px;">
    <div class="graph-v" role="img" aria-label="Q1 銷售 40%">
      <div class="bar-v" style="height: 40%;" aria-hidden="true"></div>
      <span class="sr-only">Q1 40%</span>
    </div>
    <div class="graph-v" role="img" aria-label="Q2 銷售 75%">
      <div class="bar-v" style="height: 75%;" aria-hidden="true"></div>
      <span class="sr-only">Q2 75%</span>
    </div>
    <div class="graph-v" role="img" aria-label="Q3 銷售 90%">
      <div class="bar-v" style="height: 90%;" aria-hidden="true"></div>
      <span class="sr-only">Q3 90%</span>
    </div>
  </div>
</body>
</html>

總結

純 CSS 長條圖輕量、無依賴、易維護,適合簡單資料視覺化。複雜圖表(堆疊、分組、互動、Tooltip)仍建議使用 Chart.js、ECharts、D3.js 等專業庫。

copyright © 萬盛學電腦網 all rights reserved