萬盛學電腦網

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

網頁圖表構建基礎 CSS 代碼庫:橫向條形、分組條形、垂直柱狀三大核心模式完整實現

構建輕量級數據視覺化的三大核心 CSS 模式,代碼可直接複製適配項目,零依賴、語意化、易擴展。

模式一:基礎橫向條形圖

適用:單指標、進度條、技能熟練度

<style>
.graph {
  position: relative;
  width: 200px;
  border: 1px solid #b1d632;
  padding: 2px;
}
.graph .bar {
  display: block;
  position: relative;
  background: #b1d632;
  text-align: center;
  color: #333;
  height: 2em;
  line-height: 2em;
}
.graph .bar span { position: absolute; left: 1em; }
</style>

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

模式二:分組橫向條形圖(DL 結構 + 雙層背景圖)

適用:多指標對比、儀表盤卡片、問卷結果

<style>
.chart { margin: 0; padding: 0; }
.chart dt {
  position: relative; clear: both; float: left;
  width: 104px; height: 20px; line-height: 20px;
  margin-right: 17px; font-size: .75em; text-align: right;
}
.chart dd {
  position: relative; float: left;
  width: 197px; height: 20px; margin: 0 0 15px;
  background: url(g_colorbar.jpg);  /* 軌道背景 */
}
* html .chart dd { float: none; }  /* IE6 hack */
.chart dd div {
  position: relative; height: 20px; width: 75%;
  background: url(g_colorbar2.jpg);  /* 進度填充 */
  text-align: right;
}
.chart dd div strong {
  position: absolute; right: -5px; top: -2px;
  display: block; width: 9px; height: 24px;
  background: url(g_marker.gif);  /* 端點標記 */
  text-indent: -9999px; overflow: hidden;
}
</style>

<dl class="chart">
  <dt>Love Life</dt>
  <dd><div style="width:25%;"><strong>25%</strong></div></dd>
  <dt>Education</dt>
  <dd><div style="width:55%;"><strong>55%</strong></div></dd>
  <dt>War Craft 3 Rank</dt>
  <dd><div style="width:75%;"><strong>75%</strong></div></dd>
</dl>

模式三:垂直柱狀圖(絕對定位底部對齊)

適用:統計分佈、錯誤等級分佈、流量時段對比

<style>
#vertgraph {
  width: 378px; height: 207px; position: relative;
  background: url(g_backbar.gif) no-repeat;  /* 底部座標軸 */
}
#vertgraph ul { margin:0; padding:0; width:100%; height:100%; }
#vertgraph li {
  position: absolute; bottom: 34px; width: 28px;
  background: url(g_colorbar3.jpg) no-repeat;  /* 柱狀漸變 */
  text-align: center; font-weight: bold; color: white; line-height: 2.5em;
}
#vertgraph li.critical { left: 24px; background-position: 0; }
#vertgraph li.high { left: 101px; background-position: -28px; }
#vertgraph li.medium { left: 176px; background-position: -56px; }
#vertgraph li.low { left: 251px; background-position: -84px; }
#vertgraph li.info { left: 327px; background-position: -112px; }
</style>

<div id="vertgraph">
  <ul>
    <li class="critical" style="height: 150px;">22</li>
    <li class="high" style="height: 80px;">7</li>
    <li class="medium" style="height: 50px;">3</li>
    <li class="low" style="height: 90px;">8</li>
    <li class="info" style="height: 40px;">2</li>
  </ul>
</div>

現代化重構建議

  1. 移除圖片依賴:用 `linear-gradient` 替代 `g_colorbar*.jpg`,`clip-path` 替代 `g_marker.gif`
  2. CSS 變量驅動:`--bar-height: 75%; --bar-color: #09f;`
  3. 語意化標記:`
  4. 動畫入場:`@keyframes grow { from { height: 0; } }` / `width: 0 → var(--value)`
  5. 響應式**:容器 `width: 100%; max-width: 400px;`,柱寬 `clamp(20px, 5vw, 40px)`

📦 這三個模式覆蓋 80%+ 輕量圖表需求,建議封裝為組件庫(React/Vue/Svelte)或 Web Components 復用。

copyright © 萬盛學電腦網 all rights reserved