-
Notifications
You must be signed in to change notification settings - Fork 46
/
px-vis-event.html
495 lines (416 loc) · 13.7 KB
/
px-vis-event.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<!--
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.
-->
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="px-vis-behavior-common.html" />
<link rel="import" href="px-vis-behavior-d3.html" />
<link rel="import" href="px-vis-behavior-datetime.html"/>
<link rel="import" href="../px-icon-set/px-icon-set.html"/>
<!--
### Usage
<px-vis-event
svg="[[svg]]"
x-axis-type="[[xAxisType]]"
clip-path="[[clipPath]]"
event-id="[[item.id]]"
chart-data="[[item]]"
x="[[x]]"
y="[[y]]"
event-config="[[eventConfig]]">
</px-vis-event>
The configuration object allows developers to set the icon used for each event type.
It should follow this form:
eventConfig = {
"Event Name 1":{
"color": "blue",
"icon": "px-fea:deployments",
"type": "px",
"offset":[0,0]
},
"Event Name 2":{
"color": "green",
"icon": "px-nav:favorite",
"type": "px",
"offset":[0,0],
"lineWeight": 0
},
"Event Name 3":{
"icon": "Dancing_banana.gif",
"type": "image",
"offset":[-2,-20],
"size":"25"
}
}
### Styling
The following custom properties are available for styling:
Custom property | Description
:----------------|:-------------
`--px-vis-event-line-color` | The default color for the vertical lines below the icon
`--px-vis-event-icon-color` | The default color for the event icon markers
@element px-vis-event
@blurb d3 element which draws a line and icon on the chart representing an event.
@homepage index.html
@demo demo.html
-->
<dom-module id="px-vis-event">
<template>
</template>
</dom-module>
<script>
Polymer({
is: 'px-vis-event',
behaviors: [
PxVisBehavior.observerCheck,
PxVisBehaviorD3.svg,
PxVisBehaviorD3.axes,
PxVisBehavior.sizing,
PxVisBehavior.axisTypes,
PxVisBehaviorD3.clipPath,
PxVisBehavior.commonMethods,
PxVisBehavior.events,
PxVisBehaviorD3.domainUpdate,
PxVisBehaviorD3.icons,
PxVisBehavior.updateStylesOverride,
PxVisBehavior.scaleTypeCheck
],
/**
* Properties block, expose attribute values to the DOM via 'reflect'
*
* @property properties
* @type Object
*/
properties: {
/**
* Holder object for the event svg "g" element.
*
* @property eventGroup
* @type Object
*/
eventGroup:{
type:Object,
value: () => { return {}; }
},
/**
* Tracks if the event configuration has been edited.
*
*/
_eventConfigChanged: {
type: Boolean,
value: false
},
_eventDrawn: {
type: Boolean,
value: false
},
/**
* Default defaultEventConfig
*
*```
* Default: {
* 'color': 'rgb(0,0,0)',
* 'icon': 'px-utl:information',
* 'type': 'px',
* 'offset': 0,
* 'size': 16,
* 'lineColor': 'rgb(0,0,0)',
* 'lineWeight': 1,
* 'enableTooltip': true,
* 'firstDateTimeFormat': 'HH:mm:ss ZZ',
* 'secondDateTimeFormat': 'DD MMM YYYY',
* 'separator': '|',
* 'timezone': 'utc',
* 'tooltipOrientation': 'left'
* 'dataKey': 'time'
* }
*```
*
*/
_defaultDefaultEventConfig: {
type: Object,
value: () => {
return {
'color': 'rgb(0,0,0)',
'icon': 'px-utl:information',
'type': 'px',
'offset': [0,0],
'size': 16,
'lineColor': 'rgb(0,0,0)',
'lineWeight': 1,
'enableTooltip': true,
'firstDateTimeFormat': 'HH:mm:ss ZZ',
'secondDateTimeFormat': 'DD MMM YYYY',
'separator': '|',
'timezone': 'utc',
'tooltipOrientation': 'left',
'dataKey': 'time'
};
}
},
_iconInfo: {
type: Object,
value: () => { return {}; }
}
},
observers: [
'drawElement(svg, eventData.*, height, _stylesUpdated, domainChanged)',
'_eventConfigUpdated(eventConfig.*)',
'_eventConfigUpdated(defaultEventConfig.*)',
'_addClipPath(clipPath)'
],
detached: function() {
this._eventDrawn = false;
if(this.eventGroup && this._doesD3HaveValues(this.eventGroup)) {
this.eventGroup.remove();
this.eventGroup = null;
}
},
ready: function() {
window.requestAnimationFrame(function() {
this.updateStyles();
}.bind(this));
this._iconCallbackQueue = [this._forceIconRedraw];
},
_getConfigValue: function(key, eventType, theme) {
if(this.eventConfig && this.eventConfig[eventType] && this.eventConfig[eventType][key]) {
return this.eventConfig[eventType][key];
}
if(theme) {
return this._getDefaultValueOrTheme(key, theme);
}
return this._getDefaultValue(key);
},
_getDefaultValue: function(key) {
return this.defaultEventConfig && this.defaultEventConfig[key] ? this.defaultEventConfig[key] : this._defaultDefaultEventConfig[key];
},
_getDefaultValueOrTheme: function(key, theme) {
return this.defaultEventConfig && this.defaultEventConfig[key] ? this.defaultEventConfig[key] : this._checkThemeVariable(theme, this._defaultDefaultEventConfig[key]);
},
_eventConfigUpdated: function() {
if(this.hasUndefinedArguments(arguments)) {
return;
}
if(!this._eventDrawn) {
return;
}
this._forceIconRedraw();
},
_forceIconRedraw: function() {
this._eventConfigChanged = true;
this.drawElement();
},
drawElement: function() {
if(this.hasUndefinedArguments(arguments)) {
return;
}
if(this.drawDebounceTime > 0) {
this.debounce('_drawEvents', function() {
this._drawElementDebounced();
}, this.drawDebounceTime);
} else {
this._drawElementDebounced();
}
},
/**
* Draws or updates the event element.
* Called from an observer watching for data and the necessary d3 objects.
*
* @method drawElement
*/
_drawElementDebounced: function() {
if(!this.svg ||
!this.domainChanged) {
return;
}
let _this = this;
if(this._isD3Empty(this.eventGroup)) {
this.eventGroup = this.svg.append('g')
.attr('class', 'events')
.attr('pointer-events', 'all');
this._addClipPath();
}
this._eventBuilder = this.eventGroup.selectAll('g.event')
.data(this.eventData, function(d) { return d.id });
this._eventBuilder.exit().remove();
this._eventBuilder
.enter()
.append('g')
.attr('class', 'event')
.attr('event-id',function(d) { return d.id })
.each(function(d,i) {
_this._enterLine(d, i, this);
_this._enterIcon(d, i, this);
})
.merge(this._eventBuilder)
.each(function(d,i) {
// since the config controls the type of icon (image vs svg) we append, we cannot just rely on d3 updating. Have to remove and reappend the correct icon type
if(_this._eventConfigChanged) {
Px.d3.select(this).select('.event-icon').remove();
_this._enterIcon(d, i, this);
}
_this._mergeLine(d, i, this);
_this._mergeIcon(d, i, this);
});
this._eventConfigChanged = false;
this._eventDrawn = true;
this.dispatchEvent(new CustomEvent('px-vis-event-rendering-ended', { bubbles: true, composed: true}));
},
_enterLine: function(d, i, elem) {
Px.d3.select(elem).append("line")
.attr('class', 'event-line');
},
_enterIcon: function(d, i, elem) {
const iconType = this._getConfigValue('type', d.label, false);
let iconSvg;
if(iconType === 'image') {
iconSvg = Px.d3.select(elem).append("image")
.attr('class', 'event-icon')
.attr("xlink:href", this._getConfigValue('icon', d.label, false))
.attr("width", this._getConfigValue('size', d.label, false) + 'px')
.attr("height", this._getConfigValue('size', d.label, false) + 'px')
.style("cursor","pointer");
} else {
iconSvg = Px.d3.select(elem).append("g")
.attr('class', 'event-icon')
.style("cursor","pointer");
const iconName = this._getConfigValue('icon', d.label, false);
const size = this._getConfigValue('size', d.label, false);
if(!this._iconInfo[iconName]) {
this._iconInfo[iconName] = this._getPxIcon(iconName, size, 'px-utl', 'information');
if(!this._iconInfo[iconName]) {
// error messages and handling are in _getPxIcon
return;
}
}
const icon = iconSvg.node().appendChild(this._iconInfo[iconName].icon.cloneNode(true));
Px.d3.select(icon)
.attr('fill', 'transparent')
.attr('stroke', this._getConfigValue('color', d.label, "--px-vis-event-icon-color"))
.style("cursor","pointer");
}
iconSvg
.on('mouseenter', this._requestTooltip.bind(this,d,i,iconSvg.node()))
.on('mouseleave', this._clearTooltipRequest.bind(this,d,i));
},
_mergeLine: function(d, i, elem) {
const key = this._getConfigValue('dataKey', d.label, false);
if(!d[key]) {
return;
}
Px.d3.select(elem).select("line.event-line")
.attr('stroke', this._getConfigValue('lineColor', d.label, "--px-vis-event-line-color"))
.attr('stroke-width', this._getConfigValue('lineWeight', d.label, false))
.attr("x1", this.x(d[key]))
.attr("x2", this.x(d[key]))
.attr("y1", Math.max(this.height - this.margin.top, 0))
.attr("y2", 0);
},
_mergeIcon: function(d, i, elem) {
const key = this._getConfigValue('dataKey', d.label, false);
if(!d[key]) {
return;
}
const size = Number(this._getConfigValue('size', d.label, false));
const offset = this._getConfigValue('offset', d.label, false);
const type = this._getConfigValue('type', d.label, false);
const iconName = this._getConfigValue('icon', d.label, false);
const x = this.x(d[key]) - size/2 + offset[0];
const y0 = offset[1] - 5;
const y = type === "image" ? y0 : y0 - size;
const translate = `translate(${x},${y})`;
const scale = type === "image" ? '' : `scale(${this._iconInfo[iconName].scale})`;
Px.d3.select(elem).select('.event-icon')
.attr("transform", `${translate} ${scale}`);
},
/**
* Helper to call addClipPath with the element.
*
* @method _addClipPath
*/
_addClipPath: function() {
if(this.hasUndefinedArguments(arguments)) {
return;
}
this.addClipPath(this.eventGroup);
},
_requestTooltip: function(d,i, elem) {
if(!this._getConfigValue('enableTooltip', d.label, false)) {
return;
}
const detail = {
element: elem,
delay: 10,
tooltipType: this.is,
origin: this,
orientation: this._getConfigValue('tooltipOrientation', d.label, false),
data: [{
title: d.label,
data: this._getAllKeys(d)
}]
};
if(this._isTimeType(this.xAxisType)) {
detail.data[0].time = d[this._getConfigValue('dataKey', d.label, false)];
} else {
detail.data[0].data.push({
title: this._getConfigValue('dataKey', d.label, false),
value: d[this._getConfigValue('dataKey', d.label, false)],
type: 'number'
});
}
this.dispatchEvent(new CustomEvent('central-tooltip-display-request', { bubbles: true, composed: true, detail: detail }));
},
_getAllKeys: function(d) {
const arr = [];
Object.entries(d).forEach(([key,val]) => {
if(key !== this._getConfigValue('dataKey', d.label, false) && key !== 'label') {
arr.push({
title: key,
value: val
});
}
});
return arr;
},
_clearTooltipRequest: function(d,i) {
if(!this._getConfigValue('enableTooltip', d.label, false)) {
return;
}
this.dispatchEvent(new CustomEvent('central-tooltip-cancel-request', { bubbles: true, composed: true, detail: { origin: this } }));
},
/**
* Formats the datetime to match the custom format.
*
* @method _formatDatetime
*/
_formatDatetime: function(time,format) {
return this.formatTimestamp(time, this.timezone, format);
},
/**
* Returns the title value to the tooltip.
*
*/
_getValueTitle: function() {
return this._isTimeType(this.xAxisType) ? 'Timestamp' : 'X';
},
/**
* Returns the x value.
*
*/
_getValue: function(xAxisType) {
if(this.xAxisType === 'time' || this.xAxisType === 'timeLocal') {
return ': ' + this._formatDatetime(this.eventData[this.xKey], this.firstDateTimeFormat) +
' ' + this.separator + ' ' + this._formatDatetime(this.eventData[this.xKey], this.secondDateTimeFormat);
} else {
return ': ' + this.eventData[this.xKey];
}
}
});
</script>