Cylinders.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. define("dojox/charting/plot3d/Cylinders", ["dojox/gfx3d", "dojox/gfx3d/matrix", "dojo/_base/declare", "dojo/_base/Color", "dojo/_base/window", "./Base"],
  2. function(gfx3d, matrix3d, declare, Color, win, 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.Cylinders", Base, {
  16. constructor: function(width, height, kwArgs){
  17. this.depth = "auto";
  18. this.gap = 0;
  19. this.data = [];
  20. this.material = {type: "plastic", finish: "shiny", color: "lime"};
  21. this.outline = null;
  22. if(kwArgs){
  23. if("depth" in kwArgs){ this.depth = kwArgs.depth; }
  24. if("gap" in kwArgs){ this.gap = kwArgs.gap; }
  25. if("material" in kwArgs){
  26. var m = kwArgs.material;
  27. if(typeof m == "string" || m instanceof Color){
  28. this.material.color = m;
  29. }else{
  30. this.material = m;
  31. }
  32. }
  33. if("outline" in kwArgs){ this.outline = kwArgs.outline; }
  34. }
  35. },
  36. getDepth: function(){
  37. if(this.depth == "auto"){
  38. var w = this.width;
  39. if(this.data && this.data.length){
  40. w = w / this.data.length;
  41. }
  42. return w - 2 * this.gap;
  43. }
  44. return this.depth;
  45. },
  46. generate: function(chart, creator){
  47. if(!this.data){ return this; }
  48. var step = this.width / this.data.length, org = 0,
  49. scale = this.height / reduce(this.data, Math.max);
  50. if(!creator){ creator = chart.view; }
  51. for(var i = 0; i < this.data.length; ++i, org += step){
  52. creator
  53. .createCylinder({
  54. center: {x: org + step / 2, y: 0, z: 0},
  55. radius: step / 2 - this.gap,
  56. height: this.data[i] * scale
  57. })
  58. .setTransform(matrix3d.rotateXg(-90))
  59. .setFill(this.material).setStroke(this.outline);
  60. }
  61. }
  62. });
  63. });