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.

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