-
Notifications
You must be signed in to change notification settings - Fork 13
/
nvd3-behavior.html
385 lines (336 loc) · 8.59 KB
/
nvd3-behavior.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import"
href="../iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="nvd3-import.html">
<style is="custom-style" include="nvd3-shared-styles"></style>
<script>
var NVD3 = NVD3 || {};
/**
* @polymerBehavior NVD3.ChartBehavior
*/
NVD3.ChartBehaviorImpl = {
properties: {
/*
* Data
*/
data: {
type: Array,
value: function() {
return [];
},
observer: '_dataChanged'
},
/*
* SVG object for d3
*/
svg: {
type: Object
},
/**
* Stop auto drawing the chart on attaching the element.
*/
stopAutoDrawing: {
type: Boolean,
value: false
},
/**
* Auto resize chart after resizing browser window.
*/
autoResize: {
type: Boolean,
value: false
},
/**
* Colors to use for the different data.
* If an array is given, it is converted to a function automatically.
*/
color: {
type: Array,
value: function() {
nv.utils.defaultColor();
}
},
/**
* The height the graph or component created inside the SVG should be made.
* Default: The height of the container element (normally the svg itself).
*/
height: {
type: Number
},
/**
* Object containing the margins for the chart or component.
* You can specify only certain margins in the object to change just
* those parts.
*/
margin: {
type: Object,
value: function() {
return {
left: null,
right: null,
top: null,
bottom: null
};
}
},
/**
* Message to display if no data is provided.
*/
noData: {
type: String
},
/**
* The width the graph or component created inside the SVG should be made.
* Default: The width of the container element (normally the svg itself).
*/
width: {
type: Number
},
/**
* View box height
*/
viewBoxHeight: {
type: Number,
value: 100
},
/**
* View box width
*/
viewBoxWidth: {
type: Number,
value: 100
},
/**
* Extra width for view box so labels don't get cut off
*/
extraViewBoxWidth: {
type: Number,
value: 50
},
/**
* Holds the id for the tooltip so it can be removed when chart is detached/removed from DOM.
*/
_tooltipId: {
type: String,
readOnly: true
},
/**
* Make use of viewBox for sizing
*/
flexSize: {
type: Boolean,
value: false
}
},
observers: [
'_setColor(color.length)',
'_setMargin(margin.top, margin.right, margin.bottom, margin.left)',
'_setNoData(noData)',
'_setWidth(width)',
'_setHeight(height)',
'_setShowLabels(showLabels)',
'_setShowLegend(showLegend)',
'_setUseInteractiveGuideline(useInteractiveGuideline)',
'_setGrowOnHover(growOnHover)',
'_setDisableTooltip(disableTooltip)'
],
listeners: {
'iron-resize': '_onResize',
'transitionend': '_onTransitionEnd'
},
/**
* Listener for iron-resize
*/
_onResize: function() {
if (this.autoResize) {
this.debounce('autoResize', function() {
this.refresh();
}.bind(this), 200);
}
},
_onTransitionEnd: function(e) {
// Workaround when nvd3 elements are used inside an iron-collapse.
// Some nvd3-elemts use css transitions, for exemple to change opacity in
// pie when mouse is hover.
// iron-collaspe react to transitionend events by firing an iron-resize,
// which cause the chart to repaint.
e.stopPropagation();
},
/**
* Observer for setting color.
*/
_setColor: function(colorLength) {
this._chart.color(this.color);
},
/**
* Observer for setting height.
*/
_setHeight: function(height) {
if (typeof height === 'number' && height > 0) {
this.$$('div').style.height = height + 'px';
}
},
/**
* Observer for setting margins.
*/
_setMargin: function(marginTop, marginRight, marginBottom, marginLeft) {
if (marginTop) {
this._chart.margin({
"top": marginTop
});
}
if (marginRight) {
this._chart.margin({
"right": marginRight
});
}
if (marginBottom) {
this._chart.margin({
"bottom": marginBottom
});
}
if (marginLeft) {
this._chart.margin({
"left": marginLeft
});
}
},
/**
* Observer for setting text of no data.
*/
_setNoData: function(noData) {
this._chart.noData(noData);
},
/**
* Observer for setting show labels.
*/
_setShowLabels: function(showLabels) {
this._chart.showLabels(showLabels);
},
/**
* Observer for setting show legend.
*/
_setShowLegend: function(showLegend) {
this._chart.showLegend(showLegend);
},
/**
* Observer for setting width.
*/
_setWidth: function(width) {
if (typeof width === 'number' && width > 0) {
this.$$('div').style.width = width + 'px';
}
},
/**
* Observer for setting use interactive guideline.
*/
_setUseInteractiveGuideline: function(useInteractiveGuideline) {
this._chart.useInteractiveGuideline(this.useInteractiveGuideline);
},
/**
* Observer for growOnHover.
*/
_setGrowOnHover: function(growOnHover) {
this._chart.growOnHover(this.growOnHover);
},
/**
* Observer for disableTooltip
*/
_setDisableTooltip: function(disableTooltip) {
this._chart.tooltip.enabled(!disableTooltip);
},
/**
* Observer for data changed
*/
_dataChanged: function() {
if (this._chart) {
d3.select(this.svg).datum(this.data);
this.refresh();
}
},
/**
* Updates the chart
*/
refresh: function() {
if (this._chart.update) {
this._chart.update();
}
},
/**
* Generate and attach the chart;
*/
generateChart: function() {
nv.addGraph(function() {
d3.select(this.svg)
.datum(this.data)
.call(this._chart);
if (this.flexSize) {
d3.select(this.svg)
.attr('width', this.viewBoxWidth + this.extraViewBoxWidth)
.attr('height', this.viewBoxHeight);
this._chart.update();
}
this._set_tooltipId(this._chart.tooltip.id());
this._bindEvents();
return this._chart;
}.bind(this));
},
/**
* Bind NVD3 events of chart and its components to fire custom events
* @private
*/
_bindEvents: function() {
let componentsEvents = {};
this._components.forEach(function(component) {
if (this._chart[component].dispatch) {
componentsEvents[component] = Object.keys(this._chart[component].dispatch);
}
}.bind(this));
Object.keys(this._chart.dispatch).forEach(function(event) {
this._bindChartEvent(event);
}.bind(this));
Object.keys(componentsEvents).forEach(function(component) {
componentsEvents[component].forEach(function(eventName) {
this._bindComponentEvent(component, eventName);
}.bind(this));
}.bind(this));
},
/**
* Bind NVD3 events of chart to fire custom events
* @private
*/
_bindChartEvent: function(eventName) {
this._chart.dispatch.on(eventName, function(e) {
this.fire(eventName, e);
}.bind(this));
},
/**
* Bind NVD3 events of chart's components to fire custom events
* @private
*/
_bindComponentEvent: function(component, eventName) {
this._chart[component].dispatch.on(eventName, function(e) {
this.fire(eventName, e);
}.bind(this));
},
ready: function() {
this.set('svg', this.$.svg);
this.scopeSubtree(this.$.svg, true);
},
attached: function() {
if (!this.stopAutoDrawing) {
this.generateChart();
}
},
detached: function() {
var tooltip = document.getElementById(this._tooltipId);
if (tooltip) {
tooltip.remove();
}
}
};
NVD3.ChartBehavior = [
NVD3.ChartBehaviorImpl,
Polymer.IronResizableBehavior
];
</script>