Passed
Push — master ( c83a47...2e74fa )
by Andreas
26:58
created

static/org.openpsa.reports/chart.js   A

Complexity

Total Complexity 8
Complexity/F 1.6

Size

Lines of Code 51
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 8
mnd 3
bc 3
fnc 5
bpm 0.6
cpm 1.6
noi 0
1
function init_chart(grid_id) {
2
3
    const ctx = document.getElementById('chart-' + grid_id);
4
    var chart;
5
6
    function render_grid() {
7
        let group_data = $('#' + grid_id).jqGrid('getGridParam', 'groupingView'),
8
        chart_labels = [],
9
        chart_data = [];
10
11
        group_data.groups.forEach(function(group) {
12
            chart_labels.push(group.value.replace(/<\/?[^>]+(>|$)/g, ""));
13
            chart_data.push(group.summary[0].v);
14
        });
15
16
        var label = '';
17
        $('#' + grid_id).jqGrid('getGridParam', 'colModel').forEach(function(col, index) {
18
            if (col.name == 'sum') {
19
                label = $('#' + grid_id).jqGrid('getGridParam', 'colNames')[index];
20
            }
21
        });
22
23
        chart = new Chart(ctx, {
24
        type: 'bar',
25
        data: {
26
            labels: chart_labels,
27
            datasets: [{
28
                label: label,
29
                data: chart_data,
30
                borderWidth: 1
31
            }]
32
        },
33
        options: {
34
            scales: {
35
            y: {
36
                beginAtZero: true
37
            }
38
            }
39
        }
40
        });
41
    }
42
43
    $('#chgrouping_' + grid_id).on('change', function() {
44
        if (chart) {
45
            chart.destroy();
46
        }
47
        if ($('#' + grid_id).jqGrid('getGridParam', 'grouping')) {
48
            render_grid();
49
        }
50
    }).trigger('change');
51
}