Bars.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.charting.plot3d.Bars"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.plot3d.Bars"] = true;
  8. dojo.provide("dojox.charting.plot3d.Bars");
  9. dojo.require("dojox.charting.plot3d.Base");
  10. (function(){
  11. // reduce function borrowed from dojox.fun
  12. var reduce = function(/*Array*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  13. // summary: repeatedly applies a binary function to an array from left
  14. // to right; returns the final value.
  15. a = typeof a == "string" ? a.split("") : a; o = o || dojo.global;
  16. var z = a[0];
  17. for(var i = 1; i < a.length; z = f.call(o, z, a[i++]));
  18. return z; // Object
  19. };
  20. dojo.declare("dojox.charting.plot3d.Bars", dojox.charting.plot3d.Base, {
  21. constructor: function(width, height, kwArgs){
  22. this.depth = "auto";
  23. this.gap = 0;
  24. this.data = [];
  25. this.material = {type: "plastic", finish: "dull", color: "lime"};
  26. if(kwArgs){
  27. if("depth" in kwArgs){ this.depth = kwArgs.depth; }
  28. if("gap" in kwArgs){ this.gap = kwArgs.gap; }
  29. if("material" in kwArgs){
  30. var m = kwArgs.material;
  31. if(typeof m == "string" || m instanceof dojo.Color){
  32. this.material.color = m;
  33. }else{
  34. this.material = m;
  35. }
  36. }
  37. }
  38. },
  39. getDepth: function(){
  40. if(this.depth == "auto"){
  41. var w = this.width;
  42. if(this.data && this.data.length){
  43. w = w / this.data.length;
  44. }
  45. return w - 2 * this.gap;
  46. }
  47. return this.depth;
  48. },
  49. generate: function(chart, creator){
  50. if(!this.data){ return this; }
  51. var step = this.width / this.data.length, org = 0,
  52. depth = this.depth == "auto" ? step - 2 * this.gap : this.depth,
  53. scale = this.height / reduce(this.data, Math.max);
  54. if(!creator){ creator = chart.view; }
  55. for(var i = 0; i < this.data.length; ++i, org += step){
  56. creator
  57. .createCube({
  58. bottom: {x: org + this.gap, y: 0, z: 0},
  59. top: {x: org + step - this.gap, y: this.data[i] * scale, z: depth}
  60. })
  61. .setFill(this.material);
  62. }
  63. }
  64. });
  65. })();
  66. }