vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2854 lines
85 KiB

  1. var Emitter = require('emitter-component');
  2. var DataSet = require('../DataSet');
  3. var DataView = require('../DataView');
  4. var Point3d = require('./Point3d');
  5. var Point2d = require('./Point2d');
  6. var Filter = require('./Filter');
  7. var StepNumber = require('./StepNumber');
  8. /**
  9. * @constructor Graph3d
  10. * Graph3d displays data in 3d.
  11. *
  12. * Graph3d is developed in javascript as a Google Visualization Chart.
  13. *
  14. * @param {Element} container The DOM element in which the Graph3d will
  15. * be created. Normally a div element.
  16. * @param {DataSet | DataView | Array} [data]
  17. * @param {Object} [options]
  18. */
  19. function Graph3d(container, data, options) {
  20. if (!(this instanceof Graph3d)) {
  21. throw new SyntaxError('Constructor must be called with the new operator');
  22. }
  23. // create variables and set default values
  24. this.containerElement = container;
  25. this.width = '400px';
  26. this.height = '400px';
  27. this.margin = 10; // px
  28. this.defaultXCenter = '55%';
  29. this.defaultYCenter = '50%';
  30. this.xLabel = 'x';
  31. this.yLabel = 'y';
  32. this.zLabel = 'z';
  33. this.filterLabel = 'time';
  34. this.legendLabel = 'value';
  35. this.style = Graph3d.STYLE.DOT;
  36. this.showPerspective = true;
  37. this.showGrid = true;
  38. this.keepAspectRatio = true;
  39. this.showShadow = false;
  40. this.showGrayBottom = false; // TODO: this does not work correctly
  41. this.showTooltip = false;
  42. this.verticalRatio = 0.5; // 0.1 to 1.0, where 1.0 results in a 'cube'
  43. this.animationInterval = 1000; // milliseconds
  44. this.animationPreload = false;
  45. this.camera = new Graph3d.Camera();
  46. this.eye = new Point3d(0, 0, -1); // TODO: set eye.z about 3/4 of the width of the window?
  47. this.dataTable = null; // The original data table
  48. this.dataPoints = null; // The table with point objects
  49. // the column indexes
  50. this.colX = undefined;
  51. this.colY = undefined;
  52. this.colZ = undefined;
  53. this.colValue = undefined;
  54. this.colFilter = undefined;
  55. this.xMin = 0;
  56. this.xStep = undefined; // auto by default
  57. this.xMax = 1;
  58. this.yMin = 0;
  59. this.yStep = undefined; // auto by default
  60. this.yMax = 1;
  61. this.zMin = 0;
  62. this.zStep = undefined; // auto by default
  63. this.zMax = 1;
  64. this.valueMin = 0;
  65. this.valueMax = 1;
  66. this.xBarWidth = 1;
  67. this.yBarWidth = 1;
  68. // TODO: customize axis range
  69. // constants
  70. this.colorAxis = '#4D4D4D';
  71. this.colorGrid = '#D3D3D3';
  72. this.colorDot = '#7DC1FF';
  73. this.colorDotBorder = '#3267D2';
  74. // create a frame and canvas
  75. this.create();
  76. // apply options (also when undefined)
  77. this.setOptions(options);
  78. // apply data
  79. if (data) {
  80. this.setData(data);
  81. }
  82. }
  83. // Extend Graph3d with an Emitter mixin
  84. Emitter(Graph3d.prototype);
  85. /**
  86. * @class Camera
  87. * The camera is mounted on a (virtual) camera arm. The camera arm can rotate
  88. * The camera is always looking in the direction of the origin of the arm.
  89. * This way, the camera always rotates around one fixed point, the location
  90. * of the camera arm.
  91. *
  92. * Documentation:
  93. * http://en.wikipedia.org/wiki/3D_projection
  94. */
  95. Graph3d.Camera = function () {
  96. this.armLocation = new Point3d();
  97. this.armRotation = {};
  98. this.armRotation.horizontal = 0;
  99. this.armRotation.vertical = 0;
  100. this.armLength = 1.7;
  101. this.cameraLocation = new Point3d();
  102. this.cameraRotation = new Point3d(0.5*Math.PI, 0, 0);
  103. this.calculateCameraOrientation();
  104. };
  105. /**
  106. * Set the location (origin) of the arm
  107. * @param {Number} x Normalized value of x
  108. * @param {Number} y Normalized value of y
  109. * @param {Number} z Normalized value of z
  110. */
  111. Graph3d.Camera.prototype.setArmLocation = function(x, y, z) {
  112. this.armLocation.x = x;
  113. this.armLocation.y = y;
  114. this.armLocation.z = z;
  115. this.calculateCameraOrientation();
  116. };
  117. /**
  118. * Set the rotation of the camera arm
  119. * @param {Number} horizontal The horizontal rotation, between 0 and 2*PI.
  120. * Optional, can be left undefined.
  121. * @param {Number} vertical The vertical rotation, between 0 and 0.5*PI
  122. * if vertical=0.5*PI, the graph is shown from the
  123. * top. Optional, can be left undefined.
  124. */
  125. Graph3d.Camera.prototype.setArmRotation = function(horizontal, vertical) {
  126. if (horizontal !== undefined) {
  127. this.armRotation.horizontal = horizontal;
  128. }
  129. if (vertical !== undefined) {
  130. this.armRotation.vertical = vertical;
  131. if (this.armRotation.vertical < 0) this.armRotation.vertical = 0;
  132. if (this.armRotation.vertical > 0.5*Math.PI) this.armRotation.vertical = 0.5*Math.PI;
  133. }
  134. if (horizontal !== undefined || vertical !== undefined) {
  135. this.calculateCameraOrientation();
  136. }
  137. };
  138. /**
  139. * Retrieve the current arm rotation
  140. * @return {object} An object with parameters horizontal and vertical
  141. */
  142. Graph3d.Camera.prototype.getArmRotation = function() {
  143. var rot = {};
  144. rot.horizontal = this.armRotation.horizontal;
  145. rot.vertical = this.armRotation.vertical;
  146. return rot;
  147. };
  148. /**
  149. * Set the (normalized) length of the camera arm.
  150. * @param {Number} length A length between 0.71 and 5.0
  151. */
  152. Graph3d.Camera.prototype.setArmLength = function(length) {
  153. if (length === undefined)
  154. return;
  155. this.armLength = length;
  156. // Radius must be larger than the corner of the graph,
  157. // which has a distance of sqrt(0.5^2+0.5^2) = 0.71 from the center of the
  158. // graph
  159. if (this.armLength < 0.71) this.armLength = 0.71;
  160. if (this.armLength > 5.0) this.armLength = 5.0;
  161. this.calculateCameraOrientation();
  162. };
  163. /**
  164. * Retrieve the arm length
  165. * @return {Number} length
  166. */
  167. Graph3d.Camera.prototype.getArmLength = function() {
  168. return this.armLength;
  169. };
  170. /**
  171. * Retrieve the camera location
  172. * @return {Point3d} cameraLocation
  173. */
  174. Graph3d.Camera.prototype.getCameraLocation = function() {
  175. return this.cameraLocation;
  176. };
  177. /**
  178. * Retrieve the camera rotation
  179. * @return {Point3d} cameraRotation
  180. */
  181. Graph3d.Camera.prototype.getCameraRotation = function() {
  182. return this.cameraRotation;
  183. };
  184. /**
  185. * Calculate the location and rotation of the camera based on the
  186. * position and orientation of the camera arm
  187. */
  188. Graph3d.Camera.prototype.calculateCameraOrientation = function() {
  189. // calculate location of the camera
  190. this.cameraLocation.x = this.armLocation.x - this.armLength * Math.sin(this.armRotation.horizontal) * Math.cos(this.armRotation.vertical);
  191. this.cameraLocation.y = this.armLocation.y - this.armLength * Math.cos(this.armRotation.horizontal) * Math.cos(this.armRotation.vertical);
  192. this.cameraLocation.z = this.armLocation.z + this.armLength * Math.sin(this.armRotation.vertical);
  193. // calculate rotation of the camera
  194. this.cameraRotation.x = Math.PI/2 - this.armRotation.vertical;
  195. this.cameraRotation.y = 0;
  196. this.cameraRotation.z = -this.armRotation.horizontal;
  197. };
  198. /**
  199. * Calculate the scaling values, dependent on the range in x, y, and z direction
  200. */
  201. Graph3d.prototype._setScale = function() {
  202. this.scale = new Point3d(1 / (this.xMax - this.xMin),
  203. 1 / (this.yMax - this.yMin),
  204. 1 / (this.zMax - this.zMin));
  205. // keep aspect ration between x and y scale if desired
  206. if (this.keepAspectRatio) {
  207. if (this.scale.x < this.scale.y) {
  208. //noinspection JSSuspiciousNameCombination
  209. this.scale.y = this.scale.x;
  210. }
  211. else {
  212. //noinspection JSSuspiciousNameCombination
  213. this.scale.x = this.scale.y;
  214. }
  215. }
  216. // scale the vertical axis
  217. this.scale.z *= this.verticalRatio;
  218. // TODO: can this be automated? verticalRatio?
  219. // determine scale for (optional) value
  220. this.scale.value = 1 / (this.valueMax - this.valueMin);
  221. // position the camera arm
  222. var xCenter = (this.xMax + this.xMin) / 2 * this.scale.x;
  223. var yCenter = (this.yMax + this.yMin) / 2 * this.scale.y;
  224. var zCenter = (this.zMax + this.zMin) / 2 * this.scale.z;
  225. this.camera.setArmLocation(xCenter, yCenter, zCenter);
  226. };
  227. /**
  228. * Convert a 3D location to a 2D location on screen
  229. * http://en.wikipedia.org/wiki/3D_projection
  230. * @param {Point3d} point3d A 3D point with parameters x, y, z
  231. * @return {Point2d} point2d A 2D point with parameters x, y
  232. */
  233. Graph3d.prototype._convert3Dto2D = function(point3d) {
  234. var translation = this._convertPointToTranslation(point3d);
  235. return this._convertTranslationToScreen(translation);
  236. };
  237. /**
  238. * Convert a 3D location its translation seen from the camera
  239. * http://en.wikipedia.org/wiki/3D_projection
  240. * @param {Point3d} point3d A 3D point with parameters x, y, z
  241. * @return {Point3d} translation A 3D point with parameters x, y, z This is
  242. * the translation of the point, seen from the
  243. * camera
  244. */
  245. Graph3d.prototype._convertPointToTranslation = function(point3d) {
  246. var ax = point3d.x * this.scale.x,
  247. ay = point3d.y * this.scale.y,
  248. az = point3d.z * this.scale.z,
  249. cx = this.camera.getCameraLocation().x,
  250. cy = this.camera.getCameraLocation().y,
  251. cz = this.camera.getCameraLocation().z,
  252. // calculate angles
  253. sinTx = Math.sin(this.camera.getCameraRotation().x),
  254. cosTx = Math.cos(this.camera.getCameraRotation().x),
  255. sinTy = Math.sin(this.camera.getCameraRotation().y),
  256. cosTy = Math.cos(this.camera.getCameraRotation().y),
  257. sinTz = Math.sin(this.camera.getCameraRotation().z),
  258. cosTz = Math.cos(this.camera.getCameraRotation().z),
  259. // calculate translation
  260. dx = cosTy * (sinTz * (ay - cy) + cosTz * (ax - cx)) - sinTy * (az - cz),
  261. dy = sinTx * (cosTy * (az - cz) + sinTy * (sinTz * (ay - cy) + cosTz * (ax - cx))) + cosTx * (cosTz * (ay - cy) - sinTz * (ax-cx)),
  262. dz = cosTx * (cosTy * (az - cz) + sinTy * (sinTz * (ay - cy) + cosTz * (ax - cx))) - sinTx * (cosTz * (ay - cy) - sinTz * (ax-cx));
  263. return new Point3d(dx, dy, dz);
  264. };
  265. /**
  266. * Convert a translation point to a point on the screen
  267. * @param {Point3d} translation A 3D point with parameters x, y, z This is
  268. * the translation of the point, seen from the
  269. * camera
  270. * @return {Point2d} point2d A 2D point with parameters x, y
  271. */
  272. Graph3d.prototype._convertTranslationToScreen = function(translation) {
  273. var ex = this.eye.x,
  274. ey = this.eye.y,
  275. ez = this.eye.z,
  276. dx = translation.x,
  277. dy = translation.y,
  278. dz = translation.z;
  279. // calculate position on screen from translation
  280. var bx;
  281. var by;
  282. if (this.showPerspective) {
  283. bx = (dx - ex) * (ez / dz);
  284. by = (dy - ey) * (ez / dz);
  285. }
  286. else {
  287. bx = dx * -(ez / this.camera.getArmLength());
  288. by = dy * -(ez / this.camera.getArmLength());
  289. }
  290. // shift and scale the point to the center of the screen
  291. // use the width of the graph to scale both horizontally and vertically.
  292. return new Point2d(
  293. this.xcenter + bx * this.frame.canvas.clientWidth,
  294. this.ycenter - by * this.frame.canvas.clientWidth);
  295. };
  296. /**
  297. * Set the background styling for the graph
  298. * @param {string | {fill: string, stroke: string, strokeWidth: string}} backgroundColor
  299. */
  300. Graph3d.prototype._setBackgroundColor = function(backgroundColor) {
  301. var fill = 'white';
  302. var stroke = 'gray';
  303. var strokeWidth = 1;
  304. if (typeof(backgroundColor) === 'string') {
  305. fill = backgroundColor;
  306. stroke = 'none';
  307. strokeWidth = 0;
  308. }
  309. else if (typeof(backgroundColor) === 'object') {
  310. if (backgroundColor.fill !== undefined) fill = backgroundColor.fill;
  311. if (backgroundColor.stroke !== undefined) stroke = backgroundColor.stroke;
  312. if (backgroundColor.strokeWidth !== undefined) strokeWidth = backgroundColor.strokeWidth;
  313. }
  314. else if (backgroundColor === undefined) {
  315. // use use defaults
  316. }
  317. else {
  318. throw 'Unsupported type of backgroundColor';
  319. }
  320. this.frame.style.backgroundColor = fill;
  321. this.frame.style.borderColor = stroke;
  322. this.frame.style.borderWidth = strokeWidth + 'px';
  323. this.frame.style.borderStyle = 'solid';
  324. };
  325. /// enumerate the available styles
  326. Graph3d.STYLE = {
  327. BAR: 0,
  328. BARCOLOR: 1,
  329. BARSIZE: 2,
  330. DOT : 3,
  331. DOTLINE : 4,
  332. DOTCOLOR: 5,
  333. DOTSIZE: 6,
  334. GRID : 7,
  335. LINE: 8,
  336. SURFACE : 9
  337. };
  338. /**
  339. * Retrieve the style index from given styleName
  340. * @param {string} styleName Style name such as 'dot', 'grid', 'dot-line'
  341. * @return {Number} styleNumber Enumeration value representing the style, or -1
  342. * when not found
  343. */
  344. Graph3d.prototype._getStyleNumber = function(styleName) {
  345. switch (styleName) {
  346. case 'dot': return Graph3d.STYLE.DOT;
  347. case 'dot-line': return Graph3d.STYLE.DOTLINE;
  348. case 'dot-color': return Graph3d.STYLE.DOTCOLOR;
  349. case 'dot-size': return Graph3d.STYLE.DOTSIZE;
  350. case 'line': return Graph3d.STYLE.LINE;
  351. case 'grid': return Graph3d.STYLE.GRID;
  352. case 'surface': return Graph3d.STYLE.SURFACE;
  353. case 'bar': return Graph3d.STYLE.BAR;
  354. case 'bar-color': return Graph3d.STYLE.BARCOLOR;
  355. case 'bar-size': return Graph3d.STYLE.BARSIZE;
  356. }
  357. return -1;
  358. };
  359. /**
  360. * Determine the indexes of the data columns, based on the given style and data
  361. * @param {DataSet} data
  362. * @param {Number} style
  363. */
  364. Graph3d.prototype._determineColumnIndexes = function(data, style) {
  365. if (this.style === Graph3d.STYLE.DOT ||
  366. this.style === Graph3d.STYLE.DOTLINE ||
  367. this.style === Graph3d.STYLE.LINE ||
  368. this.style === Graph3d.STYLE.GRID ||
  369. this.style === Graph3d.STYLE.SURFACE ||
  370. this.style === Graph3d.STYLE.BAR) {
  371. // 3 columns expected, and optionally a 4th with filter values
  372. this.colX = 0;
  373. this.colY = 1;
  374. this.colZ = 2;
  375. this.colValue = undefined;
  376. if (data.getNumberOfColumns() > 3) {
  377. this.colFilter = 3;
  378. }
  379. }
  380. else if (this.style === Graph3d.STYLE.DOTCOLOR ||
  381. this.style === Graph3d.STYLE.DOTSIZE ||
  382. this.style === Graph3d.STYLE.BARCOLOR ||
  383. this.style === Graph3d.STYLE.BARSIZE) {
  384. // 4 columns expected, and optionally a 5th with filter values
  385. this.colX = 0;
  386. this.colY = 1;
  387. this.colZ = 2;
  388. this.colValue = 3;
  389. if (data.getNumberOfColumns() > 4) {
  390. this.colFilter = 4;
  391. }
  392. }
  393. else {
  394. throw 'Unknown style "' + this.style + '"';
  395. }
  396. };
  397. Graph3d.prototype.getNumberOfRows = function(data) {
  398. return data.length;
  399. }
  400. Graph3d.prototype.getNumberOfColumns = function(data) {
  401. var counter = 0;
  402. for (var column in data[0]) {
  403. if (data[0].hasOwnProperty(column)) {
  404. counter++;
  405. }
  406. }
  407. return counter;
  408. }
  409. Graph3d.prototype.getDistinctValues = function(data, column) {
  410. var distinctValues = [];
  411. for (var i = 0; i < data.length; i++) {
  412. if (distinctValues.indexOf(data[i][column]) == -1) {
  413. distinctValues.push(data[i][column]);
  414. }
  415. }
  416. return distinctValues;
  417. }
  418. Graph3d.prototype.getColumnRange = function(data,column) {
  419. var minMax = {min:data[0][column],max:data[0][column]};
  420. for (var i = 0; i < data.length; i++) {
  421. if (minMax.min > data[i][column]) { minMax.min = data[i][column]; }
  422. if (minMax.max < data[i][column]) { minMax.max = data[i][column]; }
  423. }
  424. return minMax;
  425. };
  426. /**
  427. * Initialize the data from the data table. Calculate minimum and maximum values
  428. * and column index values
  429. * @param {Array | DataSet | DataView} rawData The data containing the items for the Graph.
  430. * @param {Number} style Style Number
  431. */
  432. Graph3d.prototype._dataInitialize = function (rawData, style) {
  433. var me = this;
  434. // unsubscribe from the dataTable
  435. if (this.dataSet) {
  436. this.dataSet.off('*', this._onChange);
  437. }
  438. if (rawData === undefined)
  439. return;
  440. if (Array.isArray(rawData)) {
  441. rawData = new DataSet(rawData);
  442. }
  443. var data;
  444. if (rawData instanceof DataSet || rawData instanceof DataView) {
  445. data = rawData.get();
  446. }
  447. else {
  448. throw new Error('Array, DataSet, or DataView expected');
  449. }
  450. if (data.length == 0)
  451. return;
  452. this.dataSet = rawData;
  453. this.dataTable = data;
  454. // subscribe to changes in the dataset
  455. this._onChange = function () {
  456. me.setData(me.dataSet);
  457. };
  458. this.dataSet.on('*', this._onChange);
  459. // _determineColumnIndexes
  460. // getNumberOfRows (points)
  461. // getNumberOfColumns (x,y,z,v,t,t1,t2...)
  462. // getDistinctValues (unique values?)
  463. // getColumnRange
  464. // determine the location of x,y,z,value,filter columns
  465. this.colX = 'x';
  466. this.colY = 'y';
  467. this.colZ = 'z';
  468. this.colValue = 'style';
  469. this.colFilter = 'filter';
  470. // check if a filter column is provided
  471. if (data[0].hasOwnProperty('filter')) {
  472. if (this.dataFilter === undefined) {
  473. this.dataFilter = new Filter(rawData, this.colFilter, this);
  474. this.dataFilter.setOnLoadCallback(function() {me.redraw();});
  475. }
  476. }
  477. var withBars = this.style == Graph3d.STYLE.BAR ||
  478. this.style == Graph3d.STYLE.BARCOLOR ||
  479. this.style == Graph3d.STYLE.BARSIZE;
  480. // determine barWidth from data
  481. if (withBars) {
  482. if (this.defaultXBarWidth !== undefined) {
  483. this.xBarWidth = this.defaultXBarWidth;
  484. }
  485. else {
  486. var dataX = this.getDistinctValues(data,this.colX);
  487. this.xBarWidth = (dataX[1] - dataX[0]) || 1;
  488. }
  489. if (this.defaultYBarWidth !== undefined) {
  490. this.yBarWidth = this.defaultYBarWidth;
  491. }
  492. else {
  493. var dataY = this.getDistinctValues(data,this.colY);
  494. this.yBarWidth = (dataY[1] - dataY[0]) || 1;
  495. }
  496. }
  497. // calculate minimums and maximums
  498. var xRange = this.getColumnRange(data,this.colX);
  499. if (withBars) {
  500. xRange.min -= this.xBarWidth / 2;
  501. xRange.max += this.xBarWidth / 2;
  502. }
  503. this.xMin = (this.defaultXMin !== undefined) ? this.defaultXMin : xRange.min;
  504. this.xMax = (this.defaultXMax !== undefined) ? this.defaultXMax : xRange.max;
  505. if (this.xMax <= this.xMin) this.xMax = this.xMin + 1;
  506. this.xStep = (this.defaultXStep !== undefined) ? this.defaultXStep : (this.xMax-this.xMin)/5;
  507. var yRange = this.getColumnRange(data,this.colY);
  508. if (withBars) {
  509. yRange.min -= this.yBarWidth / 2;
  510. yRange.max += this.yBarWidth / 2;
  511. }
  512. this.yMin = (this.defaultYMin !== undefined) ? this.defaultYMin : yRange.min;
  513. this.yMax = (this.defaultYMax !== undefined) ? this.defaultYMax : yRange.max;
  514. if (this.yMax <= this.yMin) this.yMax = this.yMin + 1;
  515. this.yStep = (this.defaultYStep !== undefined) ? this.defaultYStep : (this.yMax-this.yMin)/5;
  516. var zRange = this.getColumnRange(data,this.colZ);
  517. this.zMin = (this.defaultZMin !== undefined) ? this.defaultZMin : zRange.min;
  518. this.zMax = (this.defaultZMax !== undefined) ? this.defaultZMax : zRange.max;
  519. if (this.zMax <= this.zMin) this.zMax = this.zMin + 1;
  520. this.zStep = (this.defaultZStep !== undefined) ? this.defaultZStep : (this.zMax-this.zMin)/5;
  521. if (this.colValue !== undefined) {
  522. var valueRange = this.getColumnRange(data,this.colValue);
  523. this.valueMin = (this.defaultValueMin !== undefined) ? this.defaultValueMin : valueRange.min;
  524. this.valueMax = (this.defaultValueMax !== undefined) ? this.defaultValueMax : valueRange.max;
  525. if (this.valueMax <= this.valueMin) this.valueMax = this.valueMin + 1;
  526. }
  527. // set the scale dependent on the ranges.
  528. this._setScale();
  529. };
  530. /**
  531. * Filter the data based on the current filter
  532. * @param {Array} data
  533. * @return {Array} dataPoints Array with point objects which can be drawn on screen
  534. */
  535. Graph3d.prototype._getDataPoints = function (data) {
  536. // TODO: store the created matrix dataPoints in the filters instead of reloading each time
  537. var x, y, i, z, obj, point;
  538. var dataPoints = [];
  539. if (this.style === Graph3d.STYLE.GRID ||
  540. this.style === Graph3d.STYLE.SURFACE) {
  541. // copy all values from the google data table to a matrix
  542. // the provided values are supposed to form a grid of (x,y) positions
  543. // create two lists with all present x and y values
  544. var dataX = [];
  545. var dataY = [];
  546. for (i = 0; i < this.getNumberOfRows(data); i++) {
  547. x = data[i][this.colX] || 0;
  548. y = data[i][this.colY] || 0;
  549. if (dataX.indexOf(x) === -1) {
  550. dataX.push(x);
  551. }
  552. if (dataY.indexOf(y) === -1) {
  553. dataY.push(y);
  554. }
  555. }
  556. function sortNumber(a, b) {
  557. return a - b;
  558. }
  559. dataX.sort(sortNumber);
  560. dataY.sort(sortNumber);
  561. // create a grid, a 2d matrix, with all values.
  562. var dataMatrix = []; // temporary data matrix
  563. for (i = 0; i < data.length; i++) {
  564. x = data[i][this.colX] || 0;
  565. y = data[i][this.colY] || 0;
  566. z = data[i][this.colZ] || 0;
  567. var xIndex = dataX.indexOf(x); // TODO: implement Array().indexOf() for Internet Explorer
  568. var yIndex = dataY.indexOf(y);
  569. if (dataMatrix[xIndex] === undefined) {
  570. dataMatrix[xIndex] = [];
  571. }
  572. var point3d = new Point3d();
  573. point3d.x = x;
  574. point3d.y = y;
  575. point3d.z = z;
  576. obj = {};
  577. obj.point = point3d;
  578. obj.trans = undefined;
  579. obj.screen = undefined;
  580. obj.bottom = new Point3d(x, y, this.zMin);
  581. dataMatrix[xIndex][yIndex] = obj;
  582. dataPoints.push(obj);
  583. }
  584. // fill in the pointers to the neighbors.
  585. for (x = 0; x < dataMatrix.length; x++) {
  586. for (y = 0; y < dataMatrix[x].length; y++) {
  587. if (dataMatrix[x][y]) {
  588. dataMatrix[x][y].pointRight = (x < dataMatrix.length-1) ? dataMatrix[x+1][y] : undefined;
  589. dataMatrix[x][y].pointTop = (y < dataMatrix[x].length-1) ? dataMatrix[x][y+1] : undefined;
  590. dataMatrix[x][y].pointCross =
  591. (x < dataMatrix.length-1 && y < dataMatrix[x].length-1) ?
  592. dataMatrix[x+1][y+1] :
  593. undefined;
  594. }
  595. }
  596. }
  597. }
  598. else { // 'dot', 'dot-line', etc.
  599. // copy all values from the google data table to a list with Point3d objects
  600. for (i = 0; i < data.length; i++) {
  601. point = new Point3d();
  602. point.x = data[i][this.colX] || 0;
  603. point.y = data[i][this.colY] || 0;
  604. point.z = data[i][this.colZ] || 0;
  605. if (this.colValue !== undefined) {
  606. point.value = data[i][this.colValue] || 0;
  607. }
  608. obj = {};
  609. obj.point = point;
  610. obj.bottom = new Point3d(point.x, point.y, this.zMin);
  611. obj.trans = undefined;
  612. obj.screen = undefined;
  613. dataPoints.push(obj);
  614. }
  615. }
  616. return dataPoints;
  617. };
  618. /**
  619. * Create the main frame for the Graph3d.
  620. * This function is executed once when a Graph3d object is created. The frame
  621. * contains a canvas, and this canvas contains all objects like the axis and
  622. * nodes.
  623. */
  624. Graph3d.prototype.create = function () {
  625. // remove all elements from the container element.
  626. while (this.containerElement.hasChildNodes()) {
  627. this.containerElement.removeChild(this.containerElement.firstChild);
  628. }
  629. this.frame = document.createElement('div');
  630. this.frame.style.position = 'relative';
  631. this.frame.style.overflow = 'hidden';
  632. // create the graph canvas (HTML canvas element)
  633. this.frame.canvas = document.createElement( 'canvas' );
  634. this.frame.canvas.style.position = 'relative';
  635. this.frame.appendChild(this.frame.canvas);
  636. //if (!this.frame.canvas.getContext) {
  637. {
  638. var noCanvas = document.createElement( 'DIV' );
  639. noCanvas.style.color = 'red';
  640. noCanvas.style.fontWeight = 'bold' ;
  641. noCanvas.style.padding = '10px';
  642. noCanvas.innerHTML = 'Error: your browser does not support HTML canvas';
  643. this.frame.canvas.appendChild(noCanvas);
  644. }
  645. this.frame.filter = document.createElement( 'div' );
  646. this.frame.filter.style.position = 'absolute';
  647. this.frame.filter.style.bottom = '0px';
  648. this.frame.filter.style.left = '0px';
  649. this.frame.filter.style.width = '100%';
  650. this.frame.appendChild(this.frame.filter);
  651. // add event listeners to handle moving and zooming the contents
  652. var me = this;
  653. var onmousedown = function (event) {me._onMouseDown(event);};
  654. var ontouchstart = function (event) {me._onTouchStart(event);};
  655. var onmousewheel = function (event) {me._onWheel(event);};
  656. var ontooltip = function (event) {me._onTooltip(event);};
  657. // TODO: these events are never cleaned up... can give a 'memory leakage'
  658. G3DaddEventListener(this.frame.canvas, 'keydown', onkeydown);
  659. G3DaddEventListener(this.frame.canvas, 'mousedown', onmousedown);
  660. G3DaddEventListener(this.frame.canvas, 'touchstart', ontouchstart);
  661. G3DaddEventListener(this.frame.canvas, 'mousewheel', onmousewheel);
  662. G3DaddEventListener(this.frame.canvas, 'mousemove', ontooltip);
  663. // add the new graph to the container element
  664. this.containerElement.appendChild(this.frame);
  665. };
  666. /**
  667. * Set a new size for the graph
  668. * @param {string} width Width in pixels or percentage (for example '800px'
  669. * or '50%')
  670. * @param {string} height Height in pixels or percentage (for example '400px'
  671. * or '30%')
  672. */
  673. Graph3d.prototype.setSize = function(width, height) {
  674. this.frame.style.width = width;
  675. this.frame.style.height = height;
  676. this._resizeCanvas();
  677. };
  678. /**
  679. * Resize the canvas to the current size of the frame
  680. */
  681. Graph3d.prototype._resizeCanvas = function() {
  682. this.frame.canvas.style.width = '100%';
  683. this.frame.canvas.style.height = '100%';
  684. this.frame.canvas.width = this.frame.canvas.clientWidth;
  685. this.frame.canvas.height = this.frame.canvas.clientHeight;
  686. // adjust with for margin
  687. this.frame.filter.style.width = (this.frame.canvas.clientWidth - 2 * 10) + 'px';
  688. };
  689. /**
  690. * Start animation
  691. */
  692. Graph3d.prototype.animationStart = function() {
  693. if (!this.frame.filter || !this.frame.filter.slider)
  694. throw 'No animation available';
  695. this.frame.filter.slider.play();
  696. };
  697. /**
  698. * Stop animation
  699. */
  700. Graph3d.prototype.animationStop = function() {
  701. if (!this.frame.filter || !this.frame.filter.slider) return;
  702. this.frame.filter.slider.stop();
  703. };
  704. /**
  705. * Resize the center position based on the current values in this.defaultXCenter
  706. * and this.defaultYCenter (which are strings with a percentage or a value
  707. * in pixels). The center positions are the variables this.xCenter
  708. * and this.yCenter
  709. */
  710. Graph3d.prototype._resizeCenter = function() {
  711. // calculate the horizontal center position
  712. if (this.defaultXCenter.charAt(this.defaultXCenter.length-1) === '%') {
  713. this.xcenter =
  714. parseFloat(this.defaultXCenter) / 100 *
  715. this.frame.canvas.clientWidth;
  716. }
  717. else {
  718. this.xcenter = parseFloat(this.defaultXCenter); // supposed to be in px
  719. }
  720. // calculate the vertical center position
  721. if (this.defaultYCenter.charAt(this.defaultYCenter.length-1) === '%') {
  722. this.ycenter =
  723. parseFloat(this.defaultYCenter) / 100 *
  724. (this.frame.canvas.clientHeight - this.frame.filter.clientHeight);
  725. }
  726. else {
  727. this.ycenter = parseFloat(this.defaultYCenter); // supposed to be in px
  728. }
  729. };
  730. /**
  731. * Set the rotation and distance of the camera
  732. * @param {Object} pos An object with the camera position. The object
  733. * contains three parameters:
  734. * - horizontal {Number}
  735. * The horizontal rotation, between 0 and 2*PI.
  736. * Optional, can be left undefined.
  737. * - vertical {Number}
  738. * The vertical rotation, between 0 and 0.5*PI
  739. * if vertical=0.5*PI, the graph is shown from the
  740. * top. Optional, can be left undefined.
  741. * - distance {Number}
  742. * The (normalized) distance of the camera to the
  743. * center of the graph, a value between 0.71 and 5.0.
  744. * Optional, can be left undefined.
  745. */
  746. Graph3d.prototype.setCameraPosition = function(pos) {
  747. if (pos === undefined) {
  748. return;
  749. }
  750. if (pos.horizontal !== undefined && pos.vertical !== undefined) {
  751. this.camera.setArmRotation(pos.horizontal, pos.vertical);
  752. }
  753. if (pos.distance !== undefined) {
  754. this.camera.setArmLength(pos.distance);
  755. }
  756. this.redraw();
  757. };
  758. /**
  759. * Retrieve the current camera rotation
  760. * @return {object} An object with parameters horizontal, vertical, and
  761. * distance
  762. */
  763. Graph3d.prototype.getCameraPosition = function() {
  764. var pos = this.camera.getArmRotation();
  765. pos.distance = this.camera.getArmLength();
  766. return pos;
  767. };
  768. /**
  769. * Load data into the 3D Graph
  770. */
  771. Graph3d.prototype._readData = function(data) {
  772. // read the data
  773. this._dataInitialize(data, this.style);
  774. if (this.dataFilter) {
  775. // apply filtering
  776. this.dataPoints = this.dataFilter._getDataPoints();
  777. }
  778. else {
  779. // no filtering. load all data
  780. this.dataPoints = this._getDataPoints(this.dataTable);
  781. }
  782. // draw the filter
  783. this._redrawFilter();
  784. };
  785. /**
  786. * Replace the dataset of the Graph3d
  787. * @param {Array | DataSet | DataView} data
  788. */
  789. Graph3d.prototype.setData = function (data) {
  790. this._readData(data);
  791. this.redraw();
  792. // start animation when option is true
  793. if (this.animationAutoStart && this.dataFilter) {
  794. this.animationStart();
  795. }
  796. };
  797. /**
  798. * Update the options. Options will be merged with current options
  799. * @param {Object} options
  800. */
  801. Graph3d.prototype.setOptions = function (options) {
  802. var cameraPosition = undefined;
  803. this.animationStop();
  804. if (options !== undefined) {
  805. // retrieve parameter values
  806. if (options.width !== undefined) this.width = options.width;
  807. if (options.height !== undefined) this.height = options.height;
  808. if (options.xCenter !== undefined) this.defaultXCenter = options.xCenter;
  809. if (options.yCenter !== undefined) this.defaultYCenter = options.yCenter;
  810. if (options.filterLabel !== undefined) this.filterLabel = options.filterLabel;
  811. if (options.legendLabel !== undefined) this.legendLabel = options.legendLabel;
  812. if (options.xLabel !== undefined) this.xLabel = options.xLabel;
  813. if (options.yLabel !== undefined) this.yLabel = options.yLabel;
  814. if (options.zLabel !== undefined) this.zLabel = options.zLabel;
  815. if (options.style !== undefined) {
  816. var styleNumber = this._getStyleNumber(options.style);
  817. if (styleNumber !== -1) {
  818. this.style = styleNumber;
  819. }
  820. }
  821. if (options.showGrid !== undefined) this.showGrid = options.showGrid;
  822. if (options.showPerspective !== undefined) this.showPerspective = options.showPerspective;
  823. if (options.showShadow !== undefined) this.showShadow = options.showShadow;
  824. if (options.tooltip !== undefined) this.showTooltip = options.tooltip;
  825. if (options.showAnimationControls !== undefined) this.showAnimationControls = options.showAnimationControls;
  826. if (options.keepAspectRatio !== undefined) this.keepAspectRatio = options.keepAspectRatio;
  827. if (options.verticalRatio !== undefined) this.verticalRatio = options.verticalRatio;
  828. if (options.animationInterval !== undefined) this.animationInterval = options.animationInterval;
  829. if (options.animationPreload !== undefined) this.animationPreload = options.animationPreload;
  830. if (options.animationAutoStart !== undefined)this.animationAutoStart = options.animationAutoStart;
  831. if (options.xBarWidth !== undefined) this.defaultXBarWidth = options.xBarWidth;
  832. if (options.yBarWidth !== undefined) this.defaultYBarWidth = options.yBarWidth;
  833. if (options.xMin !== undefined) this.defaultXMin = options.xMin;
  834. if (options.xStep !== undefined) this.defaultXStep = options.xStep;
  835. if (options.xMax !== undefined) this.defaultXMax = options.xMax;
  836. if (options.yMin !== undefined) this.defaultYMin = options.yMin;
  837. if (options.yStep !== undefined) this.defaultYStep = options.yStep;
  838. if (options.yMax !== undefined) this.defaultYMax = options.yMax;
  839. if (options.zMin !== undefined) this.defaultZMin = options.zMin;
  840. if (options.zStep !== undefined) this.defaultZStep = options.zStep;
  841. if (options.zMax !== undefined) this.defaultZMax = options.zMax;
  842. if (options.valueMin !== undefined) this.defaultValueMin = options.valueMin;
  843. if (options.valueMax !== undefined) this.defaultValueMax = options.valueMax;
  844. if (options.cameraPosition !== undefined) cameraPosition = options.cameraPosition;
  845. if (cameraPosition !== undefined) {
  846. this.camera.setArmRotation(cameraPosition.horizontal, cameraPosition.vertical);
  847. this.camera.setArmLength(cameraPosition.distance);
  848. }
  849. else {
  850. this.camera.setArmRotation(1.0, 0.5);
  851. this.camera.setArmLength(1.7);
  852. }
  853. }
  854. this._setBackgroundColor(options && options.backgroundColor);
  855. this.setSize(this.width, this.height);
  856. // re-load the data
  857. if (this.dataTable) {
  858. this.setData(this.dataTable);
  859. }
  860. // start animation when option is true
  861. if (this.animationAutoStart && this.dataFilter) {
  862. this.animationStart();
  863. }
  864. };
  865. /**
  866. * Redraw the Graph.
  867. */
  868. Graph3d.prototype.redraw = function() {
  869. if (this.dataPoints === undefined) {
  870. throw 'Error: graph data not initialized';
  871. }
  872. this._resizeCanvas();
  873. this._resizeCenter();
  874. this._redrawSlider();
  875. this._redrawClear();
  876. this._redrawAxis();
  877. if (this.style === Graph3d.STYLE.GRID ||
  878. this.style === Graph3d.STYLE.SURFACE) {
  879. this._redrawDataGrid();
  880. }
  881. else if (this.style === Graph3d.STYLE.LINE) {
  882. this._redrawDataLine();
  883. }
  884. else if (this.style === Graph3d.STYLE.BAR ||
  885. this.style === Graph3d.STYLE.BARCOLOR ||
  886. this.style === Graph3d.STYLE.BARSIZE) {
  887. this._redrawDataBar();
  888. }
  889. else {
  890. // style is DOT, DOTLINE, DOTCOLOR, DOTSIZE
  891. this._redrawDataDot();
  892. }
  893. this._redrawInfo();
  894. this._redrawLegend();
  895. };
  896. /**
  897. * Clear the canvas before redrawing
  898. */
  899. Graph3d.prototype._redrawClear = function() {
  900. var canvas = this.frame.canvas;
  901. var ctx = canvas.getContext('2d');
  902. ctx.clearRect(0, 0, canvas.width, canvas.height);
  903. };
  904. /**
  905. * Redraw the legend showing the colors
  906. */
  907. Graph3d.prototype._redrawLegend = function() {
  908. var y;
  909. if (this.style === Graph3d.STYLE.DOTCOLOR ||
  910. this.style === Graph3d.STYLE.DOTSIZE) {
  911. var dotSize = this.frame.clientWidth * 0.02;
  912. var widthMin, widthMax;
  913. if (this.style === Graph3d.STYLE.DOTSIZE) {
  914. widthMin = dotSize / 2; // px
  915. widthMax = dotSize / 2 + dotSize * 2; // Todo: put this in one function
  916. }
  917. else {
  918. widthMin = 20; // px
  919. widthMax = 20; // px
  920. }
  921. var height = Math.max(this.frame.clientHeight * 0.25, 100);
  922. var top = this.margin;
  923. var right = this.frame.clientWidth - this.margin;
  924. var left = right - widthMax;
  925. var bottom = top + height;
  926. }
  927. var canvas = this.frame.canvas;
  928. var ctx = canvas.getContext('2d');
  929. ctx.lineWidth = 1;
  930. ctx.font = '14px arial'; // TODO: put in options
  931. if (this.style === Graph3d.STYLE.DOTCOLOR) {
  932. // draw the color bar
  933. var ymin = 0;
  934. var ymax = height; // Todo: make height customizable
  935. for (y = ymin; y < ymax; y++) {
  936. var f = (y - ymin) / (ymax - ymin);
  937. //var width = (dotSize / 2 + (1-f) * dotSize * 2); // Todo: put this in one function
  938. var hue = f * 240;
  939. var color = this._hsv2rgb(hue, 1, 1);
  940. ctx.strokeStyle = color;
  941. ctx.beginPath();
  942. ctx.moveTo(left, top + y);
  943. ctx.lineTo(right, top + y);
  944. ctx.stroke();
  945. }
  946. ctx.strokeStyle = this.colorAxis;
  947. ctx.strokeRect(left, top, widthMax, height);
  948. }
  949. if (this.style === Graph3d.STYLE.DOTSIZE) {
  950. // draw border around color bar
  951. ctx.strokeStyle = this.colorAxis;
  952. ctx.fillStyle = this.colorDot;
  953. ctx.beginPath();
  954. ctx.moveTo(left, top);
  955. ctx.lineTo(right, top);
  956. ctx.lineTo(right - widthMax + widthMin, bottom);
  957. ctx.lineTo(left, bottom);
  958. ctx.closePath();
  959. ctx.fill();
  960. ctx.stroke();
  961. }
  962. if (this.style === Graph3d.STYLE.DOTCOLOR ||
  963. this.style === Graph3d.STYLE.DOTSIZE) {
  964. // print values along the color bar
  965. var gridLineLen = 5; // px
  966. var step = new StepNumber(this.valueMin, this.valueMax, (this.valueMax-this.valueMin)/5, true);
  967. step.start();
  968. if (step.getCurrent() < this.valueMin) {
  969. step.next();
  970. }
  971. while (!step.end()) {
  972. y = bottom - (step.getCurrent() - this.valueMin) / (this.valueMax - this.valueMin) * height;
  973. ctx.beginPath();
  974. ctx.moveTo(left - gridLineLen, y);
  975. ctx.lineTo(left, y);
  976. ctx.stroke();
  977. ctx.textAlign = 'right';
  978. ctx.textBaseline = 'middle';
  979. ctx.fillStyle = this.colorAxis;
  980. ctx.fillText(step.getCurrent(), left - 2 * gridLineLen, y);
  981. step.next();
  982. }
  983. ctx.textAlign = 'right';
  984. ctx.textBaseline = 'top';
  985. var label = this.legendLabel;
  986. ctx.fillText(label, right, bottom + this.margin);
  987. }
  988. };
  989. /**
  990. * Redraw the filter
  991. */
  992. Graph3d.prototype._redrawFilter = function() {
  993. this.frame.filter.innerHTML = '';
  994. if (this.dataFilter) {
  995. var options = {
  996. 'visible': this.showAnimationControls
  997. };
  998. var slider = new Slider(this.frame.filter, options);
  999. this.frame.filter.slider = slider;
  1000. // TODO: css here is not nice here...
  1001. this.frame.filter.style.padding = '10px';
  1002. //this.frame.filter.style.backgroundColor = '#EFEFEF';
  1003. slider.setValues(this.dataFilter.values);
  1004. slider.setPlayInterval(this.animationInterval);
  1005. // create an event handler
  1006. var me = this;
  1007. var onchange = function () {
  1008. var index = slider.getIndex();
  1009. me.dataFilter.selectValue(index);
  1010. me.dataPoints = me.dataFilter._getDataPoints();
  1011. me.redraw();
  1012. };
  1013. slider.setOnChangeCallback(onchange);
  1014. }
  1015. else {
  1016. this.frame.filter.slider = undefined;
  1017. }
  1018. };
  1019. /**
  1020. * Redraw the slider
  1021. */
  1022. Graph3d.prototype._redrawSlider = function() {
  1023. if ( this.frame.filter.slider !== undefined) {
  1024. this.frame.filter.slider.redraw();
  1025. }
  1026. };
  1027. /**
  1028. * Redraw common information
  1029. */
  1030. Graph3d.prototype._redrawInfo = function() {
  1031. if (this.dataFilter) {
  1032. var canvas = this.frame.canvas;
  1033. var ctx = canvas.getContext('2d');
  1034. ctx.font = '14px arial'; // TODO: put in options
  1035. ctx.lineStyle = 'gray';
  1036. ctx.fillStyle = 'gray';
  1037. ctx.textAlign = 'left';
  1038. ctx.textBaseline = 'top';
  1039. var x = this.margin;
  1040. var y = this.margin;
  1041. ctx.fillText(this.dataFilter.getLabel() + ': ' + this.dataFilter.getSelectedValue(), x, y);
  1042. }
  1043. };
  1044. /**
  1045. * Redraw the axis
  1046. */
  1047. Graph3d.prototype._redrawAxis = function() {
  1048. var canvas = this.frame.canvas,
  1049. ctx = canvas.getContext('2d'),
  1050. from, to, step, prettyStep,
  1051. text, xText, yText, zText,
  1052. offset, xOffset, yOffset,
  1053. xMin2d, xMax2d;
  1054. // TODO: get the actual rendered style of the containerElement
  1055. //ctx.font = this.containerElement.style.font;
  1056. ctx.font = 24 / this.camera.getArmLength() + 'px arial';
  1057. // calculate the length for the short grid lines
  1058. var gridLenX = 0.025 / this.scale.x;
  1059. var gridLenY = 0.025 / this.scale.y;
  1060. var textMargin = 5 / this.camera.getArmLength(); // px
  1061. var armAngle = this.camera.getArmRotation().horizontal;
  1062. // draw x-grid lines
  1063. ctx.lineWidth = 1;
  1064. prettyStep = (this.defaultXStep === undefined);
  1065. step = new StepNumber(this.xMin, this.xMax, this.xStep, prettyStep);
  1066. step.start();
  1067. if (step.getCurrent() < this.xMin) {
  1068. step.next();
  1069. }
  1070. while (!step.end()) {
  1071. var x = step.getCurrent();
  1072. if (this.showGrid) {
  1073. from = this._convert3Dto2D(new Point3d(x, this.yMin, this.zMin));
  1074. to = this._convert3Dto2D(new Point3d(x, this.yMax, this.zMin));
  1075. ctx.strokeStyle = this.colorGrid;
  1076. ctx.beginPath();
  1077. ctx.moveTo(from.x, from.y);
  1078. ctx.lineTo(to.x, to.y);
  1079. ctx.stroke();
  1080. }
  1081. else {
  1082. from = this._convert3Dto2D(new Point3d(x, this.yMin, this.zMin));
  1083. to = this._convert3Dto2D(new Point3d(x, this.yMin+gridLenX, this.zMin));
  1084. ctx.strokeStyle = this.colorAxis;
  1085. ctx.beginPath();
  1086. ctx.moveTo(from.x, from.y);
  1087. ctx.lineTo(to.x, to.y);
  1088. ctx.stroke();
  1089. from = this._convert3Dto2D(new Point3d(x, this.yMax, this.zMin));
  1090. to = this._convert3Dto2D(new Point3d(x, this.yMax-gridLenX, this.zMin));
  1091. ctx.strokeStyle = this.colorAxis;
  1092. ctx.beginPath();
  1093. ctx.moveTo(from.x, from.y);
  1094. ctx.lineTo(to.x, to.y);
  1095. ctx.stroke();
  1096. }
  1097. yText = (Math.cos(armAngle) > 0) ? this.yMin : this.yMax;
  1098. text = this._convert3Dto2D(new Point3d(x, yText, this.zMin));
  1099. if (Math.cos(armAngle * 2) > 0) {
  1100. ctx.textAlign = 'center';
  1101. ctx.textBaseline = 'top';
  1102. text.y += textMargin;
  1103. }
  1104. else if (Math.sin(armAngle * 2) < 0){
  1105. ctx.textAlign = 'right';
  1106. ctx.textBaseline = 'middle';
  1107. }
  1108. else {
  1109. ctx.textAlign = 'left';
  1110. ctx.textBaseline = 'middle';
  1111. }
  1112. ctx.fillStyle = this.colorAxis;
  1113. ctx.fillText(' ' + step.getCurrent() + ' ', text.x, text.y);
  1114. step.next();
  1115. }
  1116. // draw y-grid lines
  1117. ctx.lineWidth = 1;
  1118. prettyStep = (this.defaultYStep === undefined);
  1119. step = new StepNumber(this.yMin, this.yMax, this.yStep, prettyStep);
  1120. step.start();
  1121. if (step.getCurrent() < this.yMin) {
  1122. step.next();
  1123. }
  1124. while (!step.end()) {
  1125. if (this.showGrid) {
  1126. from = this._convert3Dto2D(new Point3d(this.xMin, step.getCurrent(), this.zMin));
  1127. to = this._convert3Dto2D(new Point3d(this.xMax, step.getCurrent(), this.zMin));
  1128. ctx.strokeStyle = this.colorGrid;
  1129. ctx.beginPath();
  1130. ctx.moveTo(from.x, from.y);
  1131. ctx.lineTo(to.x, to.y);
  1132. ctx.stroke();
  1133. }
  1134. else {
  1135. from = this._convert3Dto2D(new Point3d(this.xMin, step.getCurrent(), this.zMin));
  1136. to = this._convert3Dto2D(new Point3d(this.xMin+gridLenY, step.getCurrent(), this.zMin));
  1137. ctx.strokeStyle = this.colorAxis;
  1138. ctx.beginPath();
  1139. ctx.moveTo(from.x, from.y);
  1140. ctx.lineTo(to.x, to.y);
  1141. ctx.stroke();
  1142. from = this._convert3Dto2D(new Point3d(this.xMax, step.getCurrent(), this.zMin));
  1143. to = this._convert3Dto2D(new Point3d(this.xMax-gridLenY, step.getCurrent(), this.zMin));
  1144. ctx.strokeStyle = this.colorAxis;
  1145. ctx.beginPath();
  1146. ctx.moveTo(from.x, from.y);
  1147. ctx.lineTo(to.x, to.y);
  1148. ctx.stroke();
  1149. }
  1150. xText = (Math.sin(armAngle ) > 0) ? this.xMin : this.xMax;
  1151. text = this._convert3Dto2D(new Point3d(xText, step.getCurrent(), this.zMin));
  1152. if (Math.cos(armAngle * 2) < 0) {
  1153. ctx.textAlign = 'center';
  1154. ctx.textBaseline = 'top';
  1155. text.y += textMargin;
  1156. }
  1157. else if (Math.sin(armAngle * 2) > 0){
  1158. ctx.textAlign = 'right';
  1159. ctx.textBaseline = 'middle';
  1160. }
  1161. else {
  1162. ctx.textAlign = 'left';
  1163. ctx.textBaseline = 'middle';
  1164. }
  1165. ctx.fillStyle = this.colorAxis;
  1166. ctx.fillText(' ' + step.getCurrent() + ' ', text.x, text.y);
  1167. step.next();
  1168. }
  1169. // draw z-grid lines and axis
  1170. ctx.lineWidth = 1;
  1171. prettyStep = (this.defaultZStep === undefined);
  1172. step = new StepNumber(this.zMin, this.zMax, this.zStep, prettyStep);
  1173. step.start();
  1174. if (step.getCurrent() < this.zMin) {
  1175. step.next();
  1176. }
  1177. xText = (Math.cos(armAngle ) > 0) ? this.xMin : this.xMax;
  1178. yText = (Math.sin(armAngle ) < 0) ? this.yMin : this.yMax;
  1179. while (!step.end()) {
  1180. // TODO: make z-grid lines really 3d?
  1181. from = this._convert3Dto2D(new Point3d(xText, yText, step.getCurrent()));
  1182. ctx.strokeStyle = this.colorAxis;
  1183. ctx.beginPath();
  1184. ctx.moveTo(from.x, from.y);
  1185. ctx.lineTo(from.x - textMargin, from.y);
  1186. ctx.stroke();
  1187. ctx.textAlign = 'right';
  1188. ctx.textBaseline = 'middle';
  1189. ctx.fillStyle = this.colorAxis;
  1190. ctx.fillText(step.getCurrent() + ' ', from.x - 5, from.y);
  1191. step.next();
  1192. }
  1193. ctx.lineWidth = 1;
  1194. from = this._convert3Dto2D(new Point3d(xText, yText, this.zMin));
  1195. to = this._convert3Dto2D(new Point3d(xText, yText, this.zMax));
  1196. ctx.strokeStyle = this.colorAxis;
  1197. ctx.beginPath();
  1198. ctx.moveTo(from.x, from.y);
  1199. ctx.lineTo(to.x, to.y);
  1200. ctx.stroke();
  1201. // draw x-axis
  1202. ctx.lineWidth = 1;
  1203. // line at yMin
  1204. xMin2d = this._convert3Dto2D(new Point3d(this.xMin, this.yMin, this.zMin));
  1205. xMax2d = this._convert3Dto2D(new Point3d(this.xMax, this.yMin, this.zMin));
  1206. ctx.strokeStyle = this.colorAxis;
  1207. ctx.beginPath();
  1208. ctx.moveTo(xMin2d.x, xMin2d.y);
  1209. ctx.lineTo(xMax2d.x, xMax2d.y);
  1210. ctx.stroke();
  1211. // line at ymax
  1212. xMin2d = this._convert3Dto2D(new Point3d(this.xMin, this.yMax, this.zMin));
  1213. xMax2d = this._convert3Dto2D(new Point3d(this.xMax, this.yMax, this.zMin));
  1214. ctx.strokeStyle = this.colorAxis;
  1215. ctx.beginPath();
  1216. ctx.moveTo(xMin2d.x, xMin2d.y);
  1217. ctx.lineTo(xMax2d.x, xMax2d.y);
  1218. ctx.stroke();
  1219. // draw y-axis
  1220. ctx.lineWidth = 1;
  1221. // line at xMin
  1222. from = this._convert3Dto2D(new Point3d(this.xMin, this.yMin, this.zMin));
  1223. to = this._convert3Dto2D(new Point3d(this.xMin, this.yMax, this.zMin));
  1224. ctx.strokeStyle = this.colorAxis;
  1225. ctx.beginPath();
  1226. ctx.moveTo(from.x, from.y);
  1227. ctx.lineTo(to.x, to.y);
  1228. ctx.stroke();
  1229. // line at xMax
  1230. from = this._convert3Dto2D(new Point3d(this.xMax, this.yMin, this.zMin));
  1231. to = this._convert3Dto2D(new Point3d(this.xMax, this.yMax, this.zMin));
  1232. ctx.strokeStyle = this.colorAxis;
  1233. ctx.beginPath();
  1234. ctx.moveTo(from.x, from.y);
  1235. ctx.lineTo(to.x, to.y);
  1236. ctx.stroke();
  1237. // draw x-label
  1238. var xLabel = this.xLabel;
  1239. if (xLabel.length > 0) {
  1240. yOffset = 0.1 / this.scale.y;
  1241. xText = (this.xMin + this.xMax) / 2;
  1242. yText = (Math.cos(armAngle) > 0) ? this.yMin - yOffset: this.yMax + yOffset;
  1243. text = this._convert3Dto2D(new Point3d(xText, yText, this.zMin));
  1244. if (Math.cos(armAngle * 2) > 0) {
  1245. ctx.textAlign = 'center';
  1246. ctx.textBaseline = 'top';
  1247. }
  1248. else if (Math.sin(armAngle * 2) < 0){
  1249. ctx.textAlign = 'right';
  1250. ctx.textBaseline = 'middle';
  1251. }
  1252. else {
  1253. ctx.textAlign = 'left';
  1254. ctx.textBaseline = 'middle';
  1255. }
  1256. ctx.fillStyle = this.colorAxis;
  1257. ctx.fillText(xLabel, text.x, text.y);
  1258. }
  1259. // draw y-label
  1260. var yLabel = this.yLabel;
  1261. if (yLabel.length > 0) {
  1262. xOffset = 0.1 / this.scale.x;
  1263. xText = (Math.sin(armAngle ) > 0) ? this.xMin - xOffset : this.xMax + xOffset;
  1264. yText = (this.yMin + this.yMax) / 2;
  1265. text = this._convert3Dto2D(new Point3d(xText, yText, this.zMin));
  1266. if (Math.cos(armAngle * 2) < 0) {
  1267. ctx.textAlign = 'center';
  1268. ctx.textBaseline = 'top';
  1269. }
  1270. else if (Math.sin(armAngle * 2) > 0){
  1271. ctx.textAlign = 'right';
  1272. ctx.textBaseline = 'middle';
  1273. }
  1274. else {
  1275. ctx.textAlign = 'left';
  1276. ctx.textBaseline = 'middle';
  1277. }
  1278. ctx.fillStyle = this.colorAxis;
  1279. ctx.fillText(yLabel, text.x, text.y);
  1280. }
  1281. // draw z-label
  1282. var zLabel = this.zLabel;
  1283. if (zLabel.length > 0) {
  1284. offset = 30; // pixels. // TODO: relate to the max width of the values on the z axis?
  1285. xText = (Math.cos(armAngle ) > 0) ? this.xMin : this.xMax;
  1286. yText = (Math.sin(armAngle ) < 0) ? this.yMin : this.yMax;
  1287. zText = (this.zMin + this.zMax) / 2;
  1288. text = this._convert3Dto2D(new Point3d(xText, yText, zText));
  1289. ctx.textAlign = 'right';
  1290. ctx.textBaseline = 'middle';
  1291. ctx.fillStyle = this.colorAxis;
  1292. ctx.fillText(zLabel, text.x - offset, text.y);
  1293. }
  1294. };
  1295. /**
  1296. * Calculate the color based on the given value.
  1297. * @param {Number} H Hue, a value be between 0 and 360
  1298. * @param {Number} S Saturation, a value between 0 and 1
  1299. * @param {Number} V Value, a value between 0 and 1
  1300. */
  1301. Graph3d.prototype._hsv2rgb = function(H, S, V) {
  1302. var R, G, B, C, Hi, X;
  1303. C = V * S;
  1304. Hi = Math.floor(H/60); // hi = 0,1,2,3,4,5
  1305. X = C * (1 - Math.abs(((H/60) % 2) - 1));
  1306. switch (Hi) {
  1307. case 0: R = C; G = X; B = 0; break;
  1308. case 1: R = X; G = C; B = 0; break;
  1309. case 2: R = 0; G = C; B = X; break;
  1310. case 3: R = 0; G = X; B = C; break;
  1311. case 4: R = X; G = 0; B = C; break;
  1312. case 5: R = C; G = 0; B = X; break;
  1313. default: R = 0; G = 0; B = 0; break;
  1314. }
  1315. return 'RGB(' + parseInt(R*255) + ',' + parseInt(G*255) + ',' + parseInt(B*255) + ')';
  1316. };
  1317. /**
  1318. * Draw all datapoints as a grid
  1319. * This function can be used when the style is 'grid'
  1320. */
  1321. Graph3d.prototype._redrawDataGrid = function() {
  1322. var canvas = this.frame.canvas,
  1323. ctx = canvas.getContext('2d'),
  1324. point, right, top, cross,
  1325. i,
  1326. topSideVisible, fillStyle, strokeStyle, lineWidth,
  1327. h, s, v, zAvg;
  1328. if (this.dataPoints === undefined || this.dataPoints.length <= 0)
  1329. return; // TODO: throw exception?
  1330. // calculate the translations and screen position of all points
  1331. for (i = 0; i < this.dataPoints.length; i++) {
  1332. var trans = this._convertPointToTranslation(this.dataPoints[i].point);
  1333. var screen = this._convertTranslationToScreen(trans);
  1334. this.dataPoints[i].trans = trans;
  1335. this.dataPoints[i].screen = screen;
  1336. // calculate the translation of the point at the bottom (needed for sorting)
  1337. var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom);
  1338. this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z;
  1339. }
  1340. // sort the points on depth of their (x,y) position (not on z)
  1341. var sortDepth = function (a, b) {
  1342. return b.dist - a.dist;
  1343. };
  1344. this.dataPoints.sort(sortDepth);
  1345. if (this.style === Graph3d.STYLE.SURFACE) {
  1346. for (i = 0; i < this.dataPoints.length; i++) {
  1347. point = this.dataPoints[i];
  1348. right = this.dataPoints[i].pointRight;
  1349. top = this.dataPoints[i].pointTop;
  1350. cross = this.dataPoints[i].pointCross;
  1351. if (point !== undefined && right !== undefined && top !== undefined && cross !== undefined) {
  1352. if (this.showGrayBottom || this.showShadow) {
  1353. // calculate the cross product of the two vectors from center
  1354. // to left and right, in order to know whether we are looking at the
  1355. // bottom or at the top side. We can also use the cross product
  1356. // for calculating light intensity
  1357. var aDiff = Point3d.subtract(cross.trans, point.trans);
  1358. var bDiff = Point3d.subtract(top.trans, right.trans);
  1359. var crossproduct = Point3d.crossProduct(aDiff, bDiff);
  1360. var len = crossproduct.length();
  1361. // FIXME: there is a bug with determining the surface side (shadow or colored)
  1362. topSideVisible = (crossproduct.z > 0);
  1363. }
  1364. else {
  1365. topSideVisible = true;
  1366. }
  1367. if (topSideVisible) {
  1368. // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0
  1369. zAvg = (point.point.z + right.point.z + top.point.z + cross.point.z) / 4;
  1370. h = (1 - (zAvg - this.zMin) * this.scale.z / this.verticalRatio) * 240;
  1371. s = 1; // saturation
  1372. if (this.showShadow) {
  1373. v = Math.min(1 + (crossproduct.x / len) / 2, 1); // value. TODO: scale
  1374. fillStyle = this._hsv2rgb(h, s, v);
  1375. strokeStyle = fillStyle;
  1376. }
  1377. else {
  1378. v = 1;
  1379. fillStyle = this._hsv2rgb(h, s, v);
  1380. strokeStyle = this.colorAxis;
  1381. }
  1382. }
  1383. else {
  1384. fillStyle = 'gray';
  1385. strokeStyle = this.colorAxis;
  1386. }
  1387. lineWidth = 0.5;
  1388. ctx.lineWidth = lineWidth;
  1389. ctx.fillStyle = fillStyle;
  1390. ctx.strokeStyle = strokeStyle;
  1391. ctx.beginPath();
  1392. ctx.moveTo(point.screen.x, point.screen.y);
  1393. ctx.lineTo(right.screen.x, right.screen.y);
  1394. ctx.lineTo(cross.screen.x, cross.screen.y);
  1395. ctx.lineTo(top.screen.x, top.screen.y);
  1396. ctx.closePath();
  1397. ctx.fill();
  1398. ctx.stroke();
  1399. }
  1400. }
  1401. }
  1402. else { // grid style
  1403. for (i = 0; i < this.dataPoints.length; i++) {
  1404. point = this.dataPoints[i];
  1405. right = this.dataPoints[i].pointRight;
  1406. top = this.dataPoints[i].pointTop;
  1407. if (point !== undefined) {
  1408. if (this.showPerspective) {
  1409. lineWidth = 2 / -point.trans.z;
  1410. }
  1411. else {
  1412. lineWidth = 2 * -(this.eye.z / this.camera.getArmLength());
  1413. }
  1414. }
  1415. if (point !== undefined && right !== undefined) {
  1416. // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0
  1417. zAvg = (point.point.z + right.point.z) / 2;
  1418. h = (1 - (zAvg - this.zMin) * this.scale.z / this.verticalRatio) * 240;
  1419. ctx.lineWidth = lineWidth;
  1420. ctx.strokeStyle = this._hsv2rgb(h, 1, 1);
  1421. ctx.beginPath();
  1422. ctx.moveTo(point.screen.x, point.screen.y);
  1423. ctx.lineTo(right.screen.x, right.screen.y);
  1424. ctx.stroke();
  1425. }
  1426. if (point !== undefined && top !== undefined) {
  1427. // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0
  1428. zAvg = (point.point.z + top.point.z) / 2;
  1429. h = (1 - (zAvg - this.zMin) * this.scale.z / this.verticalRatio) * 240;
  1430. ctx.lineWidth = lineWidth;
  1431. ctx.strokeStyle = this._hsv2rgb(h, 1, 1);
  1432. ctx.beginPath();
  1433. ctx.moveTo(point.screen.x, point.screen.y);
  1434. ctx.lineTo(top.screen.x, top.screen.y);
  1435. ctx.stroke();
  1436. }
  1437. }
  1438. }
  1439. };
  1440. /**
  1441. * Draw all datapoints as dots.
  1442. * This function can be used when the style is 'dot' or 'dot-line'
  1443. */
  1444. Graph3d.prototype._redrawDataDot = function() {
  1445. var canvas = this.frame.canvas;
  1446. var ctx = canvas.getContext('2d');
  1447. var i;
  1448. if (this.dataPoints === undefined || this.dataPoints.length <= 0)
  1449. return; // TODO: throw exception?
  1450. // calculate the translations of all points
  1451. for (i = 0; i < this.dataPoints.length; i++) {
  1452. var trans = this._convertPointToTranslation(this.dataPoints[i].point);
  1453. var screen = this._convertTranslationToScreen(trans);
  1454. this.dataPoints[i].trans = trans;
  1455. this.dataPoints[i].screen = screen;
  1456. // calculate the distance from the point at the bottom to the camera
  1457. var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom);
  1458. this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z;
  1459. }
  1460. // order the translated points by depth
  1461. var sortDepth = function (a, b) {
  1462. return b.dist - a.dist;
  1463. };
  1464. this.dataPoints.sort(sortDepth);
  1465. // draw the datapoints as colored circles
  1466. var dotSize = this.frame.clientWidth * 0.02; // px
  1467. for (i = 0; i < this.dataPoints.length; i++) {
  1468. var point = this.dataPoints[i];
  1469. if (this.style === Graph3d.STYLE.DOTLINE) {
  1470. // draw a vertical line from the bottom to the graph value
  1471. //var from = this._convert3Dto2D(new Point3d(point.point.x, point.point.y, this.zMin));
  1472. var from = this._convert3Dto2D(point.bottom);
  1473. ctx.lineWidth = 1;
  1474. ctx.strokeStyle = this.colorGrid;
  1475. ctx.beginPath();
  1476. ctx.moveTo(from.x, from.y);
  1477. ctx.lineTo(point.screen.x, point.screen.y);
  1478. ctx.stroke();
  1479. }
  1480. // calculate radius for the circle
  1481. var size;
  1482. if (this.style === Graph3d.STYLE.DOTSIZE) {
  1483. size = dotSize/2 + 2*dotSize * (point.point.value - this.valueMin) / (this.valueMax - this.valueMin);
  1484. }
  1485. else {
  1486. size = dotSize;
  1487. }
  1488. var radius;
  1489. if (this.showPerspective) {
  1490. radius = size / -point.trans.z;
  1491. }
  1492. else {
  1493. radius = size * -(this.eye.z / this.camera.getArmLength());
  1494. }
  1495. if (radius < 0) {
  1496. radius = 0;
  1497. }
  1498. var hue, color, borderColor;
  1499. if (this.style === Graph3d.STYLE.DOTCOLOR ) {
  1500. // calculate the color based on the value
  1501. hue = (1 - (point.point.value - this.valueMin) * this.scale.value) * 240;
  1502. color = this._hsv2rgb(hue, 1, 1);
  1503. borderColor = this._hsv2rgb(hue, 1, 0.8);
  1504. }
  1505. else if (this.style === Graph3d.STYLE.DOTSIZE) {
  1506. color = this.colorDot;
  1507. borderColor = this.colorDotBorder;
  1508. }
  1509. else {
  1510. // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0
  1511. hue = (1 - (point.point.z - this.zMin) * this.scale.z / this.verticalRatio) * 240;
  1512. color = this._hsv2rgb(hue, 1, 1);
  1513. borderColor = this._hsv2rgb(hue, 1, 0.8);
  1514. }
  1515. // draw the circle
  1516. ctx.lineWidth = 1.0;
  1517. ctx.strokeStyle = borderColor;
  1518. ctx.fillStyle = color;
  1519. ctx.beginPath();
  1520. ctx.arc(point.screen.x, point.screen.y, radius, 0, Math.PI*2, true);
  1521. ctx.fill();
  1522. ctx.stroke();
  1523. }
  1524. };
  1525. /**
  1526. * Draw all datapoints as bars.
  1527. * This function can be used when the style is 'bar', 'bar-color', or 'bar-size'
  1528. */
  1529. Graph3d.prototype._redrawDataBar = function() {
  1530. var canvas = this.frame.canvas;
  1531. var ctx = canvas.getContext('2d');
  1532. var i, j, surface, corners;
  1533. if (this.dataPoints === undefined || this.dataPoints.length <= 0)
  1534. return; // TODO: throw exception?
  1535. // calculate the translations of all points
  1536. for (i = 0; i < this.dataPoints.length; i++) {
  1537. var trans = this._convertPointToTranslation(this.dataPoints[i].point);
  1538. var screen = this._convertTranslationToScreen(trans);
  1539. this.dataPoints[i].trans = trans;
  1540. this.dataPoints[i].screen = screen;
  1541. // calculate the distance from the point at the bottom to the camera
  1542. var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom);
  1543. this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z;
  1544. }
  1545. // order the translated points by depth
  1546. var sortDepth = function (a, b) {
  1547. return b.dist - a.dist;
  1548. };
  1549. this.dataPoints.sort(sortDepth);
  1550. // draw the datapoints as bars
  1551. var xWidth = this.xBarWidth / 2;
  1552. var yWidth = this.yBarWidth / 2;
  1553. for (i = 0; i < this.dataPoints.length; i++) {
  1554. var point = this.dataPoints[i];
  1555. // determine color
  1556. var hue, color, borderColor;
  1557. if (this.style === Graph3d.STYLE.BARCOLOR ) {
  1558. // calculate the color based on the value
  1559. hue = (1 - (point.point.value - this.valueMin) * this.scale.value) * 240;
  1560. color = this._hsv2rgb(hue, 1, 1);
  1561. borderColor = this._hsv2rgb(hue, 1, 0.8);
  1562. }
  1563. else if (this.style === Graph3d.STYLE.BARSIZE) {
  1564. color = this.colorDot;
  1565. borderColor = this.colorDotBorder;
  1566. }
  1567. else {
  1568. // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0
  1569. hue = (1 - (point.point.z - this.zMin) * this.scale.z / this.verticalRatio) * 240;
  1570. color = this._hsv2rgb(hue, 1, 1);
  1571. borderColor = this._hsv2rgb(hue, 1, 0.8);
  1572. }
  1573. // calculate size for the bar
  1574. if (this.style === Graph3d.STYLE.BARSIZE) {
  1575. xWidth = (this.xBarWidth / 2) * ((point.point.value - this.valueMin) / (this.valueMax - this.valueMin) * 0.8 + 0.2);
  1576. yWidth = (this.yBarWidth / 2) * ((point.point.value - this.valueMin) / (this.valueMax - this.valueMin) * 0.8 + 0.2);
  1577. }
  1578. // calculate all corner points
  1579. var me = this;
  1580. var point3d = point.point;
  1581. var top = [
  1582. {point: new Point3d(point3d.x - xWidth, point3d.y - yWidth, point3d.z)},
  1583. {point: new Point3d(point3d.x + xWidth, point3d.y - yWidth, point3d.z)},
  1584. {point: new Point3d(point3d.x + xWidth, point3d.y + yWidth, point3d.z)},
  1585. {point: new Point3d(point3d.x - xWidth, point3d.y + yWidth, point3d.z)}
  1586. ];
  1587. var bottom = [
  1588. {point: new Point3d(point3d.x - xWidth, point3d.y - yWidth, this.zMin)},
  1589. {point: new Point3d(point3d.x + xWidth, point3d.y - yWidth, this.zMin)},
  1590. {point: new Point3d(point3d.x + xWidth, point3d.y + yWidth, this.zMin)},
  1591. {point: new Point3d(point3d.x - xWidth, point3d.y + yWidth, this.zMin)}
  1592. ];
  1593. // calculate screen location of the points
  1594. top.forEach(function (obj) {
  1595. obj.screen = me._convert3Dto2D(obj.point);
  1596. });
  1597. bottom.forEach(function (obj) {
  1598. obj.screen = me._convert3Dto2D(obj.point);
  1599. });
  1600. // create five sides, calculate both corner points and center points
  1601. var surfaces = [
  1602. {corners: top, center: Point3d.avg(bottom[0].point, bottom[2].point)},
  1603. {corners: [top[0], top[1], bottom[1], bottom[0]], center: Point3d.avg(bottom[1].point, bottom[0].point)},
  1604. {corners: [top[1], top[2], bottom[2], bottom[1]], center: Point3d.avg(bottom[2].point, bottom[1].point)},
  1605. {corners: [top[2], top[3], bottom[3], bottom[2]], center: Point3d.avg(bottom[3].point, bottom[2].point)},
  1606. {corners: [top[3], top[0], bottom[0], bottom[3]], center: Point3d.avg(bottom[0].point, bottom[3].point)}
  1607. ];
  1608. point.surfaces = surfaces;
  1609. // calculate the distance of each of the surface centers to the camera
  1610. for (j = 0; j < surfaces.length; j++) {
  1611. surface = surfaces[j];
  1612. var transCenter = this._convertPointToTranslation(surface.center);
  1613. surface.dist = this.showPerspective ? transCenter.length() : -transCenter.z;
  1614. // TODO: this dept calculation doesn't work 100% of the cases due to perspective,
  1615. // but the current solution is fast/simple and works in 99.9% of all cases
  1616. // the issue is visible in example 14, with graph.setCameraPosition({horizontal: 2.97, vertical: 0.5, distance: 0.9})
  1617. }
  1618. // order the surfaces by their (translated) depth
  1619. surfaces.sort(function (a, b) {
  1620. var diff = b.dist - a.dist;
  1621. if (diff) return diff;
  1622. // if equal depth, sort the top surface last
  1623. if (a.corners === top) return 1;
  1624. if (b.corners === top) return -1;
  1625. // both are equal
  1626. return 0;
  1627. });
  1628. // draw the ordered surfaces
  1629. ctx.lineWidth = 1;
  1630. ctx.strokeStyle = borderColor;
  1631. ctx.fillStyle = color;
  1632. // NOTE: we start at j=2 instead of j=0 as we don't need to draw the two surfaces at the backside
  1633. for (j = 2; j < surfaces.length; j++) {
  1634. surface = surfaces[j];
  1635. corners = surface.corners;
  1636. ctx.beginPath();
  1637. ctx.moveTo(corners[3].screen.x, corners[3].screen.y);
  1638. ctx.lineTo(corners[0].screen.x, corners[0].screen.y);
  1639. ctx.lineTo(corners[1].screen.x, corners[1].screen.y);
  1640. ctx.lineTo(corners[2].screen.x, corners[2].screen.y);
  1641. ctx.lineTo(corners[3].screen.x, corners[3].screen.y);
  1642. ctx.fill();
  1643. ctx.stroke();
  1644. }
  1645. }
  1646. };
  1647. /**
  1648. * Draw a line through all datapoints.
  1649. * This function can be used when the style is 'line'
  1650. */
  1651. Graph3d.prototype._redrawDataLine = function() {
  1652. var canvas = this.frame.canvas,
  1653. ctx = canvas.getContext('2d'),
  1654. point, i;
  1655. if (this.dataPoints === undefined || this.dataPoints.length <= 0)
  1656. return; // TODO: throw exception?
  1657. // calculate the translations of all points
  1658. for (i = 0; i < this.dataPoints.length; i++) {
  1659. var trans = this._convertPointToTranslation(this.dataPoints[i].point);
  1660. var screen = this._convertTranslationToScreen(trans);
  1661. this.dataPoints[i].trans = trans;
  1662. this.dataPoints[i].screen = screen;
  1663. }
  1664. // start the line
  1665. if (this.dataPoints.length > 0) {
  1666. point = this.dataPoints[0];
  1667. ctx.lineWidth = 1; // TODO: make customizable
  1668. ctx.strokeStyle = 'blue'; // TODO: make customizable
  1669. ctx.beginPath();
  1670. ctx.moveTo(point.screen.x, point.screen.y);
  1671. }
  1672. // draw the datapoints as colored circles
  1673. for (i = 1; i < this.dataPoints.length; i++) {
  1674. point = this.dataPoints[i];
  1675. ctx.lineTo(point.screen.x, point.screen.y);
  1676. }
  1677. // finish the line
  1678. if (this.dataPoints.length > 0) {
  1679. ctx.stroke();
  1680. }
  1681. };
  1682. /**
  1683. * Start a moving operation inside the provided parent element
  1684. * @param {Event} event The event that occurred (required for
  1685. * retrieving the mouse position)
  1686. */
  1687. Graph3d.prototype._onMouseDown = function(event) {
  1688. event = event || window.event;
  1689. // check if mouse is still down (may be up when focus is lost for example
  1690. // in an iframe)
  1691. if (this.leftButtonDown) {
  1692. this._onMouseUp(event);
  1693. }
  1694. // only react on left mouse button down
  1695. this.leftButtonDown = event.which ? (event.which === 1) : (event.button === 1);
  1696. if (!this.leftButtonDown && !this.touchDown) return;
  1697. // get mouse position (different code for IE and all other browsers)
  1698. this.startMouseX = getMouseX(event);
  1699. this.startMouseY = getMouseY(event);
  1700. this.startStart = new Date(this.start);
  1701. this.startEnd = new Date(this.end);
  1702. this.startArmRotation = this.camera.getArmRotation();
  1703. this.frame.style.cursor = 'move';
  1704. // add event listeners to handle moving the contents
  1705. // we store the function onmousemove and onmouseup in the graph, so we can
  1706. // remove the eventlisteners lateron in the function mouseUp()
  1707. var me = this;
  1708. this.onmousemove = function (event) {me._onMouseMove(event);};
  1709. this.onmouseup = function (event) {me._onMouseUp(event);};
  1710. G3DaddEventListener(document, 'mousemove', me.onmousemove);
  1711. G3DaddEventListener(document, 'mouseup', me.onmouseup);
  1712. G3DpreventDefault(event);
  1713. };
  1714. /**
  1715. * Perform moving operating.
  1716. * This function activated from within the funcion Graph.mouseDown().
  1717. * @param {Event} event Well, eehh, the event
  1718. */
  1719. Graph3d.prototype._onMouseMove = function (event) {
  1720. event = event || window.event;
  1721. // calculate change in mouse position
  1722. var diffX = parseFloat(getMouseX(event)) - this.startMouseX;
  1723. var diffY = parseFloat(getMouseY(event)) - this.startMouseY;
  1724. var horizontalNew = this.startArmRotation.horizontal + diffX / 200;
  1725. var verticalNew = this.startArmRotation.vertical + diffY / 200;
  1726. var snapAngle = 4; // degrees
  1727. var snapValue = Math.sin(snapAngle / 360 * 2 * Math.PI);
  1728. // snap horizontally to nice angles at 0pi, 0.5pi, 1pi, 1.5pi, etc...
  1729. // the -0.001 is to take care that the vertical axis is always drawn at the left front corner
  1730. if (Math.abs(Math.sin(horizontalNew)) < snapValue) {
  1731. horizontalNew = Math.round((horizontalNew / Math.PI)) * Math.PI - 0.001;
  1732. }
  1733. if (Math.abs(Math.cos(horizontalNew)) < snapValue) {
  1734. horizontalNew = (Math.round((horizontalNew/ Math.PI - 0.5)) + 0.5) * Math.PI - 0.001;
  1735. }
  1736. // snap vertically to nice angles
  1737. if (Math.abs(Math.sin(verticalNew)) < snapValue) {
  1738. verticalNew = Math.round((verticalNew / Math.PI)) * Math.PI;
  1739. }
  1740. if (Math.abs(Math.cos(verticalNew)) < snapValue) {
  1741. verticalNew = (Math.round((verticalNew/ Math.PI - 0.5)) + 0.5) * Math.PI;
  1742. }
  1743. this.camera.setArmRotation(horizontalNew, verticalNew);
  1744. this.redraw();
  1745. // fire a cameraPositionChange event
  1746. var parameters = this.getCameraPosition();
  1747. this.emit('cameraPositionChange', parameters);
  1748. G3DpreventDefault(event);
  1749. };
  1750. /**
  1751. * Stop moving operating.
  1752. * This function activated from within the funcion Graph.mouseDown().
  1753. * @param {event} event The event
  1754. */
  1755. Graph3d.prototype._onMouseUp = function (event) {
  1756. this.frame.style.cursor = 'auto';
  1757. this.leftButtonDown = false;
  1758. // remove event listeners here
  1759. G3DremoveEventListener(document, 'mousemove', this.onmousemove);
  1760. G3DremoveEventListener(document, 'mouseup', this.onmouseup);
  1761. G3DpreventDefault(event);
  1762. };
  1763. /**
  1764. * After having moved the mouse, a tooltip should pop up when the mouse is resting on a data point
  1765. * @param {Event} event A mouse move event
  1766. */
  1767. Graph3d.prototype._onTooltip = function (event) {
  1768. var delay = 300; // ms
  1769. var mouseX = getMouseX(event) - getAbsoluteLeft(this.frame);
  1770. var mouseY = getMouseY(event) - getAbsoluteTop(this.frame);
  1771. if (!this.showTooltip) {
  1772. return;
  1773. }
  1774. if (this.tooltipTimeout) {
  1775. clearTimeout(this.tooltipTimeout);
  1776. }
  1777. // (delayed) display of a tooltip only if no mouse button is down
  1778. if (this.leftButtonDown) {
  1779. this._hideTooltip();
  1780. return;
  1781. }
  1782. if (this.tooltip && this.tooltip.dataPoint) {
  1783. // tooltip is currently visible
  1784. var dataPoint = this._dataPointFromXY(mouseX, mouseY);
  1785. if (dataPoint !== this.tooltip.dataPoint) {
  1786. // datapoint changed
  1787. if (dataPoint) {
  1788. this._showTooltip(dataPoint);
  1789. }
  1790. else {
  1791. this._hideTooltip();
  1792. }
  1793. }
  1794. }
  1795. else {
  1796. // tooltip is currently not visible
  1797. var me = this;
  1798. this.tooltipTimeout = setTimeout(function () {
  1799. me.tooltipTimeout = null;
  1800. // show a tooltip if we have a data point
  1801. var dataPoint = me._dataPointFromXY(mouseX, mouseY);
  1802. if (dataPoint) {
  1803. me._showTooltip(dataPoint);
  1804. }
  1805. }, delay);
  1806. }
  1807. };
  1808. /**
  1809. * Event handler for touchstart event on mobile devices
  1810. */
  1811. Graph3d.prototype._onTouchStart = function(event) {
  1812. this.touchDown = true;
  1813. var me = this;
  1814. this.ontouchmove = function (event) {me._onTouchMove(event);};
  1815. this.ontouchend = function (event) {me._onTouchEnd(event);};
  1816. G3DaddEventListener(document, 'touchmove', me.ontouchmove);
  1817. G3DaddEventListener(document, 'touchend', me.ontouchend);
  1818. this._onMouseDown(event);
  1819. };
  1820. /**
  1821. * Event handler for touchmove event on mobile devices
  1822. */
  1823. Graph3d.prototype._onTouchMove = function(event) {
  1824. this._onMouseMove(event);
  1825. };
  1826. /**
  1827. * Event handler for touchend event on mobile devices
  1828. */
  1829. Graph3d.prototype._onTouchEnd = function(event) {
  1830. this.touchDown = false;
  1831. G3DremoveEventListener(document, 'touchmove', this.ontouchmove);
  1832. G3DremoveEventListener(document, 'touchend', this.ontouchend);
  1833. this._onMouseUp(event);
  1834. };
  1835. /**
  1836. * Event handler for mouse wheel event, used to zoom the graph
  1837. * Code from http://adomas.org/javascript-mouse-wheel/
  1838. * @param {event} event The event
  1839. */
  1840. Graph3d.prototype._onWheel = function(event) {
  1841. if (!event) /* For IE. */
  1842. event = window.event;
  1843. // retrieve delta
  1844. var delta = 0;
  1845. if (event.wheelDelta) { /* IE/Opera. */
  1846. delta = event.wheelDelta/120;
  1847. } else if (event.detail) { /* Mozilla case. */
  1848. // In Mozilla, sign of delta is different than in IE.
  1849. // Also, delta is multiple of 3.
  1850. delta = -event.detail/3;
  1851. }
  1852. // If delta is nonzero, handle it.
  1853. // Basically, delta is now positive if wheel was scrolled up,
  1854. // and negative, if wheel was scrolled down.
  1855. if (delta) {
  1856. var oldLength = this.camera.getArmLength();
  1857. var newLength = oldLength * (1 - delta / 10);
  1858. this.camera.setArmLength(newLength);
  1859. this.redraw();
  1860. this._hideTooltip();
  1861. }
  1862. // fire a cameraPositionChange event
  1863. var parameters = this.getCameraPosition();
  1864. this.emit('cameraPositionChange', parameters);
  1865. // Prevent default actions caused by mouse wheel.
  1866. // That might be ugly, but we handle scrolls somehow
  1867. // anyway, so don't bother here..
  1868. G3DpreventDefault(event);
  1869. };
  1870. /**
  1871. * Test whether a point lies inside given 2D triangle
  1872. * @param {Point2d} point
  1873. * @param {Point2d[]} triangle
  1874. * @return {boolean} Returns true if given point lies inside or on the edge of the triangle
  1875. * @private
  1876. */
  1877. Graph3d.prototype._insideTriangle = function (point, triangle) {
  1878. var a = triangle[0],
  1879. b = triangle[1],
  1880. c = triangle[2];
  1881. function sign (x) {
  1882. return x > 0 ? 1 : x < 0 ? -1 : 0;
  1883. }
  1884. var as = sign((b.x - a.x) * (point.y - a.y) - (b.y - a.y) * (point.x - a.x));
  1885. var bs = sign((c.x - b.x) * (point.y - b.y) - (c.y - b.y) * (point.x - b.x));
  1886. var cs = sign((a.x - c.x) * (point.y - c.y) - (a.y - c.y) * (point.x - c.x));
  1887. // each of the three signs must be either equal to each other or zero
  1888. return (as == 0 || bs == 0 || as == bs) &&
  1889. (bs == 0 || cs == 0 || bs == cs) &&
  1890. (as == 0 || cs == 0 || as == cs);
  1891. };
  1892. /**
  1893. * Find a data point close to given screen position (x, y)
  1894. * @param {Number} x
  1895. * @param {Number} y
  1896. * @return {Object | null} The closest data point or null if not close to any data point
  1897. * @private
  1898. */
  1899. Graph3d.prototype._dataPointFromXY = function (x, y) {
  1900. var i,
  1901. distMax = 100, // px
  1902. dataPoint = null,
  1903. closestDataPoint = null,
  1904. closestDist = null,
  1905. center = new Point2d(x, y);
  1906. if (this.style === Graph3d.STYLE.BAR ||
  1907. this.style === Graph3d.STYLE.BARCOLOR ||
  1908. this.style === Graph3d.STYLE.BARSIZE) {
  1909. // the data points are ordered from far away to closest
  1910. for (i = this.dataPoints.length - 1; i >= 0; i--) {
  1911. dataPoint = this.dataPoints[i];
  1912. var surfaces = dataPoint.surfaces;
  1913. if (surfaces) {
  1914. for (var s = surfaces.length - 1; s >= 0; s--) {
  1915. // split each surface in two triangles, and see if the center point is inside one of these
  1916. var surface = surfaces[s];
  1917. var corners = surface.corners;
  1918. var triangle1 = [corners[0].screen, corners[1].screen, corners[2].screen];
  1919. var triangle2 = [corners[2].screen, corners[3].screen, corners[0].screen];
  1920. if (this._insideTriangle(center, triangle1) ||
  1921. this._insideTriangle(center, triangle2)) {
  1922. // return immediately at the first hit
  1923. return dataPoint;
  1924. }
  1925. }
  1926. }
  1927. }
  1928. }
  1929. else {
  1930. // find the closest data point, using distance to the center of the point on 2d screen
  1931. for (i = 0; i < this.dataPoints.length; i++) {
  1932. dataPoint = this.dataPoints[i];
  1933. var point = dataPoint.screen;
  1934. if (point) {
  1935. var distX = Math.abs(x - point.x);
  1936. var distY = Math.abs(y - point.y);
  1937. var dist = Math.sqrt(distX * distX + distY * distY);
  1938. if ((closestDist === null || dist < closestDist) && dist < distMax) {
  1939. closestDist = dist;
  1940. closestDataPoint = dataPoint;
  1941. }
  1942. }
  1943. }
  1944. }
  1945. return closestDataPoint;
  1946. };
  1947. /**
  1948. * Display a tooltip for given data point
  1949. * @param {Object} dataPoint
  1950. * @private
  1951. */
  1952. Graph3d.prototype._showTooltip = function (dataPoint) {
  1953. var content, line, dot;
  1954. if (!this.tooltip) {
  1955. content = document.createElement('div');
  1956. content.style.position = 'absolute';
  1957. content.style.padding = '10px';
  1958. content.style.border = '1px solid #4d4d4d';
  1959. content.style.color = '#1a1a1a';
  1960. content.style.background = 'rgba(255,255,255,0.7)';
  1961. content.style.borderRadius = '2px';
  1962. content.style.boxShadow = '5px 5px 10px rgba(128,128,128,0.5)';
  1963. line = document.createElement('div');
  1964. line.style.position = 'absolute';
  1965. line.style.height = '40px';
  1966. line.style.width = '0';
  1967. line.style.borderLeft = '1px solid #4d4d4d';
  1968. dot = document.createElement('div');
  1969. dot.style.position = 'absolute';
  1970. dot.style.height = '0';
  1971. dot.style.width = '0';
  1972. dot.style.border = '5px solid #4d4d4d';
  1973. dot.style.borderRadius = '5px';
  1974. this.tooltip = {
  1975. dataPoint: null,
  1976. dom: {
  1977. content: content,
  1978. line: line,
  1979. dot: dot
  1980. }
  1981. };
  1982. }
  1983. else {
  1984. content = this.tooltip.dom.content;
  1985. line = this.tooltip.dom.line;
  1986. dot = this.tooltip.dom.dot;
  1987. }
  1988. this._hideTooltip();
  1989. this.tooltip.dataPoint = dataPoint;
  1990. if (typeof this.showTooltip === 'function') {
  1991. content.innerHTML = this.showTooltip(dataPoint.point);
  1992. }
  1993. else {
  1994. content.innerHTML = '<table>' +
  1995. '<tr><td>x:</td><td>' + dataPoint.point.x + '</td></tr>' +
  1996. '<tr><td>y:</td><td>' + dataPoint.point.y + '</td></tr>' +
  1997. '<tr><td>z:</td><td>' + dataPoint.point.z + '</td></tr>' +
  1998. '</table>';
  1999. }
  2000. content.style.left = '0';
  2001. content.style.top = '0';
  2002. this.frame.appendChild(content);
  2003. this.frame.appendChild(line);
  2004. this.frame.appendChild(dot);
  2005. // calculate sizes
  2006. var contentWidth = content.offsetWidth;
  2007. var contentHeight = content.offsetHeight;
  2008. var lineHeight = line.offsetHeight;
  2009. var dotWidth = dot.offsetWidth;
  2010. var dotHeight = dot.offsetHeight;
  2011. var left = dataPoint.screen.x - contentWidth / 2;
  2012. left = Math.min(Math.max(left, 10), this.frame.clientWidth - 10 - contentWidth);
  2013. line.style.left = dataPoint.screen.x + 'px';
  2014. line.style.top = (dataPoint.screen.y - lineHeight) + 'px';
  2015. content.style.left = left + 'px';
  2016. content.style.top = (dataPoint.screen.y - lineHeight - contentHeight) + 'px';
  2017. dot.style.left = (dataPoint.screen.x - dotWidth / 2) + 'px';
  2018. dot.style.top = (dataPoint.screen.y - dotHeight / 2) + 'px';
  2019. };
  2020. /**
  2021. * Hide the tooltip when displayed
  2022. * @private
  2023. */
  2024. Graph3d.prototype._hideTooltip = function () {
  2025. if (this.tooltip) {
  2026. this.tooltip.dataPoint = null;
  2027. for (var prop in this.tooltip.dom) {
  2028. if (this.tooltip.dom.hasOwnProperty(prop)) {
  2029. var elem = this.tooltip.dom[prop];
  2030. if (elem && elem.parentNode) {
  2031. elem.parentNode.removeChild(elem);
  2032. }
  2033. }
  2034. }
  2035. }
  2036. };
  2037. /**
  2038. * Add and event listener. Works for all browsers
  2039. * @param {Element} element An html element
  2040. * @param {string} action The action, for example 'click',
  2041. * without the prefix 'on'
  2042. * @param {function} listener The callback function to be executed
  2043. * @param {boolean} useCapture
  2044. */
  2045. G3DaddEventListener = function(element, action, listener, useCapture) {
  2046. if (element.addEventListener) {
  2047. if (useCapture === undefined)
  2048. useCapture = false;
  2049. if (action === 'mousewheel' && navigator.userAgent.indexOf('Firefox') >= 0) {
  2050. action = 'DOMMouseScroll'; // For Firefox
  2051. }
  2052. element.addEventListener(action, listener, useCapture);
  2053. } else {
  2054. element.attachEvent('on' + action, listener); // IE browsers
  2055. }
  2056. };
  2057. /**
  2058. * Remove an event listener from an element
  2059. * @param {Element} element An html dom element
  2060. * @param {string} action The name of the event, for example 'mousedown'
  2061. * @param {function} listener The listener function
  2062. * @param {boolean} useCapture
  2063. */
  2064. G3DremoveEventListener = function(element, action, listener, useCapture) {
  2065. if (element.removeEventListener) {
  2066. // non-IE browsers
  2067. if (useCapture === undefined)
  2068. useCapture = false;
  2069. if (action === 'mousewheel' && navigator.userAgent.indexOf('Firefox') >= 0) {
  2070. action = 'DOMMouseScroll'; // For Firefox
  2071. }
  2072. element.removeEventListener(action, listener, useCapture);
  2073. } else {
  2074. // IE browsers
  2075. element.detachEvent('on' + action, listener);
  2076. }
  2077. };
  2078. /**
  2079. * Stop event propagation
  2080. */
  2081. G3DstopPropagation = function(event) {
  2082. if (!event)
  2083. event = window.event;
  2084. if (event.stopPropagation) {
  2085. event.stopPropagation(); // non-IE browsers
  2086. }
  2087. else {
  2088. event.cancelBubble = true; // IE browsers
  2089. }
  2090. };
  2091. /**
  2092. * Cancels the event if it is cancelable, without stopping further propagation of the event.
  2093. */
  2094. G3DpreventDefault = function (event) {
  2095. if (!event)
  2096. event = window.event;
  2097. if (event.preventDefault) {
  2098. event.preventDefault(); // non-IE browsers
  2099. }
  2100. else {
  2101. event.returnValue = false; // IE browsers
  2102. }
  2103. };
  2104. /**
  2105. * @constructor Slider
  2106. *
  2107. * An html slider control with start/stop/prev/next buttons
  2108. * @param {Element} container The element where the slider will be created
  2109. * @param {Object} options Available options:
  2110. * {boolean} visible If true (default) the
  2111. * slider is visible.
  2112. */
  2113. function Slider(container, options) {
  2114. if (container === undefined) {
  2115. throw 'Error: No container element defined';
  2116. }
  2117. this.container = container;
  2118. this.visible = (options && options.visible != undefined) ? options.visible : true;
  2119. if (this.visible) {
  2120. this.frame = document.createElement('DIV');
  2121. //this.frame.style.backgroundColor = '#E5E5E5';
  2122. this.frame.style.width = '100%';
  2123. this.frame.style.position = 'relative';
  2124. this.container.appendChild(this.frame);
  2125. this.frame.prev = document.createElement('INPUT');
  2126. this.frame.prev.type = 'BUTTON';
  2127. this.frame.prev.value = 'Prev';
  2128. this.frame.appendChild(this.frame.prev);
  2129. this.frame.play = document.createElement('INPUT');
  2130. this.frame.play.type = 'BUTTON';
  2131. this.frame.play.value = 'Play';
  2132. this.frame.appendChild(this.frame.play);
  2133. this.frame.next = document.createElement('INPUT');
  2134. this.frame.next.type = 'BUTTON';
  2135. this.frame.next.value = 'Next';
  2136. this.frame.appendChild(this.frame.next);
  2137. this.frame.bar = document.createElement('INPUT');
  2138. this.frame.bar.type = 'BUTTON';
  2139. this.frame.bar.style.position = 'absolute';
  2140. this.frame.bar.style.border = '1px solid red';
  2141. this.frame.bar.style.width = '100px';
  2142. this.frame.bar.style.height = '6px';
  2143. this.frame.bar.style.borderRadius = '2px';
  2144. this.frame.bar.style.MozBorderRadius = '2px';
  2145. this.frame.bar.style.border = '1px solid #7F7F7F';
  2146. this.frame.bar.style.backgroundColor = '#E5E5E5';
  2147. this.frame.appendChild(this.frame.bar);
  2148. this.frame.slide = document.createElement('INPUT');
  2149. this.frame.slide.type = 'BUTTON';
  2150. this.frame.slide.style.margin = '0px';
  2151. this.frame.slide.value = ' ';
  2152. this.frame.slide.style.position = 'relative';
  2153. this.frame.slide.style.left = '-100px';
  2154. this.frame.appendChild(this.frame.slide);
  2155. // create events
  2156. var me = this;
  2157. this.frame.slide.onmousedown = function (event) {me._onMouseDown(event);};
  2158. this.frame.prev.onclick = function (event) {me.prev(event);};
  2159. this.frame.play.onclick = function (event) {me.togglePlay(event);};
  2160. this.frame.next.onclick = function (event) {me.next(event);};
  2161. }
  2162. this.onChangeCallback = undefined;
  2163. this.values = [];
  2164. this.index = undefined;
  2165. this.playTimeout = undefined;
  2166. this.playInterval = 1000; // milliseconds
  2167. this.playLoop = true;
  2168. }
  2169. /**
  2170. * Select the previous index
  2171. */
  2172. Slider.prototype.prev = function() {
  2173. var index = this.getIndex();
  2174. if (index > 0) {
  2175. index--;
  2176. this.setIndex(index);
  2177. }
  2178. };
  2179. /**
  2180. * Select the next index
  2181. */
  2182. Slider.prototype.next = function() {
  2183. var index = this.getIndex();
  2184. if (index < this.values.length - 1) {
  2185. index++;
  2186. this.setIndex(index);
  2187. }
  2188. };
  2189. /**
  2190. * Select the next index
  2191. */
  2192. Slider.prototype.playNext = function() {
  2193. var start = new Date();
  2194. var index = this.getIndex();
  2195. if (index < this.values.length - 1) {
  2196. index++;
  2197. this.setIndex(index);
  2198. }
  2199. else if (this.playLoop) {
  2200. // jump to the start
  2201. index = 0;
  2202. this.setIndex(index);
  2203. }
  2204. var end = new Date();
  2205. var diff = (end - start);
  2206. // calculate how much time it to to set the index and to execute the callback
  2207. // function.
  2208. var interval = Math.max(this.playInterval - diff, 0);
  2209. // document.title = diff // TODO: cleanup
  2210. var me = this;
  2211. this.playTimeout = setTimeout(function() {me.playNext();}, interval);
  2212. };
  2213. /**
  2214. * Toggle start or stop playing
  2215. */
  2216. Slider.prototype.togglePlay = function() {
  2217. if (this.playTimeout === undefined) {
  2218. this.play();
  2219. } else {
  2220. this.stop();
  2221. }
  2222. };
  2223. /**
  2224. * Start playing
  2225. */
  2226. Slider.prototype.play = function() {
  2227. // Test whether already playing
  2228. if (this.playTimeout) return;
  2229. this.playNext();
  2230. if (this.frame) {
  2231. this.frame.play.value = 'Stop';
  2232. }
  2233. };
  2234. /**
  2235. * Stop playing
  2236. */
  2237. Slider.prototype.stop = function() {
  2238. clearInterval(this.playTimeout);
  2239. this.playTimeout = undefined;
  2240. if (this.frame) {
  2241. this.frame.play.value = 'Play';
  2242. }
  2243. };
  2244. /**
  2245. * Set a callback function which will be triggered when the value of the
  2246. * slider bar has changed.
  2247. */
  2248. Slider.prototype.setOnChangeCallback = function(callback) {
  2249. this.onChangeCallback = callback;
  2250. };
  2251. /**
  2252. * Set the interval for playing the list
  2253. * @param {Number} interval The interval in milliseconds
  2254. */
  2255. Slider.prototype.setPlayInterval = function(interval) {
  2256. this.playInterval = interval;
  2257. };
  2258. /**
  2259. * Retrieve the current play interval
  2260. * @return {Number} interval The interval in milliseconds
  2261. */
  2262. Slider.prototype.getPlayInterval = function(interval) {
  2263. return this.playInterval;
  2264. };
  2265. /**
  2266. * Set looping on or off
  2267. * @pararm {boolean} doLoop If true, the slider will jump to the start when
  2268. * the end is passed, and will jump to the end
  2269. * when the start is passed.
  2270. */
  2271. Slider.prototype.setPlayLoop = function(doLoop) {
  2272. this.playLoop = doLoop;
  2273. };
  2274. /**
  2275. * Execute the onchange callback function
  2276. */
  2277. Slider.prototype.onChange = function() {
  2278. if (this.onChangeCallback !== undefined) {
  2279. this.onChangeCallback();
  2280. }
  2281. };
  2282. /**
  2283. * redraw the slider on the correct place
  2284. */
  2285. Slider.prototype.redraw = function() {
  2286. if (this.frame) {
  2287. // resize the bar
  2288. this.frame.bar.style.top = (this.frame.clientHeight/2 -
  2289. this.frame.bar.offsetHeight/2) + 'px';
  2290. this.frame.bar.style.width = (this.frame.clientWidth -
  2291. this.frame.prev.clientWidth -
  2292. this.frame.play.clientWidth -
  2293. this.frame.next.clientWidth - 30) + 'px';
  2294. // position the slider button
  2295. var left = this.indexToLeft(this.index);
  2296. this.frame.slide.style.left = (left) + 'px';
  2297. }
  2298. };
  2299. /**
  2300. * Set the list with values for the slider
  2301. * @param {Array} values A javascript array with values (any type)
  2302. */
  2303. Slider.prototype.setValues = function(values) {
  2304. this.values = values;
  2305. if (this.values.length > 0)
  2306. this.setIndex(0);
  2307. else
  2308. this.index = undefined;
  2309. };
  2310. /**
  2311. * Select a value by its index
  2312. * @param {Number} index
  2313. */
  2314. Slider.prototype.setIndex = function(index) {
  2315. if (index < this.values.length) {
  2316. this.index = index;
  2317. this.redraw();
  2318. this.onChange();
  2319. }
  2320. else {
  2321. throw 'Error: index out of range';
  2322. }
  2323. };
  2324. /**
  2325. * retrieve the index of the currently selected vaue
  2326. * @return {Number} index
  2327. */
  2328. Slider.prototype.getIndex = function() {
  2329. return this.index;
  2330. };
  2331. /**
  2332. * retrieve the currently selected value
  2333. * @return {*} value
  2334. */
  2335. Slider.prototype.get = function() {
  2336. return this.values[this.index];
  2337. };
  2338. Slider.prototype._onMouseDown = function(event) {
  2339. // only react on left mouse button down
  2340. var leftButtonDown = event.which ? (event.which === 1) : (event.button === 1);
  2341. if (!leftButtonDown) return;
  2342. this.startClientX = event.clientX;
  2343. this.startSlideX = parseFloat(this.frame.slide.style.left);
  2344. this.frame.style.cursor = 'move';
  2345. // add event listeners to handle moving the contents
  2346. // we store the function onmousemove and onmouseup in the graph, so we can
  2347. // remove the eventlisteners lateron in the function mouseUp()
  2348. var me = this;
  2349. this.onmousemove = function (event) {me._onMouseMove(event);};
  2350. this.onmouseup = function (event) {me._onMouseUp(event);};
  2351. G3DaddEventListener(document, 'mousemove', this.onmousemove);
  2352. G3DaddEventListener(document, 'mouseup', this.onmouseup);
  2353. G3DpreventDefault(event);
  2354. };
  2355. Slider.prototype.leftToIndex = function (left) {
  2356. var width = parseFloat(this.frame.bar.style.width) -
  2357. this.frame.slide.clientWidth - 10;
  2358. var x = left - 3;
  2359. var index = Math.round(x / width * (this.values.length-1));
  2360. if (index < 0) index = 0;
  2361. if (index > this.values.length-1) index = this.values.length-1;
  2362. return index;
  2363. };
  2364. Slider.prototype.indexToLeft = function (index) {
  2365. var width = parseFloat(this.frame.bar.style.width) -
  2366. this.frame.slide.clientWidth - 10;
  2367. var x = index / (this.values.length-1) * width;
  2368. var left = x + 3;
  2369. return left;
  2370. };
  2371. Slider.prototype._onMouseMove = function (event) {
  2372. var diff = event.clientX - this.startClientX;
  2373. var x = this.startSlideX + diff;
  2374. var index = this.leftToIndex(x);
  2375. this.setIndex(index);
  2376. G3DpreventDefault();
  2377. };
  2378. Slider.prototype._onMouseUp = function (event) {
  2379. this.frame.style.cursor = 'auto';
  2380. // remove event listeners
  2381. G3DremoveEventListener(document, 'mousemove', this.onmousemove);
  2382. G3DremoveEventListener(document, 'mouseup', this.onmouseup);
  2383. G3DpreventDefault();
  2384. };
  2385. /**--------------------------------------------------------------------------**/
  2386. /**
  2387. * Retrieve the absolute left value of a DOM element
  2388. * @param {Element} elem A dom element, for example a div
  2389. * @return {Number} left The absolute left position of this element
  2390. * in the browser page.
  2391. */
  2392. getAbsoluteLeft = function(elem) {
  2393. var left = 0;
  2394. while( elem !== null ) {
  2395. left += elem.offsetLeft;
  2396. left -= elem.scrollLeft;
  2397. elem = elem.offsetParent;
  2398. }
  2399. return left;
  2400. };
  2401. /**
  2402. * Retrieve the absolute top value of a DOM element
  2403. * @param {Element} elem A dom element, for example a div
  2404. * @return {Number} top The absolute top position of this element
  2405. * in the browser page.
  2406. */
  2407. getAbsoluteTop = function(elem) {
  2408. var top = 0;
  2409. while( elem !== null ) {
  2410. top += elem.offsetTop;
  2411. top -= elem.scrollTop;
  2412. elem = elem.offsetParent;
  2413. }
  2414. return top;
  2415. };
  2416. /**
  2417. * Get the horizontal mouse position from a mouse event
  2418. * @param {Event} event
  2419. * @return {Number} mouse x
  2420. */
  2421. getMouseX = function(event) {
  2422. if ('clientX' in event) return event.clientX;
  2423. return event.targetTouches[0] && event.targetTouches[0].clientX || 0;
  2424. };
  2425. /**
  2426. * Get the vertical mouse position from a mouse event
  2427. * @param {Event} event
  2428. * @return {Number} mouse y
  2429. */
  2430. getMouseY = function(event) {
  2431. if ('clientY' in event) return event.clientY;
  2432. return event.targetTouches[0] && event.targetTouches[0].clientY || 0;
  2433. };
  2434. module.exports = Graph3d;