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.

2312 lines
66 KiB

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