charting.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var CreateChartingTestScene = function (engine) {
  2. var scene = new BABYLON.Scene(engine);
  3. var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(0, -0.5, 1.0), scene);
  4. var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
  5. camera.setPosition(new BABYLON.Vector3(20, 70, -100));
  6. light.position = new BABYLON.Vector3(0, 25, -50);
  7. // Data
  8. var scale = 0.6;
  9. var operatingSystem_Series = [
  10. { label: "Macintosh", value: 12, color: new BABYLON.Color3(0, 1, 0) },
  11. { label: "Windows", value: 77, color: new BABYLON.Color3(1, 0, 0) },
  12. { label: "Linux", value: 4, color: new BABYLON.Color3(1, 0, 1) },
  13. { label: "iOS", value: 3, color: new BABYLON.Color3(1, 1, 0) },
  14. { label: "Android", value: 2, color: new BABYLON.Color3(0, 0, 1) },
  15. { label: "Win Phone", value: 1, color: new BABYLON.Color3(1, 1, 1) }
  16. ];
  17. var browsers_Series = [
  18. { label: "IE", value: 32, color: new BABYLON.Color3(0, 0, 1) },
  19. { label: "Chrome", value: 28, color: new BABYLON.Color3(1, 0, 0) },
  20. { label: "Firefox", value: 16, color: new BABYLON.Color3(1, 0, 1) },
  21. { label: "Opera", value: 14, color: new BABYLON.Color3(1, 1, 0) },
  22. { label: "Safari", value: 10, color: new BABYLON.Color3(0, 1, 1) }
  23. ];
  24. var playgroundSize = 100;
  25. // Background
  26. var background = BABYLON.Mesh.CreatePlane("background", playgroundSize, scene, false);
  27. background.material = new BABYLON.StandardMaterial("background", scene);
  28. background.scaling.y = 0.5;
  29. background.position.z = playgroundSize / 2 - 0.5;
  30. background.position.y = playgroundSize / 4;
  31. background.receiveShadows = true;
  32. var backgroundTexture = new BABYLON.DynamicTexture("dynamic texture", 512, scene, true);
  33. background.material.diffuseTexture = backgroundTexture;
  34. background.material.specularColor = new BABYLON.Color3(0, 0, 0);
  35. background.material.backFaceCulling = false;
  36. backgroundTexture.drawText("Eternalcoding", null, 80, "bold 70px Segoe UI", "white", "#555555");
  37. backgroundTexture.drawText("- browsers statistics -", null, 250, "35px Segoe UI", "white", null);
  38. //background.material.reflectionTexture = new BABYLON.MirrorTexture("mirror", 1024, scene, true);
  39. //background.material.reflectionTexture.mirrorPlane = new BABYLON.Plane(0, 0, 1.0, -playgroundSize / 2 + 0.5);
  40. //background.material.reflectionTexture.level = 0.5;
  41. // Ground
  42. var ground = BABYLON.Mesh.CreateGround("ground", playgroundSize, playgroundSize, 1, scene, false);
  43. var groundMaterial = new BABYLON.StandardMaterial("ground", scene);
  44. groundMaterial.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  45. groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  46. ground.material = groundMaterial;
  47. ground.receiveShadows = true;
  48. ground.position.y = -0.1;
  49. //background.material.reflectionTexture.renderList.push(ground);
  50. var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
  51. var createSeries = function (series) {
  52. var margin = 2;
  53. var offset = playgroundSize / (series.length) - margin;
  54. var x = -playgroundSize / 2 + offset / 2;
  55. for (var index = 0; index < series.length; index++) {
  56. var data = series[index];
  57. var bar = BABYLON.Mesh.CreateBox(data.label, 1.0, scene, false);
  58. bar.scaling = new BABYLON.Vector3(offset / 2.0, 0, offset / 2.0);
  59. bar.position.x = x;
  60. bar.position.y = 0;
  61. // Animate a bit
  62. var animation = new BABYLON.Animation("anim", "scaling", 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3);
  63. animation.setKeys([
  64. { frame: 0, value: new BABYLON.Vector3(offset / 2.0, 0, offset / 2.0) },
  65. { frame: 100, value: new BABYLON.Vector3(offset / 2.0, data.value * scale, offset / 2.0) }]);
  66. bar.animations.push(animation);
  67. animation = new BABYLON.Animation("anim2", "position.y", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT);
  68. animation.setKeys([
  69. { frame: 0, value: 0 },
  70. { frame: 100, value: (data.value * scale) / 2 }]);
  71. bar.animations.push(animation);
  72. scene.beginAnimation(bar, 0, 100, false, 2.0);
  73. // Material
  74. bar.material = new BABYLON.StandardMaterial(data.label + "mat", scene);
  75. bar.material.diffuseColor = data.color;
  76. bar.material.emissiveColor = data.color.scale(0.3);
  77. bar.material.specularColor = new BABYLON.Color3(0, 0, 0);
  78. // Shadows
  79. shadowGenerator.getShadowMap().renderList.push(bar);
  80. // Mirror
  81. // background.material.reflectionTexture.renderList.push(bar);
  82. // Legend
  83. var barLegend = BABYLON.Mesh.CreateGround(data.label + "Legend", playgroundSize / 2, offset * 2, 1, scene, false);
  84. barLegend.position.x = x;
  85. barLegend.position.z = -playgroundSize / 4;
  86. barLegend.rotation.y = Math.PI / 2;
  87. barLegend.material = new BABYLON.StandardMaterial(data.label + "LegendMat", scene);
  88. var barLegendTexture = new BABYLON.DynamicTexture("dynamic texture", 512, scene, true);
  89. barLegendTexture.hasAlpha = true;
  90. barLegend.material.diffuseTexture = barLegendTexture;
  91. barLegend.material.emissiveColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  92. var size = barLegendTexture.getSize();
  93. barLegendTexture.drawText(data.label + " (" + data.value + "%)", 80, size.height / 2 + 30, "bold 50px Segoe UI", "white", "transparent");
  94. // background.material.reflectionTexture.renderList.push(barLegend);
  95. // Going next
  96. x += offset + margin;
  97. }
  98. };
  99. createSeries(browsers_Series);
  100. // Limit camera
  101. camera.lowerAlphaLimit = Math.PI;
  102. camera.upperAlphaLimit = 2 * Math.PI;
  103. camera.lowerBetaLimit = 0.1;
  104. camera.upperBetaLimit = (Math.PI / 2) * 0.99;
  105. camera.lowerRadiusLimit = 5;
  106. camera.upperRadiusLimit = 150;
  107. return scene;
  108. };