forked from predixdesignsystem/px-vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx-vis-event.html
401 lines (363 loc) · 11.3 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
<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-colors-design/colors.html" />
<link rel="import" href="fa_codes.html" />
<link rel="import" href="../px-tooltip/px-tooltip.html"/>
<!--
d3 element which draws a line and icon on the chart representing an event.
##### 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]]"
current-domain-x="[[currentDomainX]]"
current-domain-y="[[currentDomainY]]">
</px-vis-event>
The configureation object allows developers to set the icon used for each event type. It comes in the form:
eventConfig = {
"Event Name 1":{
"color": "blue",
"icon": "fa-camera",
"type": "fa",
"offset":[0,0]
},
"Event Name 1":{
"color": "green",
"icon": "\uf015",
"type": "unicode",
"offset":[1,0]
},
"Event Name 3":{
"icon": "Dancing_banana.gif",
"type": "image",
"offset":[-2,-20],
"size":"25"
}
}
@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
-->
<link rel="import" href="css/px-vis-styles.html">
<dom-module id="px-vis-event">
<template>
<style include="px-vis-styles"></style>
<px-tooltip
id="eventTooltip"
smart-orientation
delay="10"
orientation="[[tooltipOrientation]]">
<span>
<b>Event</b>: [[eventData.label]] <br/>
<b>ID</b>: [[eventData.id]] <br/>
<b>[[_getValueTitle(xAxisType)]]</b>[[_getValue(xAxisType, eventData)]]<br/>
</span>
</px-tooltip>
</template>
</dom-module>
<script>
Polymer({
is: 'px-vis-event',
behaviors: [
PxVisBehaviorD3.svg,
PxVisBehaviorD3.axes,
PxVisBehavior.axisTypes,
PxVisBehaviorTime.datetime,
PxVisBehaviorD3.clipPath,
PxVisBehavior.commonMethods,
PxVisBehavior.events,
PxVisBehaviorD3.domainUpdate,
PxColorsBehavior.baseColors,
faCodes
],
/**
* Properties block, expose attribute values to the DOM via 'reflect'
*
* @property properties
* @type Object
*/
properties: {
/**
* The ID of the event. Either dev set or auto-generated
*
* @property eventId
* @type String
*/
eventId: {
type:String,
value: '',
notify: true,
reflectToAttribute: true
},
/**
* Holder object for the event svg "g" element
*
* @property eventGroup
* @type Object
*/
eventGroup:{
type:Object,
value: function() { return {}; }
},
/**
* Holder object for the event svg "line" element
*
* @property eventLine
* @type Object
*/
eventLine:{
type:Object,
value: function() { return {}; }
},
/**
* Holder object for the event svg icon
*
* @property eventIcon
* @type Object
*/
eventIcon:{
type:Object,
value: function() { return {}; }
},
/**
* Computed configuration object based on dev set styles and default styles
*
* @property _thisConfig
* @type Object
*/
_thisConfig:{
type:Object,
value: function() { return {}; }
},
/**
* Specifies the orientation of the event tooltip
*
* @property tooltipOrientation
* @type String
*/
tooltipOrientation: {
type: String,
value: 'left',
notify:true
},
/**
* Specifies the x key for the event data
*
*/
xKey: {
type: String,
value: 'x'
},
/**
* Tracks if the event configuration has been edited
*
*/
_eventConfigChanged: {
type: Boolean,
value: false
},
_animationFrameDone: {
type: Boolean,
value: false
}
},
observers: [
'_eventConfigHasChanged(eventConfig.*)',
'drawElement(svg, x, y, currentDomainX, currentDomainY, domainChanged, eventData.*,eventConfig.*, _animationFrameDone)',
'_addClipPath(clipPath)'
],
detached: function() {
if(this.eventGroup && this._doesD3HaveValues(this.eventGroup)) {
this.eventGroup.remove();
this.eventGroup = null;
this.eventLine = null;
this.eventIcon = null;
}
},
ready: function() {
window.requestAnimationFrame(this._calcThemeOrConfig.bind(this));
// if there is no dev set unique ID, generate one
if(!this.eventId) {
this.set('eventId', this.generateRandomID('event_'));
}
},
_calcThemeOrConfig: function() {
// if we have lineConfig, must be passed in so use it.
if(!this.lineConfig) {
var lineColor = this._checkThemeVariable("--px-vis-event-line-color", this.colors[this._lineConfigDefault.color]);
this.lineConfig = {
"color": lineColor,
"weight": this._lineConfigDefault.weight
};
}
if(!this.defaultEventConfig) {
var eventColor = this._checkThemeVariable("--px-vis-event-icon-color", this.colors[this._defaultDefaultEventConfig.color]);
this.defaultEventConfig = {
"color": eventColor,
'icon': this._defaultDefaultEventConfig.icon,
'type': this._defaultDefaultEventConfig.type,
'offset': this._defaultDefaultEventConfig.offset,
'size': this._defaultDefaultEventConfig.size
};
}
this.set('_animationFrameDone', true);
},
/**
* Draws or updates the event element.
* Called from an observer watching for data and the necessary d3 objects
*
* @method drawElement
*/
drawElement: function() {
if(this.x && this.y && this.currentDomainX.length > 0 && this.currentDomainY.length > 0 && this._doesObjHaveValues(this.eventData) && this._animationFrameDone) {
if(this._eventConfigChanged && this.eventGroup && this._doesD3HaveValues(this.eventGroup)) {
this.eventGroup.remove();
this.eventGroup = null;
}
this._processConfiguration();
// checks to see if the axis already exists. If not, create; if so, update
if(this._isObjEmpty(this.eventGroup)) {
this.eventGroup = this.svg.append('g')
.attr('class','event')
.attr('event-id',this.eventId)
.attr('id','event_' + this.eventId);
// draw the path
this.eventLine = this.eventGroup.append("line")
.attr('stroke', this._checkColorType(this.lineConfig.color))
.attr('stroke-width', this.lineConfig.weight);
this._addIcon();
this._positionEvent();
this._addClipPath();
this._eventConfigChanged = false;
} else {
// update the path
this._positionEvent();
}
this._addTooltip();
}
},
/**
* Sets a boolean to true when run
*
*/
_eventConfigHasChanged: function() {
this._eventConfigChanged = true;
},
/**
* Helper to call addClipPath with the elem
*
* @method _addClipPath
*/
_addClipPath: function() {
this.addClipPath(this.eventLine);
this.addClipPath(this.eventIcon);
},
/**
* Sets the position for the event line and icon
*
* @method _positionEvent
*/
_positionEvent: function() {
this.eventLine
.attr("x1", this.x(this.eventData[this.xKey]))
.attr("x2", this.x(this.eventData[this.xKey]))
.attr("y1", this.y(this.currentDomainY[0]))
.attr("y2", this.y(this.currentDomainY[1]));
this.eventIcon
.attr('x',this.x(this.eventData[this.xKey]) - this._thisConfig.size/2 + this._thisConfig.offset[0])
.attr("y", this.y(this.currentDomainY[1]) - 5 + this._thisConfig.offset[1]);
},
_addIcon: function() {
if(this._thisConfig.type === 'image'){
this.eventIcon = this.eventGroup.append("image")
.attr("xlink:href", this._thisConfig.icon)
.attr("width", this._thisConfig.size + 'px')
.attr("height", this._thisConfig.size + 'px');
} else if(this._thisConfig.type === 'unicode'){
this.eventIcon = this.eventGroup.append("text")
.attr('font-family', 'FontAwesome')
.attr('font-size', this._thisConfig.size + 'px')
.attr('fill', this._checkColorType(this._thisConfig.color))
.text(this._thisConfig.icon);
} else if(this._thisConfig.type === 'fa'){
this.eventIcon = this.eventGroup.append("text")
.attr('font-family', 'FontAwesome')
.attr('font-size', this._thisConfig.size + 'px')
.attr('fill', this._checkColorType(this._thisConfig.color))
.text(String.fromCharCode(parseInt(this.fa[this._thisConfig.icon],16)));
}
this.eventIcon
.style("cursor","pointer")
.attr('id','ei_' + this.eventId);
},
/**
* Compiles a configuration object by looking if there is a dev set style, or if it should use defaults
*
* @method _processConfiguration
*/
_processConfiguration:function() {
// is there a configuration object for this event type?
if(this.eventConfig[this.eventData.label]){
/*
check each key (icon, color, etc) of the defaultEventConfig and see if the eventConfig is overwriting that property. If so, set it, otherwise use default
*/
var k = Object.keys(this.defaultEventConfig),
i,
len = k.length;
for(i = 0; i < len; i++){
this._thisConfig[k[i]] = this.eventConfig[this.eventData.label][k[i]] || this.defaultEventConfig[k[i]];
}
} else {
// else, set everything to default
this._thisConfig = this.defaultEventConfig;
}
},
/**
* Configures the tooltip for that particuar event
*
* @method _addTooltip
*/
_addTooltip:function() {
this.$.eventTooltip.set('for',this.eventIcon.node());
},
/**
* Formats the datetime to match the dev set format
*
* @method _formatDatetime
*/
_formatDatetime: function(time,format) {
return this.formatTimestamp(time, this.timezone, format);
},
/**
* returns the title value to the tooltip
*
*/
_getValueTitle: function(xAxisType) {
if(xAxisType === 'time' || xAxisType === 'timeLocal') {
return 'Timestamp';
} else {
return 'X';
}
},
/**
* returns the value of the x
*
*/
_getValue: function(xAxisType) {
if(xAxisType === 'time' || 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>