Completed
Push — master ( 71d9eb...368b76 )
by Chris
01:25
created

jsondash.handlers.handleSigma   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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