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.

2578 lines
75 KiB

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