forked from predixdesignsystem/px-vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx-vis-scatter.html
418 lines (371 loc) · 11.8 KB
/
px-vis-scatter.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
<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" />
<!--
Element which draws scatter series onto the chart
##### Usage
<px-vis-scatter
svg="[[svg]]"
clip-path="[[clipPath]]"
series-id="[[item.name]]"
series-number="[[index]]"
chart-data="[[item]]"
x="[[x]]"
y="[[y]]"
current-domain-x="[[currentDomainX]]"
current-domain-y="[[currentDomainY]]"
domain-changed="[[domainChanged]]"
muted-series="[[mutedSeries]]">
</px-vis-scatter>
@element px-vis-scatter
@blurb Element which draws lines series onto the chart
@homepage index.html
@demo demo.html
-->
<link rel="import" href="css/px-vis-styles.html">
<dom-module id="px-vis-scatter">
<template>
<style include="px-vis-styles"></style>
</template>
</dom-module>
<script>
Polymer({
is: 'px-vis-scatter',
behaviors: [
PxVisBehaviorD3.svg,
PxVisBehaviorD3.axes,
PxVisBehavior.dataset,
PxVisBehavior.mutedSeries,
PxVisBehavior.commonMethods,
PxVisBehaviorD3.clipPath,
PxVisBehavior.completeSeriesConfig,
PxVisBehaviorD3.radialLineGenerator,
PxVisBehavior.tooltipData,
PxVisBehaviorD3.domainUpdate,
PxVisBehavior.polarData,
PxVisBehavior.seriesId,
PxVisBehavior.dynamicConfigProperties,
PxVisBehaviorD3.serieToRedrawOnTopSVG,
PxColorsBehavior.baseColors
],
/**
* Properties block, expose attribute values to the DOM via 'reflect'
*
* @property properties
* @type Object
*/
properties: {
/**
* An index of the series used for calculating its color
*
* @property scatterNumber
* @type String
*/
seriesNumber:{
type:Number,
value:0
},
/**
* Maps dev input markers to d3 symbols
*
*/
_markerMapping: {
type: Object,
readOnly: true,
value: {
'circle': Px.d3.symbolCircle,
'cross': Px.d3.symbolCross,
'diamond': Px.d3.symbolDiamond,
'square': Px.d3.symbolSquare,
'triangle-up': Px.d3.symbolTriangle,
'star': Px.d3.symbolStar,
'wye': Px.d3.symbolWye
},
/**
* The symbol used for the marker. Supported symbols:
* - 'circle'
* - 'cross'
* - 'diamond'
* - 'square'
* - 'triangle-up'
* - 'star'
* - 'wye'
* more info at https://github.com/d3/d3-shape/blob/master/README.md#symbols
* This property will be read from the completeSeriesConfig
*/
markerSymbol: {
type: String
},
/**
* Scales the size of the scatter marker
* This property will be read from the completeSeriesConfig
*/
markerScale: {
type: Number
},
/**
* the size of the markers
* This property will be read from the completeSeriesConfig
*/
markerSize: {
type: Number
},
/**
* The opacity of the fill (inside) of the marker
* This property will be read from the completeSeriesConfig
*/
markerFillOpacity: {
type: Number
},
/**
* The opacity of the stroke (outside) of the marker
* This property will be read from the completeSeriesConfig
*/
markerStrokeOpacity: {
type: Number
},
/**
* the opacity value of the fill to be used when muting a serie (stroke is not drawn on mute)
* This property will be read from the completeSeriesConfig
*/
mutedOpacity: {
type: Number
},
},
/**
* A holder object for the series group
*
* @property scatterGroup
* @type Object
*/
scatterGroup:{
type:Object,
value: function() {return{};}
},
/**
* A holder object for the series objects
*
*/
scatterDots:{
type:Object
},
/**
* A holder object for the series builder
*
*/
scatterBuilder: {
type: Object
},
/**
* Whether the scatter plot is using radial coiordinates (x=phase, y=amplitude)
*/
radial: {
type: Boolean,
value: false
},
/**
* Name of the variable holding the time stamp in the data. Used for non timeseries charts
*/
timeData: {
type: String,
value: 'Timestamp'
},
},
observers: [
'drawElement(svg, domainChanged, chartData.*, completeSeriesConfig.*, radial, counterClockwise,markerSymbol, markerScale, markerSize, markerFillOpacity, markerStrokeOpacity)',
'isIdInMuted(mutedSeries.*)',
'_addClipPath(clipPath)',
'_drawOnTop(serieToRedrawOnTop)'
],
ready:function() {
//generate properties dynamically
this._watchConfigProperty('markerSymbol', 'circle');
this._watchConfigProperty('markerScale', 1);
this._watchConfigProperty('markerSize', 64);
this._watchConfigProperty('markerFillOpacity', 0.6);
this._watchConfigProperty('markerStrokeOpacity', 1);
this._watchConfigProperty('mutedOpacity', 0.3);
},
detached: function() {
this.scatterGroup.remove();
},
/**
* Draws or updates the scatter element.
* Called from an observer watching for data and the necessary d3 objects
*
* @method drawElement
*/
drawElement: function() {
if(typeof this.domainChanged !== 'undefined' && this.domainChanged !== null) {
var data = this.chartData;
// checks to see if the group already exists. If not, create; if so, update
if(this._isObjEmpty(this.scatterGroup)) {
// draw the path
this.scatterGroup = this.svg.append('g')
.attr('series-id', 'scatter_' + this.seriesId)
.attr('class', 'series-scatter');
//send empty tooltip data so the register shows up
this._hideTooltip();
}
if(this.radial) {
this._defineRadialLine(false, this.multiPath, this.counterClockwise, this.useDegrees);
}
var _this = this;
this.scatterBuilder = this.scatterGroup.selectAll('path.symbol')
.data(data);
this.scatterGroup.on('mousedown', function() {
console.log('SCATTER GROUP MOUSE DOWN')
})
this.scatterBuilder.exit().remove();
this.scatterBuilder.enter()
.append('path')
.attr('class', 'symbol')
.on('mouseover', this._mouseEnterSymbol.bind(this))
.on('mouseleave', this._mouseLeaveSymbol.bind(this))
.on('mousedown', function() {
console.log('SCATTER MOUSE DOWN')
})
.merge(this.scatterBuilder)
.attr('d', Px.d3.symbol().type(this._markerMapping[this.markerSymbol]).size(this.markerSize))
.attr('transform', this._applyTransform.bind(this))
.each(function(d,i) {
// if this series is missing a datapoint, remove that point
if(!_this._isValidData(d[_this.completeSeriesConfig[_this.seriesId]['y']])) {
this.remove();
}
});
this.scatterDots = this.scatterGroup.selectAll('path.symbol');
this._colorScatter();
this._addClipPath();
}
},
/**
* Mouseevent when mouse enters
*
*/
_mouseEnterSymbol: function(d, i, paths) {
this._showTooltip(d, i, paths);
if(!this.mutedSeries[this.seriesId]) {
//if radial make sur eon hover we have 100% opacity
Px.d3.event.currentTarget.setAttribute('fill-opacity', 1);
}
},
/**
* generates tooltipData
*/
_showTooltip: function(d, i, paths) {
// setup a holder for our data to pass out
var dataObj = {
'time': d[this.timeData],
'series': [],
'mouse': [],
'xArr': null,
'yArr': null
},
values = {},
xKey = this.completeSeriesConfig[this.seriesId].x,
yKey = this.completeSeriesConfig[this.seriesId].y,
box = paths[i].getBoundingClientRect(),
center = [window.pageXOffset + box.left + box.width/2, window.pageYOffset + box.top + box.height/2];
//TODO: boolean to convert this to radians if someone wants radians in their tooltip
values[xKey] = d[xKey];
values[yKey] = d[yKey];
dataObj.series.push({'name': this.seriesId, 'value': values});
this.set('tooltipData',dataObj);
this.fire('px-vis-tooltip-updated', { 'dataVar': 'tooltipData', 'data': dataObj, 'method':'set' });
this.fire('px-vis-scatter-request-tooltip', {'show': true, 'mouseCoords': center});
},
/**
* Mousevent on leave
*/
_mouseLeaveSymbol: function(d) {
this._hideTooltip(d);
if(!this.mutedSeries[this.seriesId]) {
//if radial make sur eon hover we have 100% opacity
Px.d3.event.currentTarget.setAttribute('fill-opacity', this.markerFillOpacity);
}
},
/**
* Resets tooltipData
*/
_hideTooltip: function(d) {
var dataObj = {
'time': null,
'series': [{'name': this.seriesId, 'value':null}],
'mouse': [],
'xArr': null,
'yArr': null
};
this.set('tooltipData',dataObj);
this.fire('px-vis-tooltip-updated', { 'dataVar': 'tooltipData', 'data': dataObj, 'method':'set' });
this.fire('px-vis-scatter-request-tooltip', {'show': false});
},
/**
* Helper to call addClipPath with the elem
*
* @method _addClipPath
*/
_addClipPath: function() {
this.addClipPath(this.scatterGroup);
},
/**
* Checks mutedSeries to see if this ID is in there
* Called from an observer watching mutedSeries
*
* @method isIdInMuted
*/
isIdInMuted: function() {
if(this.mutedSeries.hasOwnProperty(this.seriesId)){
this._colorScatter();
}
},
/**
* Applies the necessary translate and scale to the scatterDots
*
*/
_applyTransform: function(d) {
if(!this._isValidData(d[this.completeSeriesConfig[this.seriesId]['y']])) {
return '';
}
if(this.radial) {
var coors = this.line([d]).slice(1).slice(0, -1);
return 'translate(' + coors + ')' + ' scale(' + this.markerScale + ')';
} else {
var xTrans,
yTrans;
if(this._isValidData(d[this.completeSeriesConfig[this.seriesId]['x']])) {
xTrans = this.x(d[this.completeSeriesConfig[this.seriesId]['x']]);
}
if(this._isValidData(d[this.completeSeriesConfig[this.seriesId]['y']])) {
yTrans = this.y(d[this.completeSeriesConfig[this.seriesId]['y']]);
}
//make sure we have a value
xTrans = xTrans ? xTrans : 0;
yTrans = yTrans ? yTrans : 0;
return 'translate(' + xTrans + ',' + yTrans + ')' + ' scale(' + this.markerScale + ')';
}
},
/**
* Adds full color to the scatters.
*
* @method _colorScatter
*/
_colorScatter:function() {
var index = this.seriesNumber,
muted = this.mutedSeries[this.seriesId];
this.scatterDots
.attr('fill',this.completeSeriesConfig[this.seriesId]['color'])
.attr('fill-opacity', muted ? this.mutedOpacity : this.markerFillOpacity)
.attr('stroke',this.completeSeriesConfig[this.seriesId]['color'])
.attr('stroke-opacity', muted ? 0 : this.markerStrokeOpacity);
},
/**
* Redraw this serie on top if needed
*/
_drawOnTop: function(serieToRedrawOnTop) {
if(this.scatterGroup && this.seriesId) {
this._drawSVGOnTop(serieToRedrawOnTop, this.seriesId, this.scatterGroup);
}
}
});
</script>