Bars.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. define("dojox/charting/plot3d/Bars", ["dojox/gfx3d", "dojo/_base/window", "dojo/_base/declare", "dojo/_base/Color", "./Base"],
  2. function(gfx3d, win, declare, Color, Base) {
  3. // reduce function borrowed from dojox.fun
  4. var reduce = function(/*Array*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  5. // summary: repeatedly applies a binary function to an array from left
  6. // to right; returns the final value.
  7. a = typeof a == "string" ? a.split("") : a; o = o || win.global;
  8. var z = a[0];
  9. for(var i = 1; i < a.length; z = f.call(o, z, a[i++]));
  10. return z; // Object
  11. };
  12. /*=====
  13. var Base = dojox.charting.plot3d.Base;
  14. =====*/
  15. return declare("dojox.charting.plot3d.Bars", Base, {
  16. constructor: function(width, height, kwArgs){
  17. this.depth = "auto";
  18. this.gap = 0;
  19. this.data = [];
  20. this.material = {type: "plastic", finish: "dull", color: "lime"};
  21. if(kwArgs){
  22. if("depth" in kwArgs){ this.depth = kwArgs.depth; }
  23. if("gap" in kwArgs){ this.gap = kwArgs.gap; }
  24. if("material" in kwArgs){
  25. var m = kwArgs.material;
  26. if(typeof m == "string" || m instanceof Color){
  27. this.material.color = m;
  28. }else{
  29. this.material = m;
  30. }
  31. }
  32. }
  33. },
  34. getDepth: function(){
  35. if(this.depth == "auto"){
  36. var w = this.width;
  37. if(this.data && this.data.length){
  38. w = w / this.data.length;
  39. }
  40. return w - 2 * this.gap;
  41. }
  42. return this.depth;
  43. },
  44. generate: function(chart, creator){
  45. if(!this.data){ return this; }
  46. var step = this.width / this.data.length, org = 0,
  47. depth = this.depth == "auto" ? step - 2 * this.gap : this.depth,
  48. scale = this.height / reduce(this.data, Math.max);
  49. if(!creator){ creator = chart.view; }
  50. for(var i = 0; i < this.data.length; ++i, org += step){
  51. creator
  52. .createCube({
  53. bottom: {x: org + this.gap, y: 0, z: 0},
  54. top: {x: org + step - this.gap, y: this.data[i] * scale, z: depth}
  55. })
  56. .setFill(this.material);
  57. }
  58. }
  59. });
  60. });