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.

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