Showing posts with label bar chart. Show all posts
Showing posts with label bar chart. Show all posts

Monday, May 16, 2011

Another Bar Chart (Part 1)

<script type="text/javascript+protovis">
var data1 = [1, 1.2, 1.7, 1.5, .7];
var data2 = [1.2, 1.1, 1.5, 1.7, .9];
var width = 25;
var barspace = 35;

var labeldata = ["a", "b", "c", "d", "e"];

var panel = new pv.Panel()
        .width(500)
        .height(150);
        
var bar = panel.add(pv.Bar)
        .data(data1)
        .bottom(0)
        .width(width)
        .height(function(d) d * 80)
        .left(function() this.index * (width + barspace));
var bar2 = panel.add(pv.Bar)
        .data(data2)
        .bottom(0)
        .width(width)
        .fillStyle("red")
        .height(function(d) d * 80)
        .left(function() this.index * (width + barspace) + width);
        
        
bar.add(pv.Label)
    .data(labeldata)
    .top(function() bar.top())            
    .left(function() bar.left()+3);
bar.add(pv.Label)
    .textStyle("white");
    
bar2.add(pv.Label)
    .data(labeldata)
    .top(function() bar2.top())            
    .left(function() bar2.left()+3);
    
bar2.add(pv.Label)
    .textStyle("white");
            
panel.render();
</script>


Simple Sorted Bar Chart

Alternative 1

<script type="text/javascript+protovis">
var mydata = [1, 1.2, 1.7, 1.5, .7];
mydata.sort();
mydata.reverse();
new pv.Panel()
        .width(150)
        .height(150)
        .add(pv.Bar)
        .data(mydata)
        .bottom(0)
        .width(20)
        .height(function(d) d * 80)
        .left(function() this.index * 25)
        .anchor("top")
        .add(pv.Label)
        .root.render();
</script>




Alternative 2

<script type="text/javascript+protovis">
var mydata = [1, 1.2, 1.7, 1.5, .7];
mydata.sort();
mydata.reverse();
var panel = new pv.Panel()
        .width(150)
        .height(150);
        
var bar = panel.add(pv.Bar)
        .data(mydata)
        .bottom(0)
        .width(20)
        .height(function(d) d * 80)
        .left(function() this.index * 25);
        
var label = bar.add(pv.Label)
            .top(function() bar.top())
            .root.render();
</script>




Alternative 3

<script type="text/javascript+protovis">
var mydata = [1, 1.2, 1.7, 1.5, .7];
var labeldata = ["a", "b", "c", "d", "e"];

mydata.sort();
mydata.reverse();
var panel = new pv.Panel()
        .width(150)
        .height(150);
        
var bar = panel.add(pv.Bar)
        .data(mydata)
        .bottom(0)
        .width(20)
        .height(function(d) d * 80)
        .left(function() this.index * 25);
        
var label = bar.add(pv.Label)
            .data(labeldata)
            .top(function() bar.top())            
            .left(function() bar.left()+3)

var label = bar.add(pv.Label)
            .textStyle("white");
            
panel.render();
</script>



Related Post :