Created
July 13, 2016 17:08
-
-
Save fernandoc1/e417ae763ac01c9b43aef4e3df0bef72 to your computer and use it in GitHub Desktop.
A simple bar chart code for the web.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <body> | |
| <style> | |
| #chartDiv | |
| { | |
| width: 50%; | |
| height: 200px; | |
| } | |
| .chartBar | |
| { | |
| height: 20px; | |
| margin: 4px; | |
| } | |
| @keyframes fillBar{ | |
| 0% {width: 0%;} | |
| 100% {width: 100%;} | |
| } | |
| .chartBarFill | |
| { | |
| height: 100%; | |
| background-color: red; | |
| opacity: 0.7; | |
| border: 1px solid black; | |
| animation-name: fillBar; | |
| animation-duration: 2s; | |
| } | |
| </style> | |
| <div id="chartDiv"> | |
| <div class="chartBar" style="width:50%;"><div class="chartBarFill"></div></div> | |
| <div class="chartBar" style="width:5%;"><div class="chartBarFill"></div></div> | |
| <div class="chartBar" style="width:25%;"><div class="chartBarFill"></div></div> | |
| </div> | |
| </body> | |
| </html> | |
| <script> | |
| function createChartBar(percent, color) | |
| { | |
| var div = document.createElement("div"); | |
| div.className = "chartBar"; | |
| div.style.width = Math.ceil(percent)+"%"; | |
| //console.log(div.style.width); | |
| var fillDiv = document.createElement("div"); | |
| fillDiv.className = "chartBarFill"; | |
| fillDiv.style.backgroundColor = color; | |
| fillDiv.style.textAlign = "center"; | |
| fillDiv.innerHTML = div.style.width; | |
| div.appendChild(fillDiv); | |
| return div; | |
| } | |
| function BarChart(chartDiv) | |
| { | |
| this.chartDiv = chartDiv; | |
| this.table = document.createElement("table"); | |
| this.chartDiv.appendChild(this.table); | |
| this.addBar = function(bar) | |
| { | |
| this.chartDiv.insertBefore(bar, this.chartDiv.firstChild); | |
| } | |
| } | |
| var chart = new BarChart(document.getElementById("chartDiv")); | |
| for (i = 0; i < 10; i++) | |
| { | |
| var percentValue = Math.random() * 100; | |
| var chartBar = createChartBar(percentValue, "yellow"); | |
| chart.addBar(chartBar); | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment