1 |
|
2 |
|
3 |
|
4 |
|
5 | var util = require('vis-util');
|
6 | var Camera = require('./Camera');
|
7 | var Point3d = require('./Point3d');
|
8 |
|
9 |
|
10 |
|
11 | var STYLE = {
|
12 | BAR : 0,
|
13 | BARCOLOR: 1,
|
14 | BARSIZE : 2,
|
15 | DOT : 3,
|
16 | DOTLINE : 4,
|
17 | DOTCOLOR: 5,
|
18 | DOTSIZE : 6,
|
19 | GRID : 7,
|
20 | LINE : 8,
|
21 | SURFACE : 9
|
22 | };
|
23 |
|
24 |
|
25 |
|
26 | var STYLENAME = {
|
27 | 'dot' : STYLE.DOT,
|
28 | 'dot-line' : STYLE.DOTLINE,
|
29 | 'dot-color': STYLE.DOTCOLOR,
|
30 | 'dot-size' : STYLE.DOTSIZE,
|
31 | 'line' : STYLE.LINE,
|
32 | 'grid' : STYLE.GRID,
|
33 | 'surface' : STYLE.SURFACE,
|
34 | 'bar' : STYLE.BAR,
|
35 | 'bar-color': STYLE.BARCOLOR,
|
36 | 'bar-size' : STYLE.BARSIZE
|
37 | };
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | var OPTIONKEYS = [
|
47 | 'width',
|
48 | 'height',
|
49 | 'filterLabel',
|
50 | 'legendLabel',
|
51 | 'xLabel',
|
52 | 'yLabel',
|
53 | 'zLabel',
|
54 | 'xValueLabel',
|
55 | 'yValueLabel',
|
56 | 'zValueLabel',
|
57 | 'showXAxis',
|
58 | 'showYAxis',
|
59 | 'showZAxis',
|
60 | 'showGrid',
|
61 | 'showPerspective',
|
62 | 'showShadow',
|
63 | 'keepAspectRatio',
|
64 | 'verticalRatio',
|
65 | 'dotSizeRatio',
|
66 | 'dotSizeMinFraction',
|
67 | 'dotSizeMaxFraction',
|
68 | 'showAnimationControls',
|
69 | 'animationInterval',
|
70 | 'animationPreload',
|
71 | 'animationAutoStart',
|
72 | 'axisColor',
|
73 | 'gridColor',
|
74 | 'xCenter',
|
75 | 'yCenter',
|
76 | 'zoomable',
|
77 | 'ctrlToZoom'
|
78 | ];
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | var PREFIXEDOPTIONKEYS = [
|
88 | 'xBarWidth',
|
89 | 'yBarWidth',
|
90 | 'valueMin',
|
91 | 'valueMax',
|
92 | 'xMin',
|
93 | 'xMax',
|
94 | 'xStep',
|
95 | 'yMin',
|
96 | 'yMax',
|
97 | 'yStep',
|
98 | 'zMin',
|
99 | 'zMax',
|
100 | 'zStep'
|
101 | ];
|
102 |
|
103 |
|
104 |
|
105 | var DEFAULTS = undefined;
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | function isEmpty(obj) {
|
117 | for(var prop in obj) {
|
118 | if (obj.hasOwnProperty(prop))
|
119 | return false;
|
120 | }
|
121 |
|
122 | return true;
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | function capitalize(str) {
|
135 | if (str === undefined || str === "" || typeof str != "string") {
|
136 | return str;
|
137 | }
|
138 |
|
139 | return str.charAt(0).toUpperCase() + str.slice(1);
|
140 | }
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function prefixFieldName(prefix, fieldName) {
|
151 | if (prefix === undefined || prefix === "") {
|
152 | return fieldName;
|
153 | }
|
154 |
|
155 | return prefix + capitalize(fieldName);
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | function forceCopy(src, dst, fields, prefix) {
|
176 | var srcKey;
|
177 | var dstKey;
|
178 |
|
179 | for (var i = 0; i < fields.length; ++i) {
|
180 | srcKey = fields[i];
|
181 | dstKey = prefixFieldName(prefix, srcKey);
|
182 |
|
183 | dst[dstKey] = src[srcKey];
|
184 | }
|
185 | }
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | function safeCopy(src, dst, fields, prefix) {
|
200 | var srcKey;
|
201 | var dstKey;
|
202 |
|
203 | for (var i = 0; i < fields.length; ++i) {
|
204 | srcKey = fields[i];
|
205 | if (src[srcKey] === undefined) continue;
|
206 |
|
207 | dstKey = prefixFieldName(prefix, srcKey);
|
208 |
|
209 | dst[dstKey] = src[srcKey];
|
210 | }
|
211 | }
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | function setDefaults(src, dst) {
|
226 | if (src === undefined || isEmpty(src)) {
|
227 | throw new Error('No DEFAULTS passed');
|
228 | }
|
229 | if (dst === undefined) {
|
230 | throw new Error('No dst passed');
|
231 | }
|
232 |
|
233 |
|
234 | DEFAULTS = src;
|
235 |
|
236 |
|
237 | forceCopy(src, dst, OPTIONKEYS);
|
238 | forceCopy(src, dst, PREFIXEDOPTIONKEYS, 'default');
|
239 |
|
240 |
|
241 | setSpecialSettings(src, dst);
|
242 |
|
243 |
|
244 | dst.margin = 10;
|
245 | dst.showGrayBottom = false;
|
246 | dst.showTooltip = false;
|
247 | dst.onclick_callback = null;
|
248 | dst.eye = new Point3d(0, 0, -1);
|
249 | }
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | function setOptions(options, dst) {
|
257 | if (options === undefined) {
|
258 | return;
|
259 | }
|
260 | if (dst === undefined) {
|
261 | throw new Error('No dst passed');
|
262 | }
|
263 |
|
264 | if (DEFAULTS === undefined || isEmpty(DEFAULTS)) {
|
265 | throw new Error('DEFAULTS not set for module Settings');
|
266 | }
|
267 |
|
268 |
|
269 | safeCopy(options, dst, OPTIONKEYS);
|
270 | safeCopy(options, dst, PREFIXEDOPTIONKEYS, 'default');
|
271 |
|
272 |
|
273 | setSpecialSettings(options, dst);
|
274 | }
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | function setSpecialSettings(src, dst) {
|
285 | if (src.backgroundColor !== undefined) {
|
286 | setBackgroundColor(src.backgroundColor, dst);
|
287 | }
|
288 |
|
289 | setDataColor(src.dataColor, dst);
|
290 | setStyle(src.style, dst);
|
291 | setShowLegend(src.showLegend, dst);
|
292 | setCameraPosition(src.cameraPosition, dst);
|
293 |
|
294 |
|
295 |
|
296 | if (src.tooltip !== undefined) {
|
297 | dst.showTooltip = src.tooltip;
|
298 | }
|
299 | if (src.onclick != undefined) {
|
300 | dst.onclick_callback = src.onclick;
|
301 | }
|
302 |
|
303 | if (src.tooltipStyle !== undefined) {
|
304 | util.selectiveDeepExtend(['tooltipStyle'], dst, src);
|
305 | }
|
306 | }
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | function setShowLegend(showLegend, dst) {
|
319 | if (showLegend === undefined) {
|
320 |
|
321 | var isAutoByDefault = (DEFAULTS.showLegend === undefined);
|
322 |
|
323 | if (isAutoByDefault) {
|
324 |
|
325 | var isLegendGraphStyle = dst.style === STYLE.DOTCOLOR
|
326 | || dst.style === STYLE.DOTSIZE;
|
327 |
|
328 | dst.showLegend = isLegendGraphStyle;
|
329 | } else {
|
330 |
|
331 | }
|
332 | } else {
|
333 | dst.showLegend = showLegend;
|
334 | }
|
335 | }
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 | function getStyleNumberByName(styleName) {
|
345 | var number = STYLENAME[styleName];
|
346 |
|
347 | if (number === undefined) {
|
348 | return -1;
|
349 | }
|
350 |
|
351 | return number;
|
352 | }
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | function checkStyleNumber(style) {
|
362 | var valid = false;
|
363 |
|
364 | for (var n in STYLE) {
|
365 | if (STYLE[n] === style) {
|
366 | valid = true;
|
367 | break;
|
368 | }
|
369 | }
|
370 |
|
371 | return valid;
|
372 | }
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 | function setStyle(style, dst) {
|
380 | if (style === undefined) {
|
381 | return;
|
382 | }
|
383 |
|
384 | var styleNumber;
|
385 |
|
386 | if (typeof style === 'string') {
|
387 | styleNumber = getStyleNumberByName(style);
|
388 |
|
389 | if (styleNumber === -1 ) {
|
390 | throw new Error('Style \'' + style + '\' is invalid');
|
391 | }
|
392 | } else {
|
393 |
|
394 | if (!checkStyleNumber(style)) {
|
395 | throw new Error('Style \'' + style + '\' is invalid');
|
396 | }
|
397 |
|
398 | styleNumber = style;
|
399 | }
|
400 |
|
401 | dst.style = styleNumber;
|
402 | }
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 | function setBackgroundColor(backgroundColor, dst) {
|
411 | var fill = 'white';
|
412 | var stroke = 'gray';
|
413 | var strokeWidth = 1;
|
414 |
|
415 | if (typeof(backgroundColor) === 'string') {
|
416 | fill = backgroundColor;
|
417 | stroke = 'none';
|
418 | strokeWidth = 0;
|
419 | }
|
420 | else if (typeof(backgroundColor) === 'object') {
|
421 | if (backgroundColor.fill !== undefined) fill = backgroundColor.fill;
|
422 | if (backgroundColor.stroke !== undefined) stroke = backgroundColor.stroke;
|
423 | if (backgroundColor.strokeWidth !== undefined) strokeWidth = backgroundColor.strokeWidth;
|
424 | }
|
425 | else {
|
426 | throw new Error('Unsupported type of backgroundColor');
|
427 | }
|
428 |
|
429 | dst.frame.style.backgroundColor = fill;
|
430 | dst.frame.style.borderColor = stroke;
|
431 | dst.frame.style.borderWidth = strokeWidth + 'px';
|
432 | dst.frame.style.borderStyle = 'solid';
|
433 | }
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 | function setDataColor(dataColor, dst) {
|
441 | if (dataColor === undefined) {
|
442 | return;
|
443 | }
|
444 |
|
445 | if (dst.dataColor === undefined) {
|
446 | dst.dataColor = {};
|
447 | }
|
448 |
|
449 | if (typeof dataColor === 'string') {
|
450 | dst.dataColor.fill = dataColor;
|
451 | dst.dataColor.stroke = dataColor;
|
452 | }
|
453 | else {
|
454 | if (dataColor.fill) {
|
455 | dst.dataColor.fill = dataColor.fill;
|
456 | }
|
457 | if (dataColor.stroke) {
|
458 | dst.dataColor.stroke = dataColor.stroke;
|
459 | }
|
460 | if (dataColor.strokeWidth !== undefined) {
|
461 | dst.dataColor.strokeWidth = dataColor.strokeWidth;
|
462 | }
|
463 | }
|
464 | }
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 | function setCameraPosition(cameraPosition, dst) {
|
472 | var camPos = cameraPosition;
|
473 | if (camPos === undefined) {
|
474 | return;
|
475 | }
|
476 |
|
477 | if (dst.camera === undefined) {
|
478 | dst.camera = new Camera();
|
479 | }
|
480 |
|
481 | dst.camera.setArmRotation(camPos.horizontal, camPos.vertical);
|
482 | dst.camera.setArmLength(camPos.distance);
|
483 | }
|
484 |
|
485 |
|
486 | module.exports.STYLE = STYLE;
|
487 | module.exports.setDefaults = setDefaults;
|
488 | module.exports.setOptions = setOptions;
|
489 | module.exports.setCameraPosition = setCameraPosition;
|