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.

2268 lines
70 KiB

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