JavaScript实现的DOM绘制柱状图效果示例
程序员文章站
2022-04-04 14:54:50
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《javascript操作dom技巧总结》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》及《javascript数学运算用法总结》
希望本文所述对大家javascript程序设计有所帮助。
下一篇: 24头牛呀
推荐阅读
-
js实现点小图看大图效果的思路及示例代码_javascript技巧
-
jQuery插件HighCharts绘制的2D堆柱状图效果示例【附demo源码下载】
-
jQuery插件FusionCharts实现的2D柱状图效果示例【附demo源码下载】
-
jQuery插件echarts实现的多柱子柱状图效果示例【附demo源码下载】
-
jQuery插件FusionCharts绘制的2D双柱状图效果示例【附demo源码】
-
jQuery插件HighCharts实现2D柱状图、折线图的组合多轴图效果示例【附demo源码下载】
-
jQuery插件HighCharts绘制2D柱状图、折线图和饼图的组合图效果示例【附demo源码下载】
-
JavaScript实现简单的文本逐字打印效果示例
-
JavaScript通过mouseover()实现图片变大效果的示例
-
jQuery插件FusionCharts绘制的3D双柱状图效果示例【附demo源码】