forked from predixdesignsystem/px-vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx-vis-line-svg.html
316 lines (285 loc) · 8.63 KB
/
px-vis-line-svg.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
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="px-vis-behavior-d3.html" />
<link rel="import" href="px-vis-behavior-common.html" />
<!--
Element which draws lines series onto the chart
##### Usage
<px-vis-line-svg
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]]"
muted-series="[[mutedSeries]]"
muted-opacity="0"
interpolation-function="[[d3InterpolationFunction]]">
</px-vis-line-svg>
@element px-vis-line-svg
@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-line-svg">
<template>
<style include="px-vis-styles"></style>
</template>
</dom-module>
<script>
Polymer({
is: 'px-vis-line-svg',
behaviors: [
PxVisBehaviorD3.svg,
PxVisBehaviorD3.lineShared,
PxVisBehaviorD3.axes,
PxVisBehavior.dataset,
PxVisBehavior.mutedSeries,
PxVisBehavior.commonMethods,
PxVisBehaviorD3.clipPath,
PxVisBehavior.completeSeriesConfig,
PxVisBehavior.categories,
PxVisBehaviorD3.selectedDomain,
PxVisBehavior.polarData,
PxVisBehavior.dynamicConfigProperties,
PxVisBehavior.serieToRedrawOnTop,
PxVisBehaviorD3.serieToRedrawOnTopSVG,
PxColorsBehavior.baseColors
],
/**
* px-vis-progressive-rendering-started, fired when a canvas with progressive rendering
* enabled starts rendering
* evt.details contains:
* { numberOfFrames: the number of frames that will be used to render,
* }
*
* @event px-vis-progressive-rendering-started
*/
/**
* px-vis-progressive-rendering-ended, fired when a canvas with progressive rendering
* enabled has finished rendering
* evt.details contains:
* { numberOfFrames: the number of frames that have been used to render,
* }
*
* @event px-vis-progressive-rendering-ended
*/
/**
* Properties block, expose attribute values to the DOM via 'reflect'
*
* @property properties
* @type Object
*/
properties: {
/**
* A holder object for the series object
*
*/
linePath:{
type:Object
},
/**
* A holder object for the series builder
*
* @property linePath
* @type String
*/
lineBuilder:{
type:Object
},
/**
* A holder object for the series group
*
*/
lineGroup:{
type:Object
},
/**
* A dev set boolean specifying if the chart is for a parallel coordinates chart
*
*/
parallelCoordinates: {
type: Boolean,
value: false
},
strokeWidth: {
type: Number,
value: 1
},
/**
* 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
},
/**
* Debounce time to use for drawing
*/
drawDebounceTime: {
type: Number,
value: 10
}
},
observers: [
'drawElement(svg,domainChanged,chartData.*,completeSeriesConfig.*, counterClockwise)',
'isIdInMuted(mutedSeries.*, completeSeriesConfig)',
'_addClipPath(clipPath)',
'_drawOnTop(serieToRedrawOnTop)'
],
ready: function() {
this._watchConfigProperty('mutedOpacity', 0.3);
},
detached: function() {
if(this._doesD3HaveValues(this.linePath)) {
this.linePath.remove();
}
if(this._doesD3HaveValues(this.lineGroup)) {
this.lineGroup.remove();
}
},
/**
* Draws or updates the line element.
* Called from an observer watching for data and the necessary d3 objects
*
* @method drawElement
*/
drawElement: function() {
//if 0 don't debounce at all
if(this.drawDebounceTime > 0) {
this.debounce('draw',function() {
this._drawElementDebounced();
}.bind(this), this.drawDebounceTime);
} else {
this._drawElementDebounced();
}
},
_drawElementDebounced: function() {
// we need to wrap the data differently depending on if we are drawing many lines or just one line
if(this.parallelCoordinates) {
var data = this.chartData;
this._defineMultiLine(false);
} else if(this.radialLine) {
this._defineRadialLine(false, this.multiPath, this.counterClockwise, this.useDegrees);
data = this.multiPath ? this.chartData : [this.chartData];
} else {
var data = [this.chartData];
this._defineSingleLine(false);
}
// checks to see if the group already exists. If not, create; if so, update
if(this._isObjEmpty(this.lineGroup)) {
// draw the path
this.lineGroup = this.svg.append('g')
.attr('series-id', 'line_' + this.seriesId)
.attr('class', 'series-line');
}
// set our data
var lineBuilder = this.lineGroup.selectAll('path.series-line')
.data(data);
// When datapoints disappear from our data, remove them
lineBuilder.exit().remove();
// When datapoints are added to our data, add stuff
lineBuilder.enter()
.append('path')
.attr('class', 'series-line')
.attr('series-id', function(d,i) {
return this.multiPath ? 'line_' + d[this.seriesId] : 'line_' + this.seriesId
}.bind(this))
.attr('fill','none')
// When datapoints are added OR this funtion is run, draw stuff
.merge(lineBuilder)
.attr('d', function(d) {
return this.line(d);
}.bind(this))
.attr('stroke-width',this.strokeWidth)
.attr('stroke',function(d,i) {
//normally, do this
if(!this.categoryKey) {
return this.completeSeriesConfig[this.seriesId]['color'];
}
//check that category has been defined
if(this.completeSeriesConfig[d[this.categoryKey]]) {
return this.completeSeriesConfig[d[this.categoryKey]]['color']
}
//do nothing if not
return
}.bind(this))
.attr('stroke-opacity',this._svgLineOpacity.bind(this));
// Make it easy to get our lines again
this.linePath = this.lineGroup.selectAll('path.series-line');
this._addClipPath();
this.isIdInMuted();
},
/**
* Helper to call adClipPath with the elem
*
* @method _addClipPath
*/
_addClipPath: function() {
this.addClipPath(this.lineGroup);
},
/**
* Checks mutedSeries to see if this ID is in there
* Called from an observer watching mutedSeries
*
* @method isIdInMuted
*/
isIdInMuted: function() {
if(this.multiPath && this._doesObjHaveValues(this.linePath)) {
this.linePath.style('display', function(d) {
// if we have multiPaths, then just hide them; might want to changes this but need to check with design
return this.mutedSeries[d[this.seriesId]] ? 'none' : null;
}.bind(this));
this._colorLine();
} else if(this.mutedSeries.hasOwnProperty(this.seriesId)) {
this._colorLine();
}
},
/**
* Adds full color to the SVG line.
*
*/
_colorLine:function() {
if(this.linePath) {
this.linePath.attr('stroke',function(d,i) {
//normally, do this
if(!this.categoryKey) {
return this.completeSeriesConfig[this.seriesId]['color'];
}
//check that category has been defined
if(this.completeSeriesConfig[d[this.categoryKey]]) {
return this.completeSeriesConfig[d[this.categoryKey]]['color']
}
//do nothing if not
return
}.bind(this))
.attr('stroke-opacity',this._svgLineOpacity.bind(this));
}
},
/**
* Returns the opacity for the SVG line
*
*/
_svgLineOpacity: function(d) {
if(this.mutedSeries[this.seriesId]) {
return this.mutedOpacity;
}
if(this.gradientLine) {
return this._opacityLine(d);
} else {
return 1;
}
},
/**
* Redraw this serie on top if needed
*/
_drawOnTop: function(serieToRedrawOnTop) {
if(this.lineGroup && this.seriesId) {
this._drawSVGOnTop(serieToRedrawOnTop, this.seriesId, this.lineGroup);
}
}
});
</script>