DrawChartHelper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 269
Duplicated Lines 7.43 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 20
loc 269
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A makeChart() 0 11 2
A makeSingleTraceChart() 0 11 1
B makeMultiTraceChart() 0 23 5
A simpleBarChart() 0 9 1
A groupedBarChart() 10 10 1
A stackedBarChart() 10 10 1
A pieChart() 0 9 1
A singleLineChart() 0 10 1
A multilineChart() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Copyright 2017, Jean Traullé <[email protected]>
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2017, Jean Traullé <[email protected]>
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeCharts\View\Helper;
13
14
use CakeCharts\Utility\Trace;
15
use CakeCharts\Utility\Traces;
16
use Cake\View\Helper;
17
18
/**
19
 * DrawChart helper
20
 * @package CakeCharts\View\Helper
21
 */
22
class DrawChartHelper extends Helper
23
{
24
25
    /**
26
     * Primary function responsible for the generation of every chart
27
     *
28
     * @param Traces $traces Traces object is an array of Trace objects
29
     * @see \CakeCharts\Utility\Traces
30
     * @param array $layout Any layout option accepted by Plotly.js
31
     * @see https://plot.ly/javascript/#layout-options for possible values and examples
32
     * @param array $configuration Any configuration option accepted by Plotly.js
33
     * @see https://plot.ly/javascript/configuration-options for possible values and examples
34
     * @param string|null $id HTML identifier of div where chart will be drawed
35
     * @return string The generated chart
36
     */
37
    private function makeChart(Traces $traces, array $layout = [], array $configuration = [], string $id = null)
38
    {
39
        $id = ($id === null) ? uniqid('chart_') : $id;
40
41
        return $this->_View->element('CakeCharts.Chart', [
42
            'id' => $id,
43
            'data' => $traces,
44
            'layout' => json_encode($layout, JSON_FORCE_OBJECT),
45
            'configuration' => json_encode($configuration, JSON_FORCE_OBJECT)
46
        ]);
47
    }
48
49
    /**
50
     * Function used to generate single series charts
51
     *
52
     * @param array $x Values to be placed on X axis
53
     * @param array $y Values to be placed on Y axis
54
     * @param string $type Type of trace : can be either "bar", "pie" or "scatter"
55
     * @param string|null $mode Line type : can be either "markers", "lines" or "markers+line"
56
     * @param array $layout Any layout option accepted by Plotly.js
57
     * @param array $configuration Any configuration option accepted by Plotly.js
58
     * @param string|null $id HTML identifier of div where chart will be drawed
59
     * @return string The generated chart
60
     * @see https://plot.ly/javascript/#layout-options for possible values and examples
61
     * @see https://plot.ly/javascript/configuration-options for possible values and examples
62
     */
63
    public function makeSingleTraceChart(
64
        array $x,
65
        array $y,
66
        string $type,
67
        string $mode = null,
68
        array $layout = [],
69
        array $configuration = [],
70
        string $id = null
71
    ) {
72
        return $this->makeChart(new Traces([new Trace($x, $y, $type, $mode)]), $layout, $configuration, $id);
73
    }
74
75
    /**
76
     * Function used to generate multi series charts
77
     *
78
     * @param array $series Multi-dimensional array of series
79
     *    $series = [
80
     *       [
81
     *          (array) X values,
82
     *          (array) Y values,
83
     *          (opt. string) Name of the series,
84
     *          (opt. string) Line type ("markers", "lines" or "markers+line")
85
     *       ], [...], [...]
86
     *    ]
87
     * @param string $type Type of trace : can be either "bar", "pie" or "scatter"
88
     * @param array $layout Any layout option accepted by Plotly.js
89
     * @see https://plot.ly/javascript/#layout-options for possible values and examples
90
     * @param array $configuration Any configuration option accepted by Plotly.js
91
     * @see https://plot.ly/javascript/configuration-options for possible values and examples
92
     * @param string|null $id HTML identifier of div where chart will be drawed
93
     * @return string The generated chart
94
     */
95
    public function makeMultiTraceChart(
96
        array $series,
97
        string $type,
98
        array $layout = [],
99
        array $configuration = [],
100
        string $id = null
101
    ) {
102
        $traces = new Traces();
103
        foreach ($series as $serie) {
104
            $traces->addTrace(
105
                new Trace(
106
                    $serie[0],
107
                    $serie[1],
108
                    $type,
109
                    isset($serie[2]) ? $serie[2] : null,
110
                    isset($serie[3]) ? $serie[3] : null,
111
                    isset($serie[4]) ? $serie[4] : []
112
                )
113
            );
114
        }
115
116
        return $this->makeChart($traces, $layout, $configuration, $id);
117
    }
118
119
    /**
120
     * Every functions below are just syntactic sugar
121
     */
122
123
    /**
124
     * BAR CHARTS FUNCTIONS
125
     */
126
127
    /**
128
     * Function used to generate a simple bar chart
129
     *
130
     * @param array $x Values to be placed on X axis
131
     * @param array $y Values to be placed on Y axis
132
     * @param array $layout Any layout option accepted by Plotly.js
133
     * @see https://plot.ly/javascript/#layout-options for possible values and examples
134
     * @param array $configuration Any configuration option accepted by Plotly.js
135
     * @see https://plot.ly/javascript/configuration-options for possible values and examples
136
     * @param string|null $id HTML identifier of div where chart will be drawed
137
     * @return string The generated chart
138
     */
139
    public function simpleBarChart(
140
        array $x,
141
        array $y,
142
        array $layout = [],
143
        array $configuration = [],
144
        string $id = null
145
    ) {
146
        return $this->makeSingleTraceChart($x, $y, 'bar', null, $layout, $configuration, $id);
147
    }
148
149
    /**
150
     * Function used to generate a grouped bar chart
151
     *
152
     * @param array $series Multi-dimensional array of series
153
     *    $series = [
154
     *       [
155
     *          (array) X values,
156
     *          (array) Y values,
157
     *          (opt. string) Name of the series,
158
     *          (opt. string) Line type ("markers", "lines" or "markers+line")
159
     *       ], [...], [...]
160
     *    ]
161
     * @param array $layout Any layout option accepted by Plotly.js
162
     * @see https://plot.ly/javascript/#layout-options for possible values and examples
163
     * @param array $configuration Any configuration option accepted by Plotly.js
164
     * @see https://plot.ly/javascript/configuration-options for possible values and examples
165
     * @param string|null $id HTML identifier of div where chart will be drawed
166
     * @return string The generated chart
167
     */
168 View Code Duplication
    public function groupedBarChart(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
        array $series,
170
        array $layout = [],
171
        array $configuration = [],
172
        string $id = null
173
    ) {
174
        $layout['barmode'] = 'grouped';
175
176
        return $this->makeMultiTraceChart($series, 'bar', $layout, $configuration, $id);
177
    }
178
179
    /**
180
     * Function used to generate a stacked bar chart
181
     *
182
     * @param array $series Multi-dimensional array of series
183
     *    $series = [
184
     *       [
185
     *          (array) X values,
186
     *          (array) Y values,
187
     *          (opt. string) Name of the series,
188
     *          (opt. string) Line type ("markers", "lines" or "markers+line")
189
     *       ], [...], [...]
190
     *    ]
191
     * @param array $layout Any layout option accepted by Plotly.js
192
     * @see https://plot.ly/javascript/#layout-options for possible values and examples
193
     * @param array $configuration Any configuration option accepted by Plotly.js
194
     * @see https://plot.ly/javascript/configuration-options for possible values and examples
195
     * @param string|null $id HTML identifier of div where chart will be drawed
196
     * @return string The generated chart
197
     */
198 View Code Duplication
    public function stackedBarChart(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
        array $series,
200
        array $layout = [],
201
        array $configuration = [],
202
        string $id = null
203
    ): string {
204
        $layout['barmode'] = 'stack';
205
206
        return $this->makeMultiTraceChart($series, 'bar', $layout, $configuration, $id);
207
    }
208
209
    /**
210
     * PIE CHART FUNCTION
211
     */
212
213
    /**
214
     * Function used to generate a simple pie chart
215
     *
216
     * @param array $x Values to be placed on X axis
217
     * @param array $y Values to be placed on Y axis
218
     * @param array $layout Any layout option accepted by Plotly.js
219
     * @see https://plot.ly/javascript/#layout-options for possible values and examples
220
     * @param array $configuration Any configuration option accepted by Plotly.js
221
     * @see https://plot.ly/javascript/configuration-options for possible values and examples
222
     * @param string|null $id HTML identifier of div where chart will be drawed
223
     * @return string The generated chart
224
     */
225
    public function pieChart(
226
        array $x,
227
        array $y,
228
        array $layout = [],
229
        array $configuration = [],
230
        string $id = null
231
    ): string {
232
        return $this->makeSingleTraceChart($x, $y, 'pie', null, $layout, $configuration, $id);
233
    }
234
235
    /**
236
     * LINE CHARTS FUNCTIONS
237
     */
238
239
    /**
240
     * Function used to generate a single line chart
241
     *
242
     * @param array $x Values to be placed on X axis
243
     * @param array $y Values to be placed on Y axis
244
     * @param string $mode Can be either "markers", "lines" or "markers+line"
245
     * @param array $layout Any layout option accepted by Plotly.js
246
     * @see https://plot.ly/javascript/#layout-options for possible values and examples
247
     * @param array $configuration Any configuration option accepted by Plotly.js
248
     * @see https://plot.ly/javascript/configuration-options for possible values and examples
249
     * @param string|null $id HTML identifier of div where chart will be drawed
250
     * @return string The generated chart
251
     */
252
    public function singleLineChart(
253
        array $x,
254
        array $y,
255
        string $mode = 'markers+line',
256
        array $layout = [],
257
        array $configuration = [],
258
        string $id = null
259
    ): string {
260
        return $this->makeSingleTraceChart($x, $y, 'scatter', $mode, $layout, $configuration, $id);
261
    }
262
263
    /**
264
     * Function used to generate a multiline chart
265
     *
266
     * @param array $series Multi-dimensional array of series
267
     *    $series = [
268
     *       [
269
     *          (array) X values,
270
     *          (array) Y values,
271
     *          (opt. string) Name of the series,
272
     *          (opt. string) Line type ("markers", "lines" or "markers+line")
273
     *       ], [...], [...]
274
     *    ]
275
     * @param array $layout Any layout option accepted by Plotly.js
276
     * @see https://plot.ly/javascript/#layout-options for possible values and examples
277
     * @param array $configuration Any configuration option accepted by Plotly.js
278
     * @see https://plot.ly/javascript/configuration-options for possible values and examples
279
     * @param string|null $id HTML identifier of div where chart will be drawed
280
     * @return string The generated chart
281
     */
282
    public function multilineChart(
283
        array $series,
284
        array $layout = [],
285
        array $configuration = [],
286
        string $id = null
287
    ): string {
288
        return $this->makeMultiTraceChart($series, 'scatter', $layout, $configuration, $id);
289
    }
290
}
291