Passed
Push — master ( 192056...6c6baa )
by Marcel
05:20 queued 14s
created

js/appMin.js   A

Complexity

Total Complexity 11
Complexity/F 1.38

Size

Lines of Code 143
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 98
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 11
mnd 3
bc 3
fnc 8
bpm 0.375
cpm 1.375
noi 2
1
/**
2
 * Analytics
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the LICENSE.md file.
6
 *
7
 * @author Marcel Scherello <[email protected]>
8
 * @copyright 2019-2022 Marcel Scherello
9
 */
10
/** global: OCA */
11
/** global: OCP */
12
/** global: OC */
13
/** global: table */
14
/** global: Chart */
15
/** global: cloner */
16
/** global: _ */
17
18
'use strict';
19
let OCA;
20
OCA = {};
21
22
if (!OCA.Analytics) {
23
    /**
24
     * @namespace
25
     */
26
    OCA.Analytics = {
27
        initialDocumentTitle: null,
28
        isAdvanced: false,
29
        currentReportData: {},
30
        chartObject: null,
31
        // flexible mapping depending on type required by the used chart library
32
        chartTypeMapping: {
33
            'datetime': 'line',
34
            'column': 'bar',
35
            'columnSt': 'bar', // map stacked type also to base type; needed in filter
36
            'columnSt100': 'bar', // map stacked type also to base type; needed in filter
37
            'area': 'line',
38
            'line': 'line',
39
            'doughnut': 'doughnut'
40
        },
41
        datasources: [],
42
        datasourceOptions: [],
43
        datasets: [],
44
        reports: [],
45
        unsavedFilters: null,
46
        refreshTimer: null,
47
        currentXhrRequest: null,
48
    };
49
}
50
51
OCA.Analytics.UI = {
52
53
    initApplication: function () {
54
        OCA.Analytics.Visualization.hideElement('analytics-intro');
55
        OCA.Analytics.Visualization.hideElement('analytics-content');
56
        OCA.Analytics.Visualization.hideElement('analytics-loading');
57
        OCA.Analytics.Visualization.showElement('analytics-content');
58
        document.getElementById('chartContainer').innerHTML = '';
59
        document.getElementById('chartContainer').innerHTML = '<button id="chartZoomReset" hidden>Reset Zoom</button><canvas id="myChart" ></canvas>';
60
        document.getElementById('chartZoomReset').addEventListener('click', OCA.Analytics.UI.handleZoomResetButton);
61
62
        OCA.Analytics.currentReportData = JSON.parse(document.getElementById('data').value);
63
        // if the user uses a special time parser (e.g. DD.MM), the data needs to be sorted differently
64
        OCA.Analytics.currentReportData = OCA.Analytics.Visualization.sortDates(OCA.Analytics.currentReportData);
65
        OCA.Analytics.currentReportData.data = OCA.Analytics.Visualization.formatDates(OCA.Analytics.currentReportData.data);
66
67
        let ctx = document.getElementById('myChart').getContext('2d');
68
        OCA.Analytics.Visualization.buildChart(ctx, OCA.Analytics.currentReportData, OCA.Analytics.UI.getDefaultChartOptions());
69
    },
70
71
    getDefaultChartOptions: function () {
72
        return {
73
            maintainAspectRatio: false,
74
            responsive: true,
75
            scales: {
76
                'primary': {
77
                    type: 'linear',
78
                    stacked: false,
79
                    position: 'left',
80
                    display: true,
81
                    grid: {
82
                        display: true,
83
                    },
84
                    ticks: {
85
                        callback: function (value) {
86
                            return value.toLocaleString();
87
                        },
88
                    },
89
                },
90
                'secondary': {
91
                    type: 'linear',
92
                    stacked: false,
93
                    position: 'right',
94
                    display: false,
95
                    grid: {
96
                        display: false,
97
                    },
98
                    ticks: {
99
                        callback: function (value) {
100
                            return value.toLocaleString();
101
                        },
102
                    },
103
                },
104
                'xAxes': {
105
                    type: 'category',
106
                    time: {
107
                        parser: 'YYYY-MM-DD HH:mm',
108
                        tooltipFormat: 'LL',
109
                    },
110
                    distribution: 'linear',
111
                    grid: {
112
                        display: false
113
                    },
114
                    display: true,
115
                },
116
            },
117
            animation: {
118
                duration: 0 // general animation time
119
            },
120
121
            tooltips: {
122
                callbacks: {
123
                    label: function (tooltipItem, data) {
124
//                        let datasetLabel = data.datasets[tooltipItem.datasetIndex].label || '';
125
                        let datasetLabel = data.datasets[tooltipItem.datasetIndex].label || data.labels[tooltipItem.index];
126
                        if (tooltipItem.yLabel !== '') {
127
                            return datasetLabel + ': ' + parseFloat(tooltipItem.yLabel).toLocaleString();
128
                        } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
129
                            return datasetLabel;
130
                        }
131
                    }
132
                }
133
            },
134
135
            plugins: {
136
                datalabels: {
137
                    display: false,
138
                    formatter: (value, ctx) => {
139
                        let sum = 0;
140
                        let dataArr = ctx.chart.data.datasets[0].data;
141
                        dataArr.map(data => {
142
                            sum += data;
143
                        });
144
                        value = (value * 100 / sum).toFixed(0);
145
                        if (value > 5) {
146
                            return value + "%";
147
                        } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
148
                            return '';
149
                        }
150
                    },
151
                },
152
            },
153
        };
154
    },
155
}
156
157
document.addEventListener('DOMContentLoaded', function () {
158
    OCA.Analytics.UI.initApplication();
159
    OCA.Analytics.Visualization.hideElement('analytics-warning');
160
});