UNPKG

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