Completed
Push — master ( 3e711f...fe0a7b )
by Chris
01:20
created

jsondash.handlers.handleWordCloud   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 17
rs 9.4285
c 1
b 0
f 0
nc 1
nop 1
1
/** global: jsondash */
2
/** global: c3 */
3
/** global: d3 */
4
/** global: venn */
5
/** global: Plotly */
6
7
jsondash.getJSON = function(url, callback) {
8
    return d3.json(url, callback);
9
};
10
11
/**
12
 * Handlers for various widget types. The method signatures are always the same,
13
 * but each handler can handle them differently.
14
 */
15
16
jsondash.handlers.handleYoutube = function(container, config) {
17
    // Clean up all previous.
18
    'use strict';
19
    container.selectAll('iframe').remove();
20
21
    function getAttr(prop, props) {
22
        // Return the propery from a list of properties for the iframe.
23
        // e.g. getAttr('width', ["width="900""]) --> "900"
24
        return props.filter(function(k, v){
0 ignored issues
show
Unused Code introduced by
The parameter v is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
25
            return k.startsWith(prop);
26
        })[0];
27
    }
28
29
    var url = config.dataSource;
0 ignored issues
show
Unused Code introduced by
The assignment to variable url seems to be never used. Consider removing it.
Loading history...
30
    var parts = config.dataSource.split(' ');
31
    var height = parseInt(getAttr('height', parts).split('=')[1].replace(/"/gi, ''), 10);
32
    var width = parseInt(getAttr('width', parts).split('=')[1].replace(/"/gi, ''), 10);
33
    var url = getAttr('src', parts).replace('src=', '').replace(/"/gi, '');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable url already seems to be declared on line 29. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
34
35
    // In the case of YouTube, we have to override the config dimensions
36
    // as this will be wonky when the aspect ratio is calculated. We will
37
    // defer to YouTube calculations instead.
38
    container.append('iframe')
39
        .attr('width', width)
40
        .attr('height', height)
41
        .attr('src', url)
42
        .attr('allowfullscreen', true)
43
        .attr('frameborder', 0);
44
    // Look for callbacks potentially registered for third party code.
45
    jsondash.api.runCallbacks(container, config);
46
    jsondash.unload(container);
47
};
48
49
/**
50
 * [handleGraph creates graphs using the dot format
51
 * spec with d3 and dagre-d3]
52
 */
53
jsondash.handlers.handleGraph = function(container, config) {
54
    'use strict';
55
    jsondash.getJSON(config.dataSource, function(error, data){
56
        if(error) {
57
            jsondash.unload(container);
58
            throw new Error("Could not load url: " + config.dataSource);
59
        }
60
        container.selectAll('.chart-graph').remove();
61
        var h = config.height - jsondash.config.WIDGET_MARGIN_Y;
0 ignored issues
show
Unused Code introduced by
The variable h seems to be never used. Consider removing it.
Loading history...
62
        var w = config.width - jsondash.config.WIDGET_MARGIN_X;
0 ignored issues
show
Unused Code introduced by
The variable w seems to be never used. Consider removing it.
Loading history...
63
        var svg = container.append('svg').classed({'chart-graph': true});
64
        var svg_group = svg.append('g');
65
        var g = graphlibDot.read(data.graph);
0 ignored issues
show
Bug introduced by
The variable graphlibDot seems to be never declared. If this is a global, consider adding a /** global: graphlibDot */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
66
        var bbox = null;
0 ignored issues
show
Unused Code introduced by
The assignment to bbox seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
67
        // Create the renderer
68
        var render = new dagreD3.render();
0 ignored issues
show
Bug introduced by
The variable dagreD3 seems to be never declared. If this is a global, consider adding a /** global: dagreD3 */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
69
        render(svg_group, g);
70
        bbox = svg.node().getBBox();
71
        svg.attr('width', bbox.width)
72
            .attr('height', bbox.height);
73
        // Look for callbacks potentially registered for third party code.
74
        jsondash.api.runCallbacks(container, config);
75
        jsondash.unload(container);
76
    });
77
};
78
79
/**
80
 * [handleWordCloud create word clouds using the d3-cloud extension.]
81
 */
82
jsondash.handlers.handleWordCloud = function(container, config) {
83
    'use strict';
84
    jsondash.getJSON(config.dataSource, function(error, data){
85
        if(error) {
86
            jsondash.unload(container);
87
            throw new Error("Could not load url: " + config.dataSource);
88
        }
89
        container.selectAll('.wordcloud').remove();
90
        var h     = config.height - jsondash.config.WIDGET_MARGIN_Y;
91
        var w     = config.width - jsondash.config.WIDGET_MARGIN_X;
92
        var svg   = container.append('svg').classed({'wordcloud': true});
93
        var fill  = d3.scale.category20();
0 ignored issues
show
Unused Code introduced by
The variable fill seems to be never used. Consider removing it.
Loading history...
94
        var cloud = d3.layout.cloud;
95
        var words = data.map(function(d) {
96
            return {text: d.text, size: d.size};
97
        });
98
        var layout = cloud()
99
            .size([w, h])
100
            .words(words)
101
            .padding(4)
102
            .rotate(function() {return ~~(Math.random() * 1) * 90;})
103
            .font('Arial')
104
            .fontSize(function(d) {return d.size;})
105
            .on('end', draw);
106
107
        layout.start();
108
109
        function draw(words) {
110
          svg
111
              .attr('width', layout.size()[0])
112
              .attr('height', layout.size()[1])
113
            .append('g')
114
              .attr('transform', 'translate(' + layout.size()[0] / 2 + ',' + layout.size()[1] / 2 + ')')
115
            .selectAll('text').data(words)
116
            .enter().append('text')
117
              .style('font-size', function(d) { return d.size + 'px'; })
118
              .style('font-family', 'arial')
119
              .style('fill', function(d, i) { return "#000"; })
0 ignored issues
show
Unused Code introduced by
The parameter d is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter i is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
120
              .attr('text-anchor', 'middle')
121
              .attr('transform', function(d) {
122
                return 'translate(' + [d.x, d.y] + ')rotate(' + d.rotate + ')';
123
              })
124
              .text(function(d) { return d.text; });
125
        }
126
127
        // Look for callbacks potentially registered for third party code.
128
        jsondash.api.runCallbacks(container, config);
129
        jsondash.unload(container);
130
    });
131
};
132
133
jsondash.handlers.handleC3 = function(container, config) {
134
    'use strict';
135
    var init_config = {
136
        bindto: '[data-guid="' + config.guid + '"] .chart-container',
137
        legend: {
138
            show: true
139
        },
140
        size: {
141
            height: config.height - jsondash.config.WIDGET_MARGIN_Y,
142
            width: config.width - jsondash.config.WIDGET_MARGIN_X
143
        },
144
        data: {
145
            type: config.type,
146
            url: config.dataSource,
147
            mimeType: 'json'
148
        },
149
        onrendered: function(){
150
            // Look for callbacks potentially registered for third party code.
151
            jsondash.api.runCallbacks(container, config);
152
            jsondash.unload(container);
153
        }
154
    };
155
    if(jsondash.util.isOverride(config)) {
156
        // Just use the raw payload for this widgets' options.
157
        jsondash.getJSON(config.dataSource, function(error, data){
158
            if(error) { throw new Error("Could not load url: " + config.dataSource); }
159
            // Keep existing options if not specified.
160
            config = $.extend(init_config, data);
161
            c3.generate(init_config);
162
        });
163
        return;
164
    }
165
    if(config.type === 'timeseries') {
166
        init_config.axis = {
167
            x: {type: 'timeseries'}
168
        };
169
        // Map the corresponding data key and list of dates
170
        // to the `x` property.
171
        init_config.data.x = 'dates';
172
    }
173
    c3.generate(init_config);
174
};
175
176
jsondash.handlers.handleD3 = function(container, config) {
177
    'use strict';
178
    // Clean up all D3 charts in one step.
179
    container.selectAll('svg').remove();
180
    // Handle specific types.
181
    if(config.type === 'radial-dendrogram') { return jsondash.handlers.handleRadialDendrogram(container, config); }
182
    if(config.type === 'dendrogram') { return jsondash.handlers.handleDendrogram(container, config); }
183
    if(config.type === 'voronoi') { return jsondash.handlers.handleVoronoi(container, config); }
184
    if(config.type === 'treemap') { return jsondash.handlers.handleTreemap(container, config); }
185
    if(config.type === 'circlepack') { return jsondash.handlers.handleCirclePack(container, config); }
186
    throw new Error('Unknown type: ' + config.type);
187
};
188
189
jsondash.handlers.handleCirclePack = function(container, config) {
190
    'use strict';
191
    // Adapted from https://bl.ocks.org/mbostock/4063530
192
    var margin = jsondash.config.WIDGET_MARGIN_Y;
193
    var diameter = d3.max([config.width, config.height]) - margin;
194
    var format = d3.format(',d');
195
196
    var pack = d3.layout.pack()
197
        .size([diameter, diameter])
198
        .value(function(d) { return d.size; });
199
200
    var svg = container
201
        .append('svg')
202
        .attr('width', diameter)
203
        .attr('height', diameter)
204
        .append('g');
205
206
    jsondash.getJSON(config.dataSource, function(error, data) {
207
        if(error) { throw new Error("Could not load url: " + config.dataSource); }
208
209
        var node = svg.datum(data).selectAll('.node')
210
        .data(pack.nodes)
211
        .enter().append('g')
212
        .attr('class', function(d) { return d.children ? 'node' : 'leaf node'; })
213
        .attr('transform', function(d) { return 'translate(' + d.x + ',' + d.y + ')'; });
214
215
        node.append('title')
216
        .text(function(d) { return d.name + (d.children ? '' : ': ' + format(d.size)); });
217
218
        node.append('circle')
219
        .attr('r', function(d) { return d.r; });
220
221
        node.filter(function(d) { return !d.children; }).append('text')
222
        .attr('dy', '.3em')
223
        .style('text-anchor', 'middle')
224
        .text(function(d) { return d.name.substring(0, d.r / 3); });
225
        // Look for callbacks potentially registered for third party code.
226
        jsondash.api.runCallbacks(container, config);
227
        jsondash.unload(container);
228
    });
229
230
    d3.select(self.frameElement).style("height", diameter + "px");
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
231
};
232
233
jsondash.handlers.handleTreemap = function(container, config) {
234
    'use strict';
235
    // Adapted from http://bl.ocks.org/mbostock/4063582
236
    var margin = {
0 ignored issues
show
Unused Code introduced by
The variable margin seems to be never used. Consider removing it.
Loading history...
237
        top: jsondash.config.WIDGET_MARGIN_Y / 2,
238
        bottom: jsondash.config.WIDGET_MARGIN_Y / 2,
239
        left: jsondash.config.WIDGET_MARGIN_X / 2,
240
        right: jsondash.config.WIDGET_MARGIN_X / 2
241
    };
242
    var width = config.width - jsondash.config.WIDGET_MARGIN_X;
243
    var height = config.height - jsondash.config.WIDGET_MARGIN_Y;
244
    var color = d3.scale.category20c();
245
    var treemap = d3.layout.treemap()
246
        .size([width, height])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
247
        .sticky(true)
248
        .value(function(d) { return d.size; });
249
    // Cleanup
250
    container.selectAll('.treemap').remove();
251
    var div = container
252
        .append('div')
253
        .classed({treemap: true, 'chart-centered': true})
254
        .style('position', 'relative')
255
        .style('width', width + 'px')
256
        .style('height', height + 'px');
257
258
    jsondash.getJSON(config.dataSource, function(error, root) {
259
        if(error) { throw new Error('Could not load url: ' + config.dataSource); }
260
        var node = div.datum(root).selectAll('.node')
261
            .data(treemap.nodes)
262
            .enter().append('div')
263
            .attr('class', 'node')
264
            .call(position)
265
            .style('border', '1px solid white')
266
            .style('font', '10px sans-serif')
267
            .style('line-height', '12px')
268
            .style('overflow', 'hidden')
269
            .style('position', 'absolute')
270
            .style('text-indent', '2px')
271
            .style('background', function(d) {
272
                return d.children ? color(d.name) : null;
273
            })
274
            .text(function(d) {
275
                return d.children ? null : d.name;
276
            });
277
        d3.selectAll('input').on('change', function change() {
278
            var value = this.value === 'count'
279
            ? function() { return 1; }
280
            : function(d) { return d.size;};
281
            node
282
            .data(treemap.value(value).nodes)
283
            .transition()
284
            .duration(1500)
285
            .call(position);
286
        });
287
        // Look for callbacks potentially registered for third party code.
288
        jsondash.api.runCallbacks(container, config);
289
        jsondash.unload(container);
290
    });
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
291
292
    function position() {
293
        this.style('left', function(d) { return d.x + 'px'; })
294
            .style('top', function(d) { return d.y + 'px'; })
295
            .style('width', function(d) { return Math.max(0, d.dx - 1) + 'px'; })
296
            .style('height', function(d) { return Math.max(0, d.dy - 1) + 'px'; });
297
    }
298
};
299
300
jsondash.handlers.handleRadialDendrogram = function(container, config) {
301
    'use strict';
302
    container.selectAll('svg').remove();
303
    // Code taken (and refactored for use here) from:
304
    // https://bl.ocks.org/mbostock/4339607
305
    var padding = 50;
306
    var radius = (config.width > config.height ? config.width : config.height) - padding;
307
    var cluster = d3.layout.cluster()
308
        .size([360, radius / 2 - 150]); // reduce size relative to `radius`
309
    var diagonal = d3.svg.diagonal.radial()
310
        .projection(function(d) { return [d.y, d.x / 180 * Math.PI]; });
311
    var svg = container.append('svg')
312
        .attr('width', radius)
313
        .attr('height', radius);
314
    var g = svg.append('g');
315
    g.attr('transform', 'translate(' + radius / 2 + ',' + radius / 2 + ')');
316
317
    jsondash.getJSON(config.dataSource, function(error, root) {
318
        if (error) { throw error; }
319
        var nodes = cluster.nodes(root);
320
        var link = g.selectAll('path.link')
0 ignored issues
show
Unused Code introduced by
The variable link seems to be never used. Consider removing it.
Loading history...
321
            .data(cluster.links(nodes))
322
            .enter().append('path')
323
            .attr('class', 'link')
324
            .attr('d', diagonal);
325
        var node = g.selectAll('g.node')
326
            .data(nodes)
327
            .enter().append('g')
328
            .attr('class', 'node')
329
            .attr('transform', function(d) { return 'rotate(' + (d.x - 90) + ')translate(' + d.y + ')'; });
330
        node.append('circle')
331
            .attr('r', 4.5);
332
        node.append('text')
333
            .attr('dy', '.31em')
334
            .attr('text-anchor', function(d) { return d.x < 180 ? 'start' : 'end'; })
335
            .attr('transform', function(d) { return d.x < 180 ? 'translate(8)' : 'rotate(180)translate(-8)'; })
336
            .text(function(d) { return d.name; });
337
        // Look for callbacks potentially registered for third party code.
338
        jsondash.api.runCallbacks(container, config);
339
        jsondash.unload(container);
340
    });
341
    d3.select(self.frameElement).style('height', radius * 2 + 'px');
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
342
};
343
344
jsondash.handlers.handleDendrogram = function(container, config) {
345
    'use strict';
346
    container.selectAll('svg').remove();
347
    // A general padding for the svg inside of the widget.
348
    // The cluster dendrogram will also need to have padding itself, so
349
    // the bounds are not clipped in the svg.
350
    var svg_pad = 20;
351
    var width = config.width - svg_pad;
352
    var height = config.height - svg_pad;
353
    var PADDING = width / 4;
354
    var cluster = d3.layout.cluster()
355
        .size([height * 0.85, width - PADDING]);
356
    var diagonal = d3.svg.diagonal()
357
        .projection(function(d) { return [d.y, d.x]; });
358
    var svg = container
359
        .append('svg')
360
        .attr('width', width)
361
        .attr('height', height);
362
    var g = svg.append('g')
363
        .attr('transform', 'translate(40, 0)');
364
365
    jsondash.getJSON(config.dataSource, function(error, root) {
366
        if(error) { throw new Error('Could not load url: ' + config.dataSource); }
367
368
        var nodes = cluster.nodes(root);
369
        var links = cluster.links(nodes);
370
        var link = g.selectAll('.link')
0 ignored issues
show
Unused Code introduced by
The variable link seems to be never used. Consider removing it.
Loading history...
371
        .data(links)
372
        .enter().append('path')
373
        .attr('class', 'link')
374
        .attr('d', diagonal);
375
376
        var node = g.selectAll('.node')
377
        .data(nodes)
378
        .enter().append('g')
379
        .attr('class', 'node')
380
        .attr('transform', function(d) { return 'translate(' + d.y + ',' + d.x + ')'; });
381
382
        node.append('circle').attr('r', 4.5);
383
        node.append('text')
384
        .attr('dx', function(d) { return d.children ? -8 : 8; })
385
        .attr('dy', 3)
386
        .style('text-anchor', function(d) { return d.children ? 'end' : 'start'; })
387
        .text(function(d) { return d.name; });
388
389
        // Look for callbacks potentially registered for third party code.
390
        jsondash.api.runCallbacks(container, config);
391
        jsondash.unload(container);
392
    });
393
};
394
395
jsondash.handlers.handleVoronoi = function(container, config) {
396
    'use strict';
397
    jsondash.getJSON(config.dataSource, function(error, data){
398
        if(error) { throw new Error('Could not load url: ' + config.dataSource); }
399
        var width = config.width - jsondash.config.WIDGET_MARGIN_X;
400
        var height = config.height - jsondash.config.WIDGET_MARGIN_Y;
401
        var vertices = data;
402
        var voronoi = d3.geom.voronoi().clipExtent([[0, 0], [width, height]]);
403
        // Cleanup
404
        var svg = container
405
        .append('svg')
406
        .attr('width', width)
407
        .attr('height', height);
408
        var path = svg.append('g').selectAll('path');
409
        svg.selectAll('circle')
410
        .data(vertices.slice(1))
411
        .enter().append('circle')
412
        .attr('transform', function(d) { return 'translate(' + d + ')'; })
413
        .attr('r', 1.5);
414
        redraw();
415
416
        function redraw() {
417
            path = path.data(voronoi(vertices), jsondash.util.polygon);
418
            path.exit().remove();
419
            path.enter().append('path')
420
            .attr('class', function(d, i) { return 'q' + (i % 9) + '-9'; })
421
            .attr('d', jsondash.util.polygon);
422
            path.order();
423
        }
424
        // Look for callbacks potentially registered for third party code.
425
        jsondash.api.runCallbacks(container, config);
426
        jsondash.unload(container);
427
    });
428
};
429
430
jsondash.handlers.handleSparkline = function(container, config) {
431
    'use strict';
432
    // Clean up old canvas elements
433
    container.selectAll('.sparkline-container').remove();
434
    var sparkline_type = config.type.split('-')[1];
435
    var spark = container
436
        .append('div')
437
        .classed({
438
            'sparkline-container': true,
439
            'text-center': true
440
        });
441
    spark = $(spark[0]);
442
    jsondash.getJSON(config.dataSource, function(data){
443
        var opts = {
444
            type: sparkline_type,
445
            width: config.width - jsondash.config.WIDGET_MARGIN_X,
446
            height: config.height - jsondash.config.WIDGET_MARGIN_Y
447
        };
448
        spark.sparkline(data, opts);
449
        // Look for callbacks potentially registered for third party code.
450
        jsondash.api.runCallbacks(container, config);
451
        jsondash.unload(container);
452
    });
453
};
454
455
jsondash.handlers.handleDataTable = function(container, config) {
456
    'use strict';
457
    // Clean up old tables if they exist, during reloading.
458
    container.selectAll('.dataTables_wrapper').remove();
459
    jsondash.getJSON(config.dataSource, function(error, res) {
460
        if(error) { throw new Error('Could not load url: ' + config.dataSource); }
461
        var keys = d3.keys(res[0]).map(function(d){
462
            return {data: d, title: d};
463
        });
464
        container
465
            .append('table')
466
            .classed({
467
                table: true,
468
                'table-striped': true,
469
                'table-bordered': true,
470
                'table-condensed': true
471
            });
472
        var opts = config.override ? res : {data: res, columns: keys};
473
        $(container.select('table')[0]).dataTable(opts).css({width: 'auto'});
474
        // Look for callbacks potentially registered for third party code.
475
        jsondash.api.runCallbacks(container, config);
476
        jsondash.unload(container);
477
    });
478
};
479
480
jsondash.handlers.handleSingleNum = function(container, config) {
481
    'use strict';
482
    container.selectAll('.singlenum').remove();
483
    jsondash.getJSON(config.dataSource, function(resp){
484
        var data = resp.data.data ? resp.data.data : resp.data;
485
        var num = container.append('div')
486
            .classed({singlenum: true})
487
            .text(data);
488
        data = String(data);
489
        // Add red or green, depending on if the number appears to be pos/neg.
490
        if(!resp.noformat) {
491
            num.classed({
492
                'text-danger': data.startsWith('-'),
493
                'text-success': !data.startsWith('-')
494
            });
495
        }
496
        // Allow custom colors.
497
        if(resp.color && resp.noformat) {
498
            num.style('color', resp.color);
499
        }
500
        // Get title height to offset box.
501
        var title_h = container
502
            .select('.widget-title')
503
            .node()
504
            .getBoundingClientRect()
505
            .height;
506
        var inner_box_height = config.height - title_h; // factor in rough height of title.
507
        num.style({
508
            'line-height': inner_box_height + 'px',
509
            height: inner_box_height + 'px'
510
        });
511
        var digits = String(data).length;
512
        var size = jsondash.util.getDigitSize()(digits);
513
        num.style('font-size', size + 'px');
514
        // Look for callbacks potentially registered for third party code.
515
        jsondash.api.runCallbacks(container, config);
516
        jsondash.unload(container);
517
    });
518
};
519
520
jsondash.handlers.handleTimeline = function(container, config) {
521
    'use strict';
522
    jsondash.getJSON(config.dataSource, function(data){
523
        container.append('div').attr('id', 'widget-' + config.guid);
524
        var timeline = new TL.Timeline('widget-' + config.guid, data);
0 ignored issues
show
Unused Code introduced by
The variable timeline seems to be never used. Consider removing it.
Loading history...
Bug introduced by
The variable TL seems to be never declared. If this is a global, consider adding a /** global: TL */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
525
        // Look for callbacks potentially registered for third party code.
526
        jsondash.api.runCallbacks(container, config);
527
        jsondash.unload(container);
528
    });
529
};
530
531
jsondash.handlers.handleIframe = function(container, config) {
532
    'use strict';
533
    container.selectAll('iframe').remove();
534
    var iframe = container.append('iframe');
535
    iframe.attr({
536
        border: 0,
537
        src: config.dataSource,
538
        height: '100%',
539
        width: '100%'
540
    });
541
    // Look for callbacks potentially registered for third party code.
542
    jsondash.api.runCallbacks(container, config);
543
    jsondash.unload(container);
544
};
545
546
jsondash.handlers.handleCustom = function(container, config) {
547
    'use strict';
548
    container.selectAll('.custom-container').remove();
549
    $.get(config.dataSource, function(html){
550
        container.append('div').classed({'custom-container': true}).html(html);
551
        // Look for callbacks potentially registered for third party code.
552
        jsondash.api.runCallbacks(container, config);
553
        jsondash.unload(container);
554
    });
555
};
556
557
jsondash.handlers.handleVenn = function(container, config) {
558
    'use strict';
559
    container.selectAll('.venn').remove();
560
    jsondash.getJSON(config.dataSource, function(error, data){
561
        if(error) { throw new Error('Could not load url: ' + config.dataSource); }
562
        var chart = venn.VennDiagram();
563
        var cont = container
564
            .append('div')
565
            .classed({venn: true});
566
        cont.datum(data).call(chart);
567
        cont.select('svg')
568
            .attr('width', config.width - jsondash.config.WIDGET_MARGIN_X)
569
            .attr('height', config.height - jsondash.config.WIDGET_MARGIN_Y);
570
        // Look for callbacks potentially registered for third party code.
571
        jsondash.api.runCallbacks(container, config);
572
        jsondash.unload(container);
573
    });
574
};
575
576
jsondash.handlers.handlePlotly = function(container, config) {
577
    'use strict';
578
    var id = 'plotly-' + config.guid;
579
    container.selectAll('.plotly-container').remove();
580
    container.append('div')
581
        .classed({'plotly-container': true})
582
        .attr('id', id);
583
    jsondash.getJSON(config.dataSource, function(error, data){
584
        if(error) { throw new Error('Could not load url: ' + config.dataSource); }
585
        if(config.override) {
586
            if(data.layout && data.layout.margin) {
587
                // Remove margins, they mess up the
588
                // layout and are already accounted for.
589
                delete data.layout['margin'];
590
            }
591
            Plotly.plot(id, data.data, data.layout || {}, data.options || {});
592
        } else {
593
            Plotly.plot(id, data);
594
        }
595
        d3.select('#' + id).select('.svg-container').style({'margin': '0 auto'})
596
        // Look for callbacks potentially registered for third party code.
597
        jsondash.api.runCallbacks(container, config);
598
        jsondash.unload(container);
599
    });
600
};
601