Chart3D.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. define("dojox/charting/Chart3D", ["dojo/_base/array", "dojo/dom","dojo/_base/declare", "dojo/_base/html", "dojox/gfx", "dojox/gfx3d"],
  2. function(arr, dom, declare, html, gfx, gfx3d){
  3. // module:
  4. // dojox/charting/Chart3D
  5. // summary:
  6. // This module provides basic 3d charting capablities (using 2d vector graphics to simulate 3d.
  7. /*=====
  8. dojox.charting.__Chart3DCtorArgs = function(node, lights, camera, theme){
  9. // summary:
  10. // The keyword arguments that can be passed in a Chart constructor.
  11. //
  12. // node: Node
  13. // The DOM node to construct the chart on.
  14. // lights:
  15. // Lighting properties for the 3d scene
  16. // camera: Object
  17. // Camera properties describing the viewing camera position.
  18. // theme: Object
  19. // Charting theme to use for coloring chart elements.
  20. }
  21. =====*/
  22. var observerVector = {x: 0, y: 0, z: 1}, v = gfx3d.vector, n = gfx.normalizedLength;
  23. return declare("dojox.charting.Chart3D", null, {
  24. constructor: function(node, lights, camera, theme){
  25. // setup a view
  26. this.node = dom.byId(node);
  27. this.surface = gfx.createSurface(this.node, n(this.node.style.width), n(this.node.style.height));
  28. this.view = this.surface.createViewport();
  29. this.view.setLights(lights.lights, lights.ambient, lights.specular);
  30. this.view.setCameraTransform(camera);
  31. this.theme = theme;
  32. // initialize internal variables
  33. this.walls = [];
  34. this.plots = [];
  35. },
  36. // public API
  37. generate: function(){
  38. return this._generateWalls()._generatePlots();
  39. },
  40. invalidate: function(){
  41. this.view.invalidate();
  42. return this;
  43. },
  44. render: function(){
  45. this.view.render();
  46. return this;
  47. },
  48. addPlot: function(plot){
  49. return this._add(this.plots, plot);
  50. },
  51. removePlot: function(plot){
  52. return this._remove(this.plots, plot);
  53. },
  54. addWall: function(wall){
  55. return this._add(this.walls, wall);
  56. },
  57. removeWall: function(wall){
  58. return this._remove(this.walls, wall);
  59. },
  60. // internal API
  61. _add: function(array, item){
  62. if(!arr.some(array, function(i){ return i == item; })){
  63. array.push(item);
  64. this.view.invalidate();
  65. }
  66. return this;
  67. },
  68. _remove: function(array, item){
  69. var a = arr.filter(array, function(i){ return i != item; });
  70. return a.length < array.length ? (array = a, this.invalidate()) : this;
  71. },
  72. _generateWalls: function(){
  73. for(var i = 0; i < this.walls.length; ++i){
  74. if(v.dotProduct(observerVector, this.walls[i].normal) > 0){
  75. this.walls[i].generate(this);
  76. }
  77. }
  78. return this;
  79. },
  80. _generatePlots: function(){
  81. var depth = 0, m = gfx3d.matrix, i = 0;
  82. for(; i < this.plots.length; ++i){
  83. depth += this.plots[i].getDepth();
  84. }
  85. for(--i; i >= 0; --i){
  86. var scene = this.view.createScene();
  87. scene.setTransform(m.translate(0, 0, -depth));
  88. this.plots[i].generate(this, scene);
  89. depth -= this.plots[i].getDepth();
  90. }
  91. return this;
  92. }
  93. });
  94. });