Chart3D.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.Chart3D"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.Chart3D"] = true;
  8. dojo.provide("dojox.charting.Chart3D");
  9. dojo.require("dojox.gfx3d");
  10. (function(){
  11. var observerVector = {x: 0, y: 0, z: 1}, v = dojox.gfx3d.vector, n = dojox.gfx.normalizedLength;
  12. dojo.declare("dojox.charting.Chart3D", null, {
  13. constructor: function(node, lights, camera, theme){
  14. // setup a view
  15. this.node = dojo.byId(node);
  16. this.surface = dojox.gfx.createSurface(this.node, n(this.node.style.width), n(this.node.style.height));
  17. this.view = this.surface.createViewport();
  18. this.view.setLights(lights.lights, lights.ambient, lights.specular);
  19. this.view.setCameraTransform(camera);
  20. this.theme = theme;
  21. // initialize internal variables
  22. this.walls = [];
  23. this.plots = [];
  24. },
  25. // public API
  26. generate: function(){
  27. return this._generateWalls()._generatePlots();
  28. },
  29. invalidate: function(){
  30. this.view.invalidate();
  31. return this;
  32. },
  33. render: function(){
  34. this.view.render();
  35. return this;
  36. },
  37. addPlot: function(plot){
  38. return this._add(this.plots, plot);
  39. },
  40. removePlot: function(plot){
  41. return this._remove(this.plots, plot);
  42. },
  43. addWall: function(wall){
  44. return this._add(this.walls, wall);
  45. },
  46. removeWall: function(wall){
  47. return this._remove(this.walls, wall);
  48. },
  49. // internal API
  50. _add: function(array, item){
  51. if(!dojo.some(array, function(i){ return i == item; })){
  52. array.push(item);
  53. this.view.invalidate();
  54. }
  55. return this;
  56. },
  57. _remove: function(array, item){
  58. var a = dojo.filter(array, function(i){ return i != item; });
  59. return a.length < array.length ? (array = a, this.invalidate()) : this;
  60. },
  61. _generateWalls: function(){
  62. for(var i = 0; i < this.walls.length; ++i){
  63. if(v.dotProduct(observerVector, this.walls[i].normal) > 0){
  64. this.walls[i].generate(this);
  65. }
  66. }
  67. return this;
  68. },
  69. _generatePlots: function(){
  70. var depth = 0, m = dojox.gfx3d.matrix, i = 0;
  71. for(; i < this.plots.length; ++i){
  72. depth += this.plots[i].getDepth();
  73. }
  74. for(--i; i >= 0; --i){
  75. var scene = this.view.createScene();
  76. scene.setTransform(m.translate(0, 0, -depth));
  77. this.plots[i].generate(this, scene);
  78. depth -= this.plots[i].getDepth();
  79. }
  80. return this;
  81. }
  82. });
  83. })();
  84. }