Completed
Push — master ( 2d87c5...932c46 )
by Chris
01:23
created

jsondash.getDiameter   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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