Skip to content

Instantly share code, notes, and snippets.

@fernandoc1
Created July 13, 2016 17:08
Show Gist options
  • Select an option

  • Save fernandoc1/e417ae763ac01c9b43aef4e3df0bef72 to your computer and use it in GitHub Desktop.

Select an option

Save fernandoc1/e417ae763ac01c9b43aef4e3df0bef72 to your computer and use it in GitHub Desktop.
A simple bar chart code for the web.
<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