-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnvd3-behavior.html
327 lines (277 loc) · 9.58 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
<!--
@license
Copyright (c) 2015 Renato Utsch. All rights reserved.
This code may only be used under the BSD style license found at the LICENSE.txt
file distributed with this project, also located at:
https://github.com/RenatoUtsch/poly-nvd3/raw/master/LICENSE.txt
-->
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="../d3-import/d3.html">
<link rel="import" href="nvd3.html">
<script>
/**
* `nvd3-behavior` wraps nvd3 library into a reactive and responsive component
* for Polymer.
*
* More info on the repository.
* @polymerBehavior nvd3-behavior
*/
var NVD3BehaviorImpl = {
properties: {
/**
* Object that contains the data that will be displayed on the
* chart.
* Ignored when the `url` attribute is used.
*
* @attribute: data
*/
data: {
type: Array,
value: null,
observer: '_update'
},
/**
* The `url` attribute specifies an url to get the JSON data that
* is used in the nvd3 chart.
* You can use the `params` attribute to specify the parameters in
* the GET request, much in the same way as the `iron-ajax` element.
*
* When this attribute is present, the `data` attribute is ignored.
*
* @attribute: url
*/
url: {
type: String,
value: '',
observer: '_request'
},
/**
* The `params` attribute specifies the parameters to the url
* specified in the `url` attribute, in JSON. This attribute works
* in the same way as the iron-ajax's one.
*
* This attribute is ignored unless the `url` attribute is set.
*
* @attribute: params
*/
params: {
type: String,
value: '',
observer: '_request'
},
/**
* If specified, the width of the chart will be this value. If not,
* it will be the width of the parent element. If there isn't a
* parent, it will be set to 1000px.
*
* @attribute: width
*/
width: {
type: Number,
value: 0,
observer: '_resize'
},
/**
* If specified, the height of the chart will be this value. If not,
* see the 'fit' property for the behaviour of the height.
*
* @attribute: height
*/
height: {
type: Number,
value: 0,
observer: '_resize'
},
/**
* This properties changes the behaviour of the height of the chart.
* If this property is not defined, the height is set to the width
* divided by the golden ratio. If this property is defined, the
* height is set to the height of the parent element of the chart.
*
* Of course, this only matters if the "height" property isn't
* defined. If it is, the height of the chart will be the one set
* in that property.
*
* @attribute: fit
*/
fit: {
type: Boolean,
value: false,
reflectToAttribute: true,
observer: '_resize'
},
/**
* NVD3 chart instance.
*
* @attribute: _chart
*/
_chart: {
type: Object,
value: null
},
/**
* SVG container used to resize the chart.
*
* @attribute: _container
*/
_container: {
type: Object,
value: null
},
/**
* D3 SVG selection.
*
* @attribute: _svg
*/
_svg: {
type: Object,
value: null
},
/**
* Ajax element to retrieve data from local files.
*
* @attribute: _ajax
*/
_ajax: {
type: Object,
value: null
}
},
listeners: {
"iron-resize": "_resize"
},
created: function() {
this._container = Polymer.dom(this.root).appendChild(document.createElement("div"));
this._svg = d3.select(this._container).append("svg");
this._ajax = document.createElement('iron-ajax');
this._ajax.handleAs = 'json';
// To add the offsetWidth property to this.root.
this.style.display = "block";
},
ready: function() {
this._create();
},
/**
* Default onCreate() function. Replace with your own,
* setting the configuration.
*/
onCreate: function(chart, svg) {
},
/**
* This function is called to format the data. Be careful and
* ensure you do not change *data* reference, generating a circular loop.
*/
formatData: function (data) {
},
/**
* Default afterCreate() function. Replace with your own if you want.
*/
afterCreate: function(chart, svg) {
},
/**
* Function called before the chart resize. Replace with your own if
* you want.
*/
beforeResize: function(chart, svg) {
},
/**
* Function called after the chart resize. Replace with your own if
* you want.
*/
afterResize: function(chart, svg) {
},
/**
* Function called after data has loaded, even if the data was empty.
*/
onDataLoaded: function(data) {
},
/**
* Function called if data fails to load
*/
onError: function(error) {
},
/**
* Creates the nvd3 model for the chart.
*/
_create: function() {
nv.addGraph(function() {
this._chart = this.nvModel();
this.onCreate(this._chart, this._svg);
this._update();
this._resize();
return this._chart;
}.bind(this), function() {
this.afterCreate(this._chart, this._svg);
}.bind(this));
},
/**
* Updates the data of the chart. Called if it changed.
*/
_update: function() {
this.onDataLoaded(this.data);
if(!this._chart || !this.data) return;
this.formatData(this.data);
this._svg.datum(this.data)
.call(this._chart);
},
/**
* Resizes the chart. Called if the parent element's size changed.
*/
_resize: function() {
if (!this._chart) return;
this.debounce("resize-chart", function () {
var w, h;
this.beforeResize(this._chart, this._svg);
/*
* The width is selected as follows:
* - First, use the 'width' property if it is set.
* - Second, use the width of the bounding client rect of the
* element if it is set.
* - Third, fallback to 1000 pixels.
*
* The height has two behaviors: if the fit attribute was defined,
* the height of the bounding client rect of the element is used
* if this.height is 0. If not, the fallback is to the width
* divided by the golden ratio.
*/
var boundingRect = this.getBoundingClientRect();
w = this.width ? this.width : (boundingRect.width ?
boundingRect.width : 1000);
h = this.height ? this.height : (this.fit ?
boundingRect.height : w / 1.6180);
this._chart
.width(w)
.height(h);
this._container.style.width = w + "px";
this._container.style.height = h + "px";
if (this.data) {
this._svg
.call(this._chart);
}
this.afterResize(this._chart, this._svg);
}, 100);
},
/*
* Gets data from the request url.
*/
_request: function() {
if(this.url) {
for (var i = 0; i < this._ajax.activeRequests.length; i++) {
this._ajax.activeRequests[i].abort();
}
// Get the data from the url and set it as the current data.
this._ajax.url = this.url;
this._ajax.params = this.params;
this._ajax.addEventListener('response', function(e) {
this.data = e.detail.response;
}.bind(this));
this._ajax.addEventListener('error', function(e) {
this.onError(e);
}.bind(this));
this._ajax.generateRequest();
}
}
};
var NVD3Behavior = [Polymer.IronResizableBehavior, NVD3BehaviorImpl];
</script>