EventSource->Produzione chart

by giacal

HTML

<section>
<div></div>
<div></div>
<div></div>
<div></div>
</section>

CSS

section{
    position:relative;
    height: 100px;
    width: 100%;
    background: gray;
}

div{
    position: absolute;
    background: red;       
    width: 50px;
    bottom: 0px;
    -webkit-transition: height 1s;
}

div:nth-child(1){
    left: 10px;
}

div:nth-child(2){
    left: 70px;
}

div:nth-child(3){
    left: 130px;
}

div:nth-child(4){
    left: 190px;
}

JavaScript

var stocks = new EventSource("http://sandropaganotti.com/wp-content/goodies/samples/tick.php");

stocks.addEventListener('message', function (event) {
    var graph_data = JSON.parse(event.data),
        d1 = document.querySelector('div:nth-child(1)'),
        d2 = document.querySelector('div:nth-child(2)'),
        d3 = document.querySelector('div:nth-child(3)'),
        d4 = document.querySelector('div:nth-child(4)');        
    
    d1.style.height = "" + (graph_data.data1 * 10) + "px";
    d2.style.height = "" + (graph_data.data2 * 10) + "px";
    d3.style.height = "" + (graph_data.data3 * 10) + "px";
    d4.style.height = "" + (graph_data.data4 * 10) + "px";
});