Completed
Push — master ( f04723...014a0d )
by Chris
01:14
created

jsondash.handlers.handlePlotly   B

Complexity

Conditions 4
Paths 20

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
c 2
b 1
f 0
dl 0
loc 23
rs 8.7972
nc 20
nop 2
1
/** global: jsondash */
2
/** global: c3 */
3
/** global: d3 */
4
/** global: venn */
5
/** global: Plotly */
6
7
jsondash.getJSON = function(container, url, callback) {
8
    if(!url) throw new Error('Invalid URL: ' + url);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
9
    d3.json(url, function(error, data){
10
        if(error) {
11
            jsondash.unload(container);
12
            throw new Error("Could not load url: " + url);
13
        }
14
        callback(error, data);
15
    });
16
};
17
18
/**
19
 * Handlers for various widget types. The method signatures are always the same,
20
 * but each handler can handle them differently.
21
 */
22
23
jsondash.handlers.handleYoutube = function(container, config) {
24
    // Clean up all previous.
25
    'use strict';
26
    container.selectAll('iframe').remove();
27
28
    function getAttr(prop, props) {
29
        // Return the propery from a list of properties for the iframe.
30
        // e.g. getAttr('width', ["width="900""]) --> "900"
31
        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...
32
            return k.startsWith(prop);
33
        })[0];
34
    }
35
36
37
    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...
38
    var parts = config.dataSource.split(' ');
39
    var yt_width = parseInt(getAttr('width', parts).split('=')[1].replace(/"/gi, ''), 10);
40
    var height = parseInt(getAttr('height', parts).split('=')[1].replace(/"/gi, ''), 10);
41
    var width = isNaN(config.width) ? '100%' : yt_width;
42
    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 37. 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...
43
44
    // In the case of YouTube, we have to override the config dimensions
45
    // as this will be wonky when the aspect ratio is calculated. We will
46
    // defer to YouTube calculations instead.
47
    container.append('iframe')
48
        .attr('width', width)
49
        .attr('height', height)
50
        .attr('src', url)
51
        .attr('allowfullscreen', true)
52
        .attr('frameborder', 0);
53
    // Look for callbacks potentially registered for third party code.
54
    jsondash.api.runCallbacks(container, config);
55
    jsondash.unload(container);
56
};
57
58
/**
59
 * [handleGraph creates graphs using the dot format
60
 * spec with d3 and dagre-d3]
61
 */
62
jsondash.handlers.handleGraph = function(container, config) {
63
    'use strict';
64
    jsondash.getJSON(container, config.dataSource, function(error, data){
65
        container.selectAll('.chart-graph').remove();
66
        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...
67
        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...
68
        var svg = container.append('svg').classed({'chart-graph': true});
69
        var svg_group = svg.append('g');
70
        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...
71
        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...
72
        // Create the renderer
73
        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...
74
        render(svg_group, g);
75
        bbox = svg.node().getBBox();
76
        svg.attr('width', bbox.width)
77
            .attr('height', bbox.height);
78
        // Look for callbacks potentially registered for third party code.
79
        jsondash.api.runCallbacks(container, config);
80
        jsondash.unload(container);
81
    });
82
};
83
84
/**
85
 * [handleWordCloud create word clouds using the d3-cloud extension.]
86
 */
87
jsondash.handlers.handleWordCloud = function(container, config) {
88
    'use strict';
89
    jsondash.getJSON(container, config.dataSource, function(error, data){
90
        container.selectAll('.wordcloud').remove();
91
        var h     = config.height - jsondash.config.WIDGET_MARGIN_Y;
92
        var w     = config.width - jsondash.config.WIDGET_MARGIN_X;
93
        var svg   = container.append('svg').classed({'wordcloud': true});
94
        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...
95
        var cloud = d3.layout.cloud;
96
        var words = data.map(function(d) {
97
            return {text: d.text, size: d.size};
98
        });
99
        var layout = cloud()
100
            .size([w, h])
101
            .words(words)
102
            .padding(4)
103
            .rotate(function() {return ~~(Math.random() * 1) * 90;})
104
            .font('Arial')
105
            .fontSize(function(d) {return d.size;})
106
            .on('end', draw);
107
108
        layout.start();
109
110
        function draw(words) {
111
          svg
112
              .attr('width', layout.size()[0])
113
              .attr('height', layout.size()[1])
114
            .append('g')
115
              .attr('transform', 'translate(' + layout.size()[0] / 2 + ',' + layout.size()[1] / 2 + ')')
116
            .selectAll('text').data(words)
117
            .enter().append('text')
118
              .style('font-size', function(d) { return d.size + 'px'; })
119
              .style('font-family', 'arial')
120
              .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...
121
              .attr('text-anchor', 'middle')
122
              .attr('transform', function(d) {
123
                return 'translate(' + [d.x, d.y] + ')rotate(' + d.rotate + ')';
124
              })
125
              .text(function(d) { return d.text; });
126
        }
127
128
        // Look for callbacks potentially registered for third party code.
129
        jsondash.api.runCallbacks(container, config);
130
        jsondash.unload(container);
131
    });
132
};
133
134
jsondash.handlers.handleC3 = function(container, config) {
135
    'use strict';
136
    var init_config = {
137
        bindto: '[data-guid="' + config.guid + '"] .chart-container',
138
        legend: {
139
            show: true
140
        },
141
        size: {
142
            height: config.height - jsondash.config.WIDGET_MARGIN_Y,
143
            width: config.width - jsondash.config.WIDGET_MARGIN_X
144
        },
145
        data: {
146
            type: config.type,
147
            url: config.dataSource,
148
            mimeType: 'json'
149
        },
150
        onrendered: function(){
151
            // Look for callbacks potentially registered for third party code.
152
            jsondash.api.runCallbacks(container, config);
153
            jsondash.unload(container);
154
        }
155
    };
156
    if(jsondash.util.isOverride(config)) {
157
        // Just use the raw payload for this widgets' options.
158
        jsondash.getJSON(container, config.dataSource, function(error, data){
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 = isNaN(config.width) ? config.height - margin : d3.max([config.width, config.height]);
194
    var format = d3.format(',d');
195
    var pack = d3.layout.pack()
196
        .size([diameter, diameter])
197
        .value(function(d) { return d.size; });
198
199
    var svg = container
200
        .append('svg')
201
        .attr('width', diameter)
202
        .attr('height', diameter)
203
        .append('g');
204
205
    jsondash.getJSON(container, config.dataSource, function(error, data) {
206
        var node = svg.datum(data).selectAll('.node')
207
        .data(pack.nodes)
208
        .enter().append('g')
209
        .attr('class', function(d) { return d.children ? 'node' : 'leaf node'; })
210
        .attr('transform', function(d) { return 'translate(' + d.x + ',' + d.y + ')'; });
211
212
        node.append('title')
213
        .text(function(d) { return d.name + (d.children ? '' : ': ' + format(d.size)); });
214
215
        node.append('circle')
216
        .attr('r', function(d) { return d.r; });
217
218
        node.filter(function(d) { return !d.children; }).append('text')
219
        .attr('dy', '.3em')
220
        .style('text-anchor', 'middle')
221
        .text(function(d) { return d.name.substring(0, d.r / 3); });
222
        // Look for callbacks potentially registered for third party code.
223
        jsondash.api.runCallbacks(container, config);
224
        jsondash.unload(container);
225
    });
226
227
    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...
228
};
229
230
jsondash.handlers.handleTreemap = function(container, config) {
231
    'use strict';
232
    // Adapted from http://bl.ocks.org/mbostock/4063582
233
    var margin = {
0 ignored issues
show
Unused Code introduced by
The variable margin seems to be never used. Consider removing it.
Loading history...
234
        top: jsondash.config.WIDGET_MARGIN_Y / 2,
235
        bottom: jsondash.config.WIDGET_MARGIN_Y / 2,
236
        left: jsondash.config.WIDGET_MARGIN_X / 2,
237
        right: jsondash.config.WIDGET_MARGIN_X / 2
238
    };
239
    var _width = isNaN(config.width) ? config.height : config.width;
240
    var width = _width - jsondash.config.WIDGET_MARGIN_X;
241
    var height = config.height - jsondash.config.WIDGET_MARGIN_Y;
242
    var color = d3.scale.category20c();
243
    var treemap = d3.layout.treemap()
244
        .size([width, height])
245
        .sticky(true)
246
        .value(function(d) { return d.size; });
247
    // Cleanup
248
    container.selectAll('.treemap').remove();
249
    var div = container
250
        .append('div')
251
        .classed({treemap: true, 'chart-centered': true})
252
        .style('position', 'relative')
253
        .style('width', width + 'px')
254
        .style('height', height + 'px');
255
256
    jsondash.getJSON(container, config.dataSource, function(error, root) {
257
        var node = div.datum(root).selectAll('.node')
258
            .data(treemap.nodes)
259
            .enter().append('div')
260
            .attr('class', 'node')
261
            .call(position)
262
            .style('border', '1px solid white')
263
            .style('font', '10px sans-serif')
264
            .style('line-height', '12px')
265
            .style('overflow', 'hidden')
266
            .style('position', 'absolute')
267
            .style('text-indent', '2px')
268
            .style('background', function(d) {
269
                return d.children ? color(d.name) : null;
270
            })
271
            .text(function(d) {
272
                return d.children ? null : d.name;
273
            });
274
        d3.selectAll('input').on('change', function change() {
275
            var value = this.value === 'count'
276
            ? function() { return 1; }
277
            : function(d) { return d.size;};
278
            node
279
            .data(treemap.value(value).nodes)
280
            .transition()
281
            .duration(1500)
282
            .call(position);
283
        });
284
        // Look for callbacks potentially registered for third party code.
285
        jsondash.api.runCallbacks(container, config);
286
        jsondash.unload(container);
287
    });
288
289
    function position() {
290
        this.style('left', function(d) { return d.x + 'px'; })
291
            .style('top', function(d) { return d.y + 'px'; })
292
            .style('width', function(d) { return Math.max(0, d.dx - 1) + 'px'; })
293
            .style('height', function(d) { return Math.max(0, d.dy - 1) + 'px'; });
294
    }
295
};
296
297
jsondash.handlers.handleRadialDendrogram = function(container, config) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
298
    'use strict';
299
    container.selectAll('svg').remove();
300
    // Code taken (and refactored for use here) from:
301
    // https://bl.ocks.org/mbostock/4339607
302
    var _width = isNaN(config.width) ? config.height : config.width;
303
    var padding = 50;
304
    var radius = (_width > config.height ? _width : config.height) - padding;
305
    var cluster = d3.layout.cluster()
306
        .size([360, radius / 2 - 150]); // reduce size relative to `radius`
307
    var diagonal = d3.svg.diagonal.radial()
308
        .projection(function(d) { return [d.y, d.x / 180 * Math.PI]; });
309
    var svg = container.append('svg')
310
        .attr('width', radius)
311
        .attr('height', radius);
312
    var g = svg.append('g');
313
    g.attr('transform', 'translate(' + radius / 2 + ',' + radius / 2 + ')');
314
315
    jsondash.getJSON(container, config.dataSource, function(error, root) {
316
        if (error) { throw error; }
317
        var nodes = cluster.nodes(root);
318
        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...
319
            .data(cluster.links(nodes))
320
            .enter().append('path')
321
            .attr('class', 'link')
322
            .attr('d', diagonal);
323
        var node = g.selectAll('g.node')
324
            .data(nodes)
325
            .enter().append('g')
326
            .attr('class', 'node')
327
            .attr('transform', function(d) { return 'rotate(' + (d.x - 90) + ')translate(' + d.y + ')'; });
328
        node.append('circle')
329
            .attr('r', 4.5);
330
        node.append('text')
331
            .attr('dy', '.31em')
332
            .attr('text-anchor', function(d) { return d.x < 180 ? 'start' : 'end'; })
333
            .attr('transform', function(d) { return d.x < 180 ? 'translate(8)' : 'rotate(180)translate(-8)'; })
334
            .text(function(d) { return d.name; });
335
        // Look for callbacks potentially registered for third party code.
336
        jsondash.api.runCallbacks(container, config);
337
        jsondash.unload(container);
338
    });
339
    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...
340
};
341
342
jsondash.handlers.handleDendrogram = function(container, config) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
343
    'use strict';
344
    container.selectAll('svg').remove();
345
    var _width = isNaN(config.width) ? container.node().getBoundingClientRect().width : config.width;
346
    // A general padding for the svg inside of the widget.
347
    // The cluster dendrogram will also need to have padding itself, so
348
    // the bounds are not clipped in the svg.
349
    var svg_pad = 20;
350
    var width = _width - svg_pad;
351
    var height = config.height - svg_pad;
352
    var PADDING = width / 4;
353
    var cluster = d3.layout.cluster()
354
        .size([height * 0.85, width - PADDING]);
355
    var diagonal = d3.svg.diagonal()
356
        .projection(function(d) { return [d.y, d.x]; });
357
    var svg = container
358
        .append('svg')
359
        .attr('width', width)
360
        .attr('height', height);
361
    var g = svg.append('g')
362
        .attr('transform', 'translate(40, 0)');
363
364
    jsondash.getJSON(container, config.dataSource, function(error, root) {
365
        var nodes = cluster.nodes(root);
366
        var links = cluster.links(nodes);
367
        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...
368
        .data(links)
369
        .enter().append('path')
370
        .attr('class', 'link')
371
        .attr('d', diagonal);
372
373
        var node = g.selectAll('.node')
374
        .data(nodes)
375
        .enter().append('g')
376
        .attr('class', 'node')
377
        .attr('transform', function(d) { return 'translate(' + d.y + ',' + d.x + ')'; });
378
379
        node.append('circle').attr('r', 4.5);
380
        node.append('text')
381
        .attr('dx', function(d) { return d.children ? -8 : 8; })
382
        .attr('dy', 3)
383
        .style('text-anchor', function(d) { return d.children ? 'end' : 'start'; })
384
        .text(function(d) { return d.name; });
385
386
        // Look for callbacks potentially registered for third party code.
387
        jsondash.api.runCallbacks(container, config);
388
        jsondash.unload(container);
389
    });
390
};
391
392
jsondash.handlers.handleVoronoi = function(container, config) {
393
    'use strict';
394
    jsondash.getJSON(container, config.dataSource, function(error, data){
395
        var _width   = isNaN(config.width) ? container.node().getBoundingClientRect().width : config.width;
396
        var width    = _width - jsondash.config.WIDGET_MARGIN_X;
397
        var height   = config.height - jsondash.config.WIDGET_MARGIN_Y;
398
        var vertices = data;
399
        var voronoi  = d3.geom.voronoi().clipExtent([[0, 0], [width, height]]);
400
        // Cleanup
401
        container.selectAll('svg').remove();
402
        var svg = container
403
            .append('svg')
404
            .attr('width', width)
405
            .attr('height', height);
406
        var path = svg.append('g').selectAll('path');
407
        svg.selectAll('circle')
408
            .data(vertices.slice(1))
409
            .enter().append('circle')
410
            .attr('transform', function(d) { return 'translate(' + d + ')'; })
411
            .attr('r', 1.5);
412
            redraw();
413
414
        function redraw() {
415
            path = path.data(voronoi(vertices), jsondash.util.polygon);
416
            path.exit().remove();
417
            path.enter().append('path')
418
            .attr('class', function(d, i) { return 'q' + (i % 9) + '-9'; })
419
            .attr('d', jsondash.util.polygon);
420
            path.order();
421
        }
422
        // Look for callbacks potentially registered for third party code.
423
        jsondash.api.runCallbacks(container, config);
424
        jsondash.unload(container);
425
    });
426
};
427
428
jsondash.handlers.handleSparkline = function(container, config) {
429
    'use strict';
430
    // Clean up old canvas elements
431
    container.selectAll('.sparkline-container').remove();
432
    var sparkline_type = config.type.split('-')[1];
433
    var spark = container
434
        .append('div')
435
        .classed({
436
            'sparkline-container': true,
437
            'text-center': true
438
        });
439
    spark = $(spark[0]);
440
    jsondash.getJSON(container, config.dataSource, function(data){
441
        var opts = {
442
            type: sparkline_type,
443
            width: config.width - jsondash.config.WIDGET_MARGIN_X,
444
            height: config.height - jsondash.config.WIDGET_MARGIN_Y
445
        };
446
        spark.sparkline(data, opts);
447
        // Look for callbacks potentially registered for third party code.
448
        jsondash.api.runCallbacks(container, config);
449
        jsondash.unload(container);
450
    });
451
};
452
453
jsondash.handlers.handleDataTable = function(container, config) {
454
    'use strict';
455
    // Clean up old tables if they exist, during reloading.
456
    container.selectAll('.dataTables_wrapper').remove();
457
    jsondash.getJSON(container, config.dataSource, function(error, res) {
458
        var keys = d3.keys(res[0]).map(function(d){
459
            return {data: d, title: d};
460
        });
461
        container
462
            .append('table')
463
            .classed({
464
                table: true,
465
                'table-striped': true,
466
                'table-bordered': true,
467
                'table-condensed': true
468
            });
469
        var opts = config.override ? res : {data: res, columns: keys};
470
        $(container.select('table')[0]).dataTable(opts).css({width: 'auto'});
471
        // Look for callbacks potentially registered for third party code.
472
        jsondash.api.runCallbacks(container, config);
473
        jsondash.unload(container);
474
    });
475
};
476
477
jsondash.handlers.handleSingleNum = function(container, config) {
478
    'use strict';
479
    container.selectAll('.singlenum').remove();
480
    jsondash.getJSON(container, config.dataSource, function(resp){
481
        var data = resp.data.data ? resp.data.data : resp.data;
482
        var num = container.select('.chart-container').append('div')
483
            .classed({singlenum: true})
484
            .text(data);
485
        data = String(data);
486
        // Add red or green, depending on if the number appears to be pos/neg.
487
        if(!resp.noformat) {
488
            num.classed({
489
                'text-danger': data.startsWith('-'),
490
                'text-success': !data.startsWith('-')
491
            });
492
        }
493
        // Allow custom colors.
494
        if(resp.color && resp.noformat) {
495
            num.style('color', resp.color);
496
        }
497
        // Get title height to offset box.
498
        var title_h = container
0 ignored issues
show
Unused Code introduced by
The variable title_h seems to be never used. Consider removing it.
Loading history...
499
            .select('.widget-title')
500
            .node()
501
            .getBoundingClientRect()
502
            .height;
503
        var h = config.height - jsondash.config.WIDGET_MARGIN_Y;
504
        num.style({
505
            'line-height': h + 'px',
506
            height: h + 'px',
507
            width: config.width - jsondash.config.WIDGET_MARGIN_X
508
        });
509
        var digits = String(data).length;
510
        var size = jsondash.util.getDigitSize()(digits);
511
        num.style('font-size', size + 'px');
512
        // Look for callbacks potentially registered for third party code.
513
        jsondash.api.runCallbacks(container, config);
514
        jsondash.unload(container);
515
    });
516
};
517
518
jsondash.handlers.handleTimeline = function(container, config) {
519
    'use strict';
520
    jsondash.getJSON(container, config.dataSource, function(data){
521
        container.append('div').attr('id', 'widget-' + config.guid);
522
        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...
523
        // Look for callbacks potentially registered for third party code.
524
        jsondash.api.runCallbacks(container, config);
525
        jsondash.unload(container);
526
    });
527
};
528
529
jsondash.handlers.handleIframe = function(container, config) {
530
    'use strict';
531
    container.selectAll('iframe').remove();
532
    var iframe = container.append('iframe');
533
    iframe.attr({
534
        border: 0,
535
        src: config.dataSource,
536
        height: config.height - jsondash.config.WIDGET_MARGIN_Y,
537
        width: config.width - jsondash.config.WIDGET_MARGIN_X
538
    });
539
    // Look for callbacks potentially registered for third party code.
540
    jsondash.api.runCallbacks(container, config);
541
    jsondash.unload(container);
542
};
543
544
jsondash.handlers.handleCustom = function(container, config) {
545
    'use strict';
546
    container.selectAll('.custom-container').remove();
547
    $.get(config.dataSource, function(html){
548
        container.append('div').classed({'custom-container': true}).html(html);
549
        // Look for callbacks potentially registered for third party code.
550
        jsondash.api.runCallbacks(container, config);
551
        jsondash.unload(container);
552
    });
553
};
554
555
jsondash.handlers.handleVenn = function(container, config) {
556
    'use strict';
557
    container.selectAll('.venn').remove();
558
    jsondash.getJSON(container, config.dataSource, function(error, data){
559
        var chart = venn.VennDiagram();
560
        var cont = container
561
            .append('div')
562
            .classed({venn: true});
563
        cont.datum(data).call(chart);
564
        cont.select('svg')
565
            .attr('width', config.width - jsondash.config.WIDGET_MARGIN_X)
566
            .attr('height', config.height - jsondash.config.WIDGET_MARGIN_Y);
567
        // Look for callbacks potentially registered for third party code.
568
        jsondash.api.runCallbacks(container, config);
569
        jsondash.unload(container);
570
    });
571
};
572
573
jsondash.handlers.handlePlotly = function(container, config) {
574
    'use strict';
575
    var id = 'plotly-' + config.guid;
576
    container.selectAll('.plotly-container').remove();
577
    container.append('div')
578
        .classed({'plotly-container': true})
579
        .attr('id', id);
580
    jsondash.getJSON(container, config.dataSource, function(error, data){
581
        var plotly_wrapper =  d3.select('#' + id);
582
        delete data.layout.height;
583
        delete data.layout.width;
584
        data.layout.margin = {l: 20, r: 20, b: 20, t: 50};
585
        if(config.override) {
586
            Plotly.plot(id, data.data, data.layout || {}, data.options || {});
587
        } else {
588
            Plotly.plot(id, data);
589
        }
590
       plotly_wrapper.select('.svg-container').style({
591
            'margin': '0 auto',
592
            'width': isNaN(config.width) ? '100%' : config.width,
593
            'height': config.height
594
        });
595
        plotly_wrapper.select('#scene').style({
596
            'width': isNaN(config.width) ? '100%' : config.width,
597
            'height': config.height
598
        });
599
        // Look for callbacks potentially registered for third party code.
600
        jsondash.api.runCallbacks(container, config);
601
        jsondash.unload(container);
602
    });
603
};
604