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.

2276 lines
71 KiB

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