Wednesday, April 2, 2008

HTA-Python : BarGraph


<html>
<head>

<style type='text/css'>
body
{
   color : #fff;
}


</style>
<script language="Python">

def getDiv(div):
    return document.getElementById(div)

def graphBar(data=[],largeur=10,hauteur=400,bgcolor="#00FFFF"):
    table = "<table><tr>"
    maxdata = max(data,key=lambda liste : liste[1])[1]
    coeffmul = 1.0*hauteur/maxdata
    for item in data:
        haut = item[1]*coeffmul
        stl = """style=background-color:%s;padding:4px;font-weight:bold;height:%spx;"""%(bgcolor,haut)
        col = """<td valign='bottom' width='%s'><div %s>%s</div></td>"""%(largeur,stl,item[0])
        table += col
    table += "</tr></table>"
    return table

def test():
    graphdiv = getDiv('graph')
    data = [('11',32),('12',16),
            ('13',8),('14',78),
            ('21',15),('22',45),
            ('23',65),('24',34),
            ('31',55),('32',34),
            ('33',67),('34',45),
            ('41',15),('42',45),
            ('43',65),('44',34)]

    g = graphBar(data,bgcolor='#FF5700',largeur=0,hauteur=200)

    graphdiv.innerHTML = "<div style='border:2px solid #797979;width:1px;'>%s</div>"%g

def init():
    test()
</script>
</head>

<body onload='init()'>
<center>

<div id='graph'></div>
</center>

</body>
</html>