您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了使用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 > ' + 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 > ' + 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绘制柱状图效果的案例”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。