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.

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