-
Notifications
You must be signed in to change notification settings - Fork 46
/
px-vis-debugger.html
417 lines (327 loc) · 12.5 KB
/
px-vis-debugger.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script>
(function() {
if(typeof window.Px === 'undefined') {
window.Px = {};
}
window.Px.vis = window.Px.vis || {};
if(!window.Px.vis.debug) {
var debug = {},
isParaRadar,
chart,
util = {};
var initialize = function(chartToProcess) {
chart = chartToProcess;
isParaRadar = chart.nodeName === 'PX-VIS-PARALLEL-COORDINATES' || chart.nodeName === 'PX-VIS-RADAR';
}
/**********************************************************
* UTILITY
**********************************************************/
var _debugObjectHasKeys = function(obj, toFind) {
var result = {
hasKeys: false,
otherKeys: []
},
keyCount = 0,
keys;
if(obj) {
keys = Object.keys(obj);
for(var i=0; i<keys.length; i++) {
if(obj.hasOwnProperty(keys[i])) {
if(toFind.indexOf(keys[i]) === -1) {
otherKeys.push(keys[i]);
} else {
keyCount++;
}
}
}
if(keyCount === toFind.length) {
result.hasKeys = true;
}
}
return result;
};
var _debugGetTimeKey = function() {
var timeKey;
if(chart.timeData) {
timeKey = chart.timeData;
} else if(chart.seriesKey) {
timeKey = chart.seriesKey;
} else {
timeKey = chart.completeSeriesConfig[Object.keys(chart.completeSeriesConfig)[0]].x;
}
return timeKey;
};
var _debugGetSeriesKeys = function() {
if(isParaRadar) {
return chart.dimensions;
} else {
return Object.keys(chart.completeSeriesConfig);
}
};
var log = function(msg) {
console.warn(msg);
hasLogged = true;
};
var logTitle = function(msg) {
console.log('Checking ' + msg);
var separator = '=========';
for(var i=0; i< msg.length; i++) {
separator += '=';
}
console.log(separator);
hasLogged = false;
};
var logEnd = function() {
if(!hasLogged) {
console.log('No problem found');
console.log(' ');
}
};
var warn = function(msg) {
console.log(msg);
hasLogged = true;
};
var _getAllPropertiesKeys = function() {
var result = Object.keys(chart.properties);
for(var i=0; i<chart.behaviors.length; i++) {
if(chart.behaviors[i].properties) {
result = result.concat(Object.keys(chart.behaviors[i].properties));
}
}
return result;
};
/***********************************************************
* Check functions
* *********************************************************/
var _debugCheckConfigInfoParaRadar = function() {
var current,
keys;
logTitle('seriesConfig');
//check axes
for(var i=0; i<chart.dimensions.length; i++) {
current = chart.dimensions[i];
if(chart.seriesConfig[current]) {
keys = Object.keys(chart.seriesConfig[current]);
for(var j=0; j<keys.length; j++) {
if(keys[i] !== 'title' || keys[i] !== 'yAxisUnit') {
console.warn('Unrecognized key "' + keys[i] + '" for axis ' + current + ' in seriesConfig. Axis can only have "title" and/or "yAxisUnit". Please delete');
}
}
}
}
//check categories
if(chart.categories) {
if(!chart.categoryKey) {
log('categories are defined but categoryKey isn\'t. This is valid but categories won\'t be used.');
}
for(var i=0; i<chart.categories.length; i++) {
current = chart.categories[i];
if(chart.seriesConfig[current]) {
keys = Object.keys(chart.seriesConfig[current]);
for(var j=0; j<keys.length; j++) {
if(keys[i] !== 'color') {
warn('Unrecognized key "' + keys[i] + '" for category ' + current + ' in seriesConfig. Categories can only have "color". Please delete');
}
}
}
}
} else if(chart.categoryKey) {
if(!chart.categoryKey) {
log('categoryKey is defined but categories aren\'t. This is valid but categories won\'t be used.');
}
}
//check for series
var timeKey = _debugGetTimeKey();
if(chart.seriesConfig[timeKey]) {
for(var j=0; j<keys.length; j++) {
if(keys[i] !== 'color') {
warn('Unrecognized key "' + keys[i] + '" for the time based series ' + timeKey + ' in seriesConfig. Series can only have "color". Please delete');
}
}
}
logEnd();
};
var _debugCheckMargin = function() {
var valid = _debugObjectHasKeys(chart.margin, ['top', 'bottom', 'left', 'right']);
if(!valid.hasKeys) {
console.warn('`margin` property must be an object with "top", "bottom", "left" and "right" properties (numbers).')
}
if(valid.otherKeys.length) {
console.warn('`margin` property can\'t have other properties than "top", "bottom", "left" and "right". invalid properties: ' + JSON.stringify(valid.otherKeys));
}
};
var _debugCheckChartExtents = function() {
logTitle('chartExtents');
if(chart.chartExtents) {
var keys = Object.keys(chart.chartExtents),
current;
for(var i=0; i<keys.length; i++) {
current = chart.chartExtents[keys[i]];
if(!Array.isArray(current) || current.length !== 2) {
warn('chartExtents key ' + keys[i] + ' is not an array of 2 values: ' + JSON.stringify(current));
} else {
if(current[0] !== 'dynamic' && isNan(parseFloat(current[0])) || current[0] === Infinity || current[0] === -Infinity) {
warn('chartExtents key ' + keys[i] + ' should only have numbers, string representing a number or "dynamic". Current value: ' + JSON.stringify(current));
}
if(current[1] !== 'dynamic' && isNan(parseFloat(current[1])) || current[1] === Infinity || current[1] === -Infinity) {
warn('chartExtents key ' + keys[i] + ' should only have numbers, string representing a number or "dynamic". Current value: ' + JSON.stringify(current));
}
}
}
}
logEnd();
};
/***********************************************************
* Info functions
* *********************************************************/
var _debugAddDataInfo = function(info) {
var data = {},
extents = {};
info.hasData = !!chart.chartData;
if(info.hasData) {
var timeKey = _debugGetTimeKey(),
seriesKeys = _debugGetSeriesKeys(),
min = Number.MAX_VALUE,
max = Number.MIN_VALUE;
data.length = chart.chartData.length;
data.timeStart = chart.chartData[0][timeKey];
data.timeStartHuman = window.Px.moment(chart.chartData[0][timeKey]).format();
data.timeEnd = chart.chartData[chart.chartData.length - 1][timeKey];
data.timeEndHuman = window.Px.moment(chart.chartData[chart.chartData.length - 1][timeKey]).format();
for(var i=0; i<seriesKeys.length; i++) {
extents[seriesKeys[i]] = window.Px.d3.extent(chart.chartData, function(d) {
return d[seriesKeys[i]];
});
min = Math.min(extents[seriesKeys[i]][0], min);
max = Math.max(extents[seriesKeys[i]][1], max);
}
data.extents = extents;
data.min = min;
data.max = max;
}
info.data = data;
};
var _debugAddConfigInfo = function(info) {
info.config = {};
info.config.seriesConfig = chart.seriesConfig;
info.config.completeSeriesConfig = chart.completeSeriesConfig;
if(isParaRadar) {
info.config.dimension = chart.dimensions;
info.config.axes = chart.axes;
}
//TODO: check XY/TS/Polar conf?
};
var _debugAddMiscInfo = function(info) {
info.misc = {};
info.misc.margin = chart.margin;
};
var _debugAddExtentsInfo = function(info) {
info.extents = {};
info.extents.chartExtents = chart.chartExtents;
info.extents.dataExtents = chart.dataExtents;
if(typeof chart.y === 'function') {
info.extents.y = {};
info.extents.y.domain = chart.y.domain();
info.extents.y.range = chart.y.range();
} else {
var keys = Object.keys(chart.y);
info.extents.y = {};
for(var i=0; i<keys.length; i++) {
info.extents.y[keys[i]] = {};
info.extents.y[keys[i]].domain = chart.y[keys[i]].domain();
info.extents.y[keys[i]].range = chart.y[keys[i]].range();
}
}
info.extents.x = {};
info.extents.x.domain = chart.x.domain();
info.extents.x.range = chart.x.range();
};
var _debugAddSubCompInfo = function(info) {
info.subConfigs = {};
var keys = _getAllPropertiesKeys();
for(var i=0; i<keys.length; i++) {
if(/^[^_]*Config/.test(keys[i]) && keys[i] !== 'seriesConfig' && keys[i] !== 'completeSeriesConfig') {
info.subConfigs[keys[i]] = chart[keys[i]];
}
}
};
var _debugAddRendererInfo = function(info) {
info.render = {};
info.render.renderToCanvas = chart.renderToCanvas;
if(info.render.renderToCanvas) {
info.render.targets = chart._canvasTargets;
info.render.timings = chart._previousFramesTiming;
}
};
/***********************************************************
* Public functions
* *********************************************************/
debug.getInfo = function(chart, log) {
initialize(chart);
var info = {};
info.timeStamp = new Date();
info.chartType = chart.nodeName;
_debugAddDataInfo(info);
_debugAddConfigInfo(info);
_debugAddExtentsInfo(info);
_debugAddSubCompInfo(info);
_debugAddMiscInfo(info);
_debugAddRendererInfo(info);
if(log) {
//exclude renderer by default because it causes circular
//dependency for JSON
delete info.render;
//JSON doesn't support undefined value so return them as '_undefined_' instead
console.log(JSON.stringify(info, function(key, value) {
if(value === undefined) { return '_undefined_';} return value;
}, 2));
}
return info;
}
debug.checkConfig = function(chart) {
initialize(chart);
if(isParaRadar) {
_debugCheckConfigInfoParaRadar();
}
_debugCheckChartExtents();
_debugCheckMargin();
}
debug.info = function(msg, origin) {
if(debug.logLevel >= 3 && debug.log[origin]) {
origin = origin ? origin.toUpperCase() : origin;
console.log('[' + origin + '] ' + msg);
}
}
debug.log = function(msg, origin) {
if(debug.logLevel >= 2 && debug.log[origin]) {
origin = origin ? origin.toUpperCase() : origin;
console.log('[' + origin + '] ' + msg);
}
}
debug.warn = function(msg, origin) {
if(debug.logLevel >= 1 && debug.log[origin]) {
origin = origin ? origin.toUpperCase() : origin;
console.log('[' + origin + '] ' + msg);
}
}
debug.version = '5.1.12';
//1: warn, 2: log, 3: info
debug.logLevel = 0;
debug.log.renderer = false;
debug.log.scheduler = false;
window.Px.vis.debug = debug;
}
})();
</script>