国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript實現的DOM繪制柱狀圖效果示例

2019-11-19 13:19:31
字體:
來源:轉載
供稿:網友

本文實例講述了JavaScript實現的DOM繪制柱狀圖效果。分享給大家供大家參考,具體如下:

<!DOCTYPE html><html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title>www.survivalescaperooms.com 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相關內容感興趣的讀者可查看本站專題:《JavaScript操作DOM技巧總結》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結

希望本文所述對大家JavaScript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武汉市| 项城市| 鸡东县| 江阴市| 云南省| 盖州市| 伊春市| 宝兴县| 阜宁县| 荃湾区| 淄博市| 和田县| 连城县| 大厂| 射阳县| 德兴市| 黔西县| 平遥县| 苏尼特右旗| 乌什县| 西和县| 黄龙县| 桂平市| 铜梁县| 平度市| 合肥市| 民权县| 忻州市| 东丽区| 左云县| 治多县| 丘北县| 东平县| 金乡县| 黑水县| 达日县| 长丰县| 应用必备| 晋州市| 汉川市| 新巴尔虎右旗|