JavaScript 纯 DOM 实现柱状图绘制示例

作者:竹影

时间:2026-10-22

浏览:531

本文提供了一段使用原生 JavaScript 通过 DOM 操作绘制柱状图的完整示例。代码包含 HTML 结构、CSS 样式及 JS 逻辑,通过动态创建元素和设置样式实现数据可视化,适合学习前端基础图表实现原理。

文章目录

JavaScript 纯 DOM 实现柱状图绘制示例

本文介绍了一个使用 JavaScript 通过 DOM 操作来绘制柱状图的完整示例,供开发者参考学习。


<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>www.jb51.net JavaScript DOM绘制柱状图</title>
    <style>
      #chart-wrap{ 
        width:910px; 
        height:240px;
        border:solid 1px #B3B3DC;
        position:relative; 
        top:40px; 
        left:20px;
      } 
    </style>
  </head>
  <body>
    <div id="chart-wrap"></div>
    <script>
      function renderChart(data) {
        var cw = document.getElementById("chart-wrap"); 
        cw.innerHTML = ""; 
        var max = 0; 
        for (var index in data) {
          if (data[index] > max)
            max = data[index];
        }
        var percent = 180 / max; 
        var i = 0;
        for (var index in data) { 
          var bar = document.createElement("div"); 
          bar.id = index + "_" + data[index]; 
          bar.style.height = Math.round(percent * data[index]) + "px"; 
          bar.style.width = "40px";
          bar.style.left = (i * 40) + 165 + "px"; 
          bar.style.marginLeft = (i * 20) + "px"; 
          bar.style.position = "absolute"; 
          bar.style.background = "none repeat scroll 0 0 pink";
          bar.style.overflow = "hidden";
          bar.setAttribute("title", index + ":" + data[index]);
          bar.style.display = "block"; 
          bar.style.top = 200 - Math.round(percent * data[index]) + "px"; 
          cw.appendChild(bar); 
          var axis = document.createElement("div"); 
          axis.id = "axis_" + i; 
          axis.style.width = "40px"; 
          axis.style.left = (i * 40) + 165 + "px"; 
          axis.style.marginLeft = (i * 20) + "px";
          axis.style.textAlign = "center"; 
          axis.style.position = "absolute"; 
          axis.style.top = "205px"; 
          axis.innerHTML = '<span style="font-size:12px; color:grey;"> ' + i + '</span>'; 
          cw.appendChild(axis); 
          i++;
        }   
        for (var i = 0; i < 5; i++) { 
          var ayis = document.createElement("div"); 
          ayis.style.width = "30px"; 
          ayis.style.position = "absolute"; 
          ayis.style.top = (36 * i) + (20 - 6) + "px"; 
          ayis.style.left = "140px"; 
          ayis.innerHTML = '<span style="font-size:12px; color:grey;"> ' + Math.round(max - (max / 5) * i) + '</span>'; 
          cw.appendChild(ayis); 
          var line = document.createElement("div"); 
          line.setAttribute("style", "width:580px; left:165px; border-top:1px dotted grey; height:1px; line-height:1px; display:block; overflow:hidden; position:absolute; "); 
          line.style.top = (36 * i) + 20 + "px"; 
          cw.appendChild(line); 
        } 
      }
      var data = [10,60,50,30,40,80,20,70,100,90];
      renderChart(data);
    </script>
  </body>
</html>

该示例的运行效果如下图所示:

JavaScript 纯 DOM 实现柱状图绘制示例

如需了解更多 JavaScript 相关知识,可参考本站的《JavaScript操作DOM技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》以及《JavaScript数学运算用法总结》等专题文章。

希望这段关于 JavaScript 程序设计的代码示例能对读者有所帮助。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章