Protovis.Bar.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Protovis.Bar = new Class({
  2. Extends : Protovis,
  3. options : {
  4. },
  5. initialize : function(el,options){
  6. this.parent(el,options);
  7. this.prepData();
  8. this.setScale();
  9. this.makeGraph();
  10. this.render();
  11. },
  12. prepData : function(){
  13. this.data = []; var d = this.options.data, x = [], y = [];
  14. var len = d[0].length - 1;
  15. for(var i = 0; i < d.length; i++){
  16. this.data.push( parseFloat(d[i][0]) );
  17. x.push(parseFloat(d[i][0]));
  18. }
  19. this.limits = { xmin : pv.min(x), xmax : pv.max(x), ymin : 0, ymax: x.length};
  20. },
  21. setScale : function(){
  22. this.parent();
  23. this.x = pv.Scale.linear(this.limits.xmin,Math.ceil(this.limits.xmax)).range(0,this.width);
  24. this.y = pv.Scale.ordinal(pv.range(this.limits.ymax)).splitBanded(0,this.height, 4/5);
  25. },
  26. makeGraph : function(){
  27. this.parent();
  28. this.addYTicks();
  29. this.addXTicks();
  30. this.makePanel();
  31. this.plot();
  32. },
  33. addYTicks : function(){
  34. },
  35. addXTicks : function(){
  36. this.xTicks = this.graph.add(pv.Rule)
  37. .data(this.x.ticks())
  38. .left(function(d){ return Math.round(this.x(d)) - .5; }.bind(this))
  39. .strokeStyle(function(d){ return d ? '#ddd' : '#000'; })
  40. .add(pv.Rule)
  41. .bottom(0)
  42. .height(5)
  43. .strokeStyle("#000")
  44. .anchor("bottom").add(pv.Label)
  45. },
  46. plot : function(){
  47. this.bar = this.graph.add(pv.Bar)
  48. .data(this.data)
  49. .height(this.y.range().band)
  50. .top(function(scope){ return this.y(scope.index); }.bind(this,this.bar))
  51. .left(0)
  52. .width(this.x);
  53. this.bar.anchor("right").add(pv.Label)
  54. .textStyle("white")
  55. }
  56. });