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.

2213 lines
66 KiB

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