1 | var Emitter = require('emitter-component');
|
2 | var util = require('vis-util');
|
3 | var Point3d = require('./Point3d');
|
4 | var Point2d = require('./Point2d');
|
5 | var Slider = require('./Slider');
|
6 | var StepNumber = require('./StepNumber');
|
7 | var Settings = require('./Settings');
|
8 | var Validator = require("./../shared/Validator").Validator;
|
9 | var printStyle = require('./../shared/Validator').printStyle;
|
10 | var {allOptions} = require('./options.js');
|
11 | var DataGroup = require('./DataGroup');
|
12 |
|
13 |
|
14 |
|
15 | Graph3d.STYLE = Settings.STYLE;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var autoByDefault = undefined;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | Graph3d.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,
|
55 |
|
56 | dotSizeRatio : 0.02,
|
57 | dotSizeMinFraction: 0.5,
|
58 | dotSizeMaxFraction: 2.5,
|
59 |
|
60 | showAnimationControls: autoByDefault,
|
61 | animationInterval : 1000,
|
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
|
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 |
|
114 |
|
115 | showLegend : autoByDefault,
|
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 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function Graph3d(container, data, options) {
|
151 | if (!(this instanceof Graph3d)) {
|
152 | throw new SyntaxError('Constructor must be called with the new operator');
|
153 | }
|
154 |
|
155 |
|
156 | this.containerElement = container;
|
157 |
|
158 | this.dataGroup = new DataGroup();
|
159 | this.dataPoints = null;
|
160 |
|
161 |
|
162 | this.create();
|
163 |
|
164 | Settings.setDefaults(Graph3d.DEFAULTS, this);
|
165 |
|
166 |
|
167 | this.colX = undefined;
|
168 | this.colY = undefined;
|
169 | this.colZ = undefined;
|
170 | this.colValue = undefined;
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | this.setOptions(options);
|
176 |
|
177 |
|
178 | this.setData(data);
|
179 | }
|
180 |
|
181 |
|
182 | Emitter(Graph3d.prototype);
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | Graph3d.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 |
|
195 | if (this.keepAspectRatio) {
|
196 | if (this.scale.x < this.scale.y) {
|
197 |
|
198 | this.scale.y = this.scale.x;
|
199 | }
|
200 | else {
|
201 |
|
202 | this.scale.x = this.scale.y;
|
203 | }
|
204 | }
|
205 |
|
206 |
|
207 | this.scale.z *= this.verticalRatio;
|
208 |
|
209 |
|
210 |
|
211 | if (this.valueRange !== undefined) {
|
212 | this.scale.value = 1 / this.valueRange.range();
|
213 | }
|
214 |
|
215 |
|
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 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | Graph3d.prototype._convert3Dto2D = function(point3d) {
|
231 | var translation = this._convertPointToTranslation(point3d);
|
232 | return this._convertTranslationToScreen(translation);
|
233 | };
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | Graph3d.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 |
|
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 |
|
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 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | Graph3d.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 |
|
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 |
|
300 |
|
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 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | Graph3d.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 |
|
320 | var transBottom = this._convertPointToTranslation(point.bottom);
|
321 | point.dist = this.showPerspective ? transBottom.length() : -transBottom.z;
|
322 | }
|
323 |
|
324 |
|
325 | var sortDepth = function (a, b) {
|
326 | return b.dist - a.dist;
|
327 | };
|
328 | points.sort(sortDepth);
|
329 | };
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | Graph3d.prototype._initializeRanges = function() {
|
336 |
|
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 |
|
344 |
|
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 |
|
357 | this._setScale();
|
358 | };
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | Graph3d.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 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 | Graph3d.prototype._getDataPoints = function (data) {
|
402 |
|
403 |
|
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 |
|
411 |
|
412 |
|
413 |
|
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 |
|
420 | var dataMatrix = [];
|
421 | for (i = 0; i < dataPoints.length; i++) {
|
422 | obj = dataPoints[i];
|
423 |
|
424 |
|
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 |
|
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 {
|
450 | this._checkValueField(data);
|
451 | dataPoints = this.getDataPoints(data);
|
452 |
|
453 | if (this.style === Graph3d.STYLE.LINE) {
|
454 |
|
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 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 | Graph3d.prototype.create = function () {
|
475 |
|
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 |
|
485 | this.frame.canvas = document.createElement( 'canvas' );
|
486 | this.frame.canvas.style.position = 'relative';
|
487 | this.frame.appendChild(this.frame.canvas);
|
488 |
|
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 |
|
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 |
|
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 |
|
521 | this.containerElement.appendChild(this.frame);
|
522 | };
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 | Graph3d.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 |
|
542 |
|
543 | Graph3d.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 |
|
551 | this.frame.filter.style.width = (this.frame.canvas.clientWidth - 2 * 10) + 'px';
|
552 | };
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 | Graph3d.prototype.animationStart = function() {
|
560 |
|
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 |
|
572 |
|
573 | Graph3d.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 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 | Graph3d.prototype._resizeCenter = function() {
|
587 |
|
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);
|
595 | }
|
596 |
|
597 |
|
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);
|
605 | }
|
606 | };
|
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 | Graph3d.prototype.getCameraPosition = function() {
|
617 | var pos = this.camera.getArmRotation();
|
618 | pos.distance = this.camera.getArmLength();
|
619 | return pos;
|
620 | };
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 | Graph3d.prototype._readData = function(data) {
|
629 |
|
630 | this.dataPoints = this.dataGroup.initializeData(this, data, this.style);
|
631 |
|
632 | this._initializeRanges();
|
633 | this._redrawFilter();
|
634 | };
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 | Graph3d.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 |
|
651 |
|
652 |
|
653 |
|
654 | Graph3d.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 |
|
675 |
|
676 | Graph3d.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 |
|
721 |
|
722 | Graph3d.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 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 | Graph3d.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 |
|
759 |
|
760 | Graph3d.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 |
|
768 | Graph3d.prototype._dotSize = function() {
|
769 | return this.frame.clientWidth * this.dotSizeRatio;
|
770 | };
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 | Graph3d.prototype._getLegendWidth = function() {
|
780 | var width;
|
781 |
|
782 | if (this.style === Graph3d.STYLE.DOTSIZE) {
|
783 | var dotSize = this._dotSize();
|
784 |
|
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 |
|
797 |
|
798 | Graph3d.prototype._redrawLegend = function() {
|
799 |
|
800 |
|
801 | if (this.showLegend !== true) {
|
802 | return;
|
803 | }
|
804 |
|
805 |
|
806 | if (this.style === Graph3d.STYLE.LINE
|
807 | || this.style === Graph3d.STYLE.BARSIZE
|
808 | ){
|
809 | return;
|
810 | }
|
811 |
|
812 |
|
813 | var isSizeLegend = (this.style === Graph3d.STYLE.BARSIZE
|
814 | || this.style === Graph3d.STYLE.DOTSIZE) ;
|
815 |
|
816 |
|
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() ;
|
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';
|
831 |
|
832 | if (isSizeLegend === false) {
|
833 |
|
834 | var ymin = 0;
|
835 | var ymax = height;
|
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 |
|
855 | var widthMin;
|
856 | if (this.style === Graph3d.STYLE.DOTSIZE) {
|
857 |
|
858 | widthMin = width * (this.dotSizeMinFraction / this.dotSizeMaxFraction);
|
859 | } else if (this.style === Graph3d.STYLE.BARSIZE) {
|
860 |
|
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 |
|
875 | var gridLineLen = 5;
|
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 |
|
907 |
|
908 | Graph3d.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 |
|
925 | filter.style.padding = '10px';
|
926 |
|
927 |
|
928 | slider.setValues(dataFilter.values);
|
929 | slider.setPlayInterval(this.animationInterval);
|
930 |
|
931 |
|
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 |
|
949 |
|
950 | Graph3d.prototype._redrawSlider = function() {
|
951 | if ( this.frame.filter.slider !== undefined) {
|
952 | this.frame.filter.slider.redraw();
|
953 | }
|
954 | };
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 | Graph3d.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';
|
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 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 |
|
988 |
|
989 | Graph3d.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 |
|
1003 |
|
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 | Graph3d.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 |
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 |
|
1042 | Graph3d.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 |
|
1071 |
|
1072 |
|
1073 |
|
1074 |
|
1075 | Graph3d.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 |
|
1093 |
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 | Graph3d.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 |
|
1112 |
|
1113 | Graph3d.prototype._redrawAxis = function() {
|
1114 | var ctx = this._getContext(),
|
1115 | from, to, step, prettyStep,
|
1116 | text, xText, yText, zText,
|
1117 | offset, xOffset, yOffset;
|
1118 |
|
1119 |
|
1120 |
|
1121 | ctx.font = 24 / this.camera.getArmLength() + 'px arial';
|
1122 |
|
1123 |
|
1124 | var gridLenX = 0.025 / this.scale.x;
|
1125 | var gridLenY = 0.025 / this.scale.y;
|
1126 | var textMargin = 5 / this.camera.getArmLength();
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
1235 | if (this.showXAxis) {
|
1236 | var xMin2d;
|
1237 | var xMax2d;
|
1238 | ctx.lineWidth = 1;
|
1239 |
|
1240 |
|
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 |
|
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 |
|
1251 | if (this.showYAxis) {
|
1252 | ctx.lineWidth = 1;
|
1253 |
|
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 |
|
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 |
|
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 |
|
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 |
|
1285 | var zLabel = this.zLabel;
|
1286 | if (zLabel.length > 0 && this.showZAxis) {
|
1287 | offset = 30;
|
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 |
|
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 | Graph3d.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);
|
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 |
|
1330 |
|
1331 |
|
1332 |
|
1333 | Graph3d.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 |
|
1349 |
|
1350 |
|
1351 |
|
1352 |
|
1353 |
|
1354 |
|
1355 |
|
1356 |
|
1357 |
|
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 | Graph3d.prototype._redrawBar = function(ctx, point, xWidth, yWidth, color, borderColor) {
|
1364 | var surface;
|
1365 |
|
1366 |
|
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 |
|
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 |
|
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 |
|
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 |
|
1407 |
|
1408 |
|
1409 | }
|
1410 |
|
1411 |
|
1412 | surfaces.sort(function (a, b) {
|
1413 | var diff = b.dist - a.dist;
|
1414 | if (diff) return diff;
|
1415 |
|
1416 |
|
1417 | if (a.corners === top) return 1;
|
1418 | if (b.corners === top) return -1;
|
1419 |
|
1420 |
|
1421 | return 0;
|
1422 | });
|
1423 |
|
1424 |
|
1425 | ctx.lineWidth = this._getStrokeWidth(point);
|
1426 | ctx.strokeStyle = borderColor;
|
1427 | ctx.fillStyle = color;
|
1428 |
|
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 |
|
1438 |
|
1439 |
|
1440 |
|
1441 |
|
1442 |
|
1443 |
|
1444 | Graph3d.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();
|
1466 | };
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 |
|
1472 |
|
1473 |
|
1474 |
|
1475 |
|
1476 |
|
1477 | Graph3d.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 |
|
1492 |
|
1493 |
|
1494 |
|
1495 |
|
1496 |
|
1497 | Graph3d.prototype._getColorsRegular = function(point) {
|
1498 |
|
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 |
|
1512 |
|
1513 |
|
1514 |
|
1515 |
|
1516 |
|
1517 |
|
1518 |
|
1519 |
|
1520 |
|
1521 |
|
1522 | Graph3d.prototype._getColorsColor = function(point) {
|
1523 |
|
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 |
|
1544 |
|
1545 |
|
1546 |
|
1547 |
|
1548 |
|
1549 | Graph3d.prototype._getColorsSize = function() {
|
1550 | return {
|
1551 | fill : this.dataColor.fill,
|
1552 | border : this.dataColor.stroke
|
1553 | };
|
1554 | };
|
1555 |
|
1556 |
|
1557 |
|
1558 |
|
1559 |
|
1560 |
|
1561 |
|
1562 |
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 | Graph3d.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 |
|
1589 |
|
1590 |
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 |
|
1599 | Graph3d.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 |
|
1610 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 | Graph3d.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 |
|
1626 |
|
1627 |
|
1628 |
|
1629 |
|
1630 |
|
1631 | Graph3d.prototype._redrawBarSizeGraphPoint = function(ctx, point) {
|
1632 |
|
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 |
|
1645 |
|
1646 |
|
1647 |
|
1648 |
|
1649 |
|
1650 | Graph3d.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 |
|
1659 |
|
1660 |
|
1661 |
|
1662 |
|
1663 |
|
1664 | Graph3d.prototype._redrawDotLineGraphPoint = function(ctx, point) {
|
1665 |
|
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 |
|
1676 |
|
1677 |
|
1678 |
|
1679 |
|
1680 |
|
1681 | Graph3d.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 |
|
1690 |
|
1691 |
|
1692 |
|
1693 |
|
1694 |
|
1695 | Graph3d.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 |
|
1711 |
|
1712 |
|
1713 |
|
1714 |
|
1715 |
|
1716 | Graph3d.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 |
|
1731 |
|
1732 |
|
1733 |
|
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 |
|
1739 |
|
1740 | topSideVisible = (crossproduct.z > 0);
|
1741 | }
|
1742 |
|
1743 | if (topSideVisible) {
|
1744 |
|
1745 |
|
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;
|
1749 | var v;
|
1750 |
|
1751 | if (this.showShadow) {
|
1752 | v = Math.min(1 + (crossproduct.x / len) / 2, 1);
|
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;
|
1760 | }
|
1761 | }
|
1762 | else {
|
1763 | fillStyle = 'gray';
|
1764 | strokeStyle = this.axisColor;
|
1765 | }
|
1766 |
|
1767 | ctx.lineWidth = this._getStrokeWidth(point);
|
1768 |
|
1769 |
|
1770 | var points = [point, right, cross, top];
|
1771 | this._polygon(ctx, points, fillStyle, strokeStyle);
|
1772 | };
|
1773 |
|
1774 |
|
1775 |
|
1776 |
|
1777 |
|
1778 |
|
1779 |
|
1780 |
|
1781 |
|
1782 |
|
1783 | Graph3d.prototype._drawGridLine = function(ctx, from, to) {
|
1784 | if (from === undefined || to === undefined) {
|
1785 | return;
|
1786 | }
|
1787 |
|
1788 |
|
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 |
|
1800 |
|
1801 |
|
1802 |
|
1803 |
|
1804 |
|
1805 | Graph3d.prototype._redrawGridGraphPoint = function(ctx, point) {
|
1806 | this._drawGridLine(ctx, point, point.pointRight);
|
1807 | this._drawGridLine(ctx, point, point.pointTop);
|
1808 | };
|
1809 |
|
1810 |
|
1811 |
|
1812 |
|
1813 |
|
1814 |
|
1815 |
|
1816 |
|
1817 |
|
1818 | Graph3d.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 |
|
1832 |
|
1833 |
|
1834 | Graph3d.prototype._redrawDataGraph = function() {
|
1835 | var ctx = this._getContext();
|
1836 | var i;
|
1837 |
|
1838 | if (this.dataPoints === undefined || this.dataPoints.length <= 0)
|
1839 | return;
|
1840 |
|
1841 | this._calcTranslations(this.dataPoints);
|
1842 |
|
1843 | for (i = 0; i < this.dataPoints.length; i++) {
|
1844 | var point = this.dataPoints[i];
|
1845 |
|
1846 |
|
1847 | this._pointDrawingMethod.call(this, ctx, point);
|
1848 | }
|
1849 | };
|
1850 |
|
1851 |
|
1852 |
|
1853 |
|
1854 |
|
1855 |
|
1856 |
|
1857 |
|
1858 |
|
1859 |
|
1860 |
|
1861 | Graph3d.prototype._storeMousePosition = function(event) {
|
1862 |
|
1863 | this.startMouseX = getMouseX(event);
|
1864 | this.startMouseY = getMouseY(event);
|
1865 |
|
1866 | this._startCameraOffset = this.camera.getOffset();
|
1867 | };
|
1868 |
|
1869 |
|
1870 |
|
1871 |
|
1872 |
|
1873 |
|
1874 |
|
1875 | Graph3d.prototype._onMouseDown = function(event) {
|
1876 | event = event || window.event;
|
1877 |
|
1878 |
|
1879 |
|
1880 | if (this.leftButtonDown) {
|
1881 | this._onMouseUp(event);
|
1882 | }
|
1883 |
|
1884 |
|
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 |
|
1897 |
|
1898 |
|
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 |
|
1910 |
|
1911 |
|
1912 |
|
1913 | Graph3d.prototype._onMouseMove = function (event) {
|
1914 | this.moving = true;
|
1915 | event = event || window.event;
|
1916 |
|
1917 |
|
1918 | var diffX = parseFloat(getMouseX(event)) - this.startMouseX;
|
1919 | var diffY = parseFloat(getMouseY(event)) - this.startMouseY;
|
1920 |
|
1921 |
|
1922 | if (event && event.ctrlKey === true) {
|
1923 |
|
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;
|
1937 | var snapValue = Math.sin(snapAngle / 360 * 2 * Math.PI);
|
1938 |
|
1939 |
|
1940 |
|
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 |
|
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 |
|
1961 | var parameters = this.getCameraPosition();
|
1962 | this.emit('cameraPositionChange', parameters);
|
1963 |
|
1964 | util.preventDefault(event);
|
1965 | };
|
1966 |
|
1967 |
|
1968 |
|
1969 |
|
1970 |
|
1971 |
|
1972 |
|
1973 | Graph3d.prototype._onMouseUp = function (event) {
|
1974 | this.frame.style.cursor = 'auto';
|
1975 | this.leftButtonDown = false;
|
1976 |
|
1977 |
|
1978 | util.removeEventListener(document, 'mousemove', this.onmousemove);
|
1979 | util.removeEventListener(document, 'mouseup', this.onmouseup);
|
1980 | util.preventDefault(event);
|
1981 | };
|
1982 |
|
1983 |
|
1984 |
|
1985 |
|
1986 | Graph3d.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 {
|
1998 | this.moving = false;
|
1999 | }
|
2000 | util.preventDefault(event);
|
2001 | };
|
2002 |
|
2003 |
|
2004 |
|
2005 |
|
2006 |
|
2007 | Graph3d.prototype._onTooltip = function (event) {
|
2008 | var delay = 300;
|
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 |
|
2022 | if (this.leftButtonDown) {
|
2023 | this._hideTooltip();
|
2024 | return;
|
2025 | }
|
2026 |
|
2027 | if (this.tooltip && this.tooltip.dataPoint) {
|
2028 |
|
2029 | var dataPoint = this._dataPointFromXY(mouseX, mouseY);
|
2030 | if (dataPoint !== this.tooltip.dataPoint) {
|
2031 |
|
2032 | if (dataPoint) {
|
2033 | this._showTooltip(dataPoint);
|
2034 | }
|
2035 | else {
|
2036 | this._hideTooltip();
|
2037 | }
|
2038 | }
|
2039 | }
|
2040 | else {
|
2041 |
|
2042 | var me = this;
|
2043 | this.tooltipTimeout = setTimeout(function () {
|
2044 | me.tooltipTimeout = null;
|
2045 |
|
2046 |
|
2047 | var dataPoint = me._dataPointFromXY(mouseX, mouseY);
|
2048 | if (dataPoint) {
|
2049 | me._showTooltip(dataPoint);
|
2050 | }
|
2051 | }, delay);
|
2052 | }
|
2053 | };
|
2054 |
|
2055 |
|
2056 |
|
2057 |
|
2058 |
|
2059 | Graph3d.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 |
|
2073 |
|
2074 |
|
2075 | Graph3d.prototype._onTouchMove = function(event) {
|
2076 | this._onMouseMove(event);
|
2077 | };
|
2078 |
|
2079 |
|
2080 |
|
2081 |
|
2082 |
|
2083 | Graph3d.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 |
|
2095 |
|
2096 |
|
2097 |
|
2098 | Graph3d.prototype._onWheel = function(event) {
|
2099 | if (!event)
|
2100 | event = window.event;
|
2101 | if (this.zoomable && (!this.ctrlToZoom || event.ctrlKey)) {
|
2102 |
|
2103 |
|
2104 | var delta = 0;
|
2105 | if (event.wheelDelta) {
|
2106 | delta = event.wheelDelta/120;
|
2107 | } else if (event.detail) {
|
2108 |
|
2109 |
|
2110 | delta = -event.detail/3;
|
2111 | }
|
2112 |
|
2113 |
|
2114 |
|
2115 |
|
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 |
|
2127 | var parameters = this.getCameraPosition();
|
2128 | this.emit('cameraPositionChange', parameters);
|
2129 |
|
2130 |
|
2131 |
|
2132 |
|
2133 | util.preventDefault(event);
|
2134 | }
|
2135 | };
|
2136 |
|
2137 |
|
2138 |
|
2139 |
|
2140 |
|
2141 |
|
2142 |
|
2143 |
|
2144 |
|
2145 |
|
2146 | Graph3d.prototype._insideTriangle = function (point, triangle) {
|
2147 | var a = triangle[0],
|
2148 | b = triangle[1],
|
2149 | c = triangle[2];
|
2150 |
|
2151 | |
2152 |
|
2153 |
|
2154 |
|
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 |
|
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 |
|
2172 |
|
2173 |
|
2174 |
|
2175 |
|
2176 |
|
2177 |
|
2178 |
|
2179 | Graph3d.prototype._dataPointFromXY = function (x, y) {
|
2180 | var i,
|
2181 | distMax = 100,
|
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 |
|
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 |
|
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 |
|
2204 | return dataPoint;
|
2205 | }
|
2206 | }
|
2207 | }
|
2208 | }
|
2209 | }
|
2210 | else {
|
2211 |
|
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 |
|
2235 |
|
2236 |
|
2237 |
|
2238 |
|
2239 | Graph3d.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 |
|
2248 |
|
2249 |
|
2250 |
|
2251 | Graph3d.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 |
|
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 |
|
2322 |
|
2323 |
|
2324 | Graph3d.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 |
|
2344 |
|
2345 |
|
2346 |
|
2347 |
|
2348 | function getMouseX (event) {
|
2349 | if ('clientX' in event) return event.clientX;
|
2350 | return event.targetTouches[0] && event.targetTouches[0].clientX || 0;
|
2351 | }
|
2352 |
|
2353 |
|
2354 |
|
2355 |
|
2356 |
|
2357 |
|
2358 |
|
2359 | function 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 |
|
2367 |
|
2368 |
|
2369 |
|
2370 |
|
2371 |
|
2372 |
|
2373 |
|
2374 |
|
2375 |
|
2376 |
|
2377 |
|
2378 |
|
2379 |
|
2380 |
|
2381 |
|
2382 | Graph3d.prototype.setCameraPosition = function(pos) {
|
2383 | Settings.setCameraPosition(pos, this);
|
2384 | this.redraw();
|
2385 | };
|
2386 |
|
2387 |
|
2388 |
|
2389 |
|
2390 |
|
2391 |
|
2392 |
|
2393 |
|
2394 |
|
2395 |
|
2396 | Graph3d.prototype.setSize = function(width, height) {
|
2397 | this._setSize(width, height);
|
2398 | this.redraw();
|
2399 | };
|
2400 |
|
2401 |
|
2402 |
|
2403 |
|
2404 |
|
2405 |
|
2406 | module.exports = Graph3d;
|