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.

2331 lines
67 KiB

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