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.

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