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
|
|
|
|
13
|
|
|
namespace CakeCharts\Utility; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Trace |
17
|
|
|
* @package CakeCharts\Utility |
18
|
|
|
*/ |
19
|
|
|
class Trace |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array Values to be placed on X axis |
23
|
|
|
*/ |
24
|
|
|
private $x; |
25
|
|
|
/** |
26
|
|
|
* @var array Values to be placed on Y axis |
27
|
|
|
*/ |
28
|
|
|
private $y; |
29
|
|
|
/** |
30
|
|
|
* @var string Type of trace : can be either "bar", "pie" or "scatter" |
31
|
|
|
*/ |
32
|
|
|
private $type; |
33
|
|
|
/** |
34
|
|
|
* @var string Name of the series |
35
|
|
|
*/ |
36
|
|
|
private $name; |
37
|
|
|
/** |
38
|
|
|
* @var string Line type ("markers", "lines" or "markers+line") |
39
|
|
|
*/ |
40
|
|
|
private $mode; |
41
|
|
|
/** |
42
|
|
|
* @var array Styling options for marker |
43
|
|
|
*/ |
44
|
|
|
private $marker; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Trace constructor. |
48
|
|
|
* |
49
|
|
|
* @param array $x Values to be placed on X axis |
50
|
|
|
* @param array $y Values to be placed on Y axis |
51
|
|
|
* @param string $type Type of trace : can be either "bar", "pie" or "scatter" |
52
|
|
|
* @param string|null $name Name of the series |
53
|
|
|
* @param string|null $mode Line type ("markers", "lines" or "markers+line") |
54
|
|
|
* @param array $marker Plot.ly option for markers |
55
|
|
|
*/ |
56
|
|
|
public function __construct(array $x, array $y, string $type, string $name = null, string $mode = null, array $marker = []) |
57
|
|
|
{ |
58
|
|
|
$this->x = $x; |
59
|
|
|
$this->y = $y; |
60
|
|
|
$this->type = $type; |
61
|
|
|
$this->name = $name; |
62
|
|
|
$this->mode = $mode; |
63
|
|
|
$this->marker = $marker; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Convert Trace object to keys/values array |
68
|
|
|
* |
69
|
|
|
* @return array Plotly.js compliant data series (also known as trace) |
70
|
|
|
* @throws \Exception If unexpected values occured |
71
|
|
|
*/ |
72
|
|
|
public function toArray() |
73
|
|
|
{ |
74
|
|
|
$this->checkXequalsY(); |
75
|
|
|
switch ($this->type) { |
76
|
|
View Code Duplication |
case 'bar': |
|
|
|
|
77
|
|
|
$trace = ['x' => $this->x, 'y' => $this->y, 'type' => $this->type, 'name' => $this->name, 'marker' => $this->marker]; |
78
|
|
|
break; |
79
|
|
View Code Duplication |
case 'scatter': |
|
|
|
|
80
|
|
|
$this->checkModeValue(); |
81
|
|
|
$trace = ['x' => $this->x, 'y' => $this->y, 'type' => $this->type, 'name' => $this->name, 'mode' => $this->mode]; |
82
|
|
|
break; |
83
|
|
|
case 'pie': |
84
|
|
|
$trace = ['labels' => $this->x, 'values' => $this->y, 'type' => $this->type]; |
85
|
|
|
break; |
86
|
|
|
default: |
87
|
|
|
throw new \Exception('Unhandled chart type : can be either "bar", "scatter" or "pie" ; got "' . $this->type . '"'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $trace; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Makes sure that number of X values equals Y values |
95
|
|
|
* |
96
|
|
|
* @throws \Exception |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
private function checkXequalsY() |
100
|
|
|
{ |
101
|
|
|
if (count($this->x) !== count($this->y)) { |
102
|
|
|
$name = isset($this->name) ? $this->name : 'unamed'; |
103
|
|
|
throw new \Exception("Number of X axis values are not equal to number of Y axis values for <u>$name</u> series"); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Makes sure that "mode" is a valid value |
109
|
|
|
* |
110
|
|
|
* @throws \Exception |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
private function checkModeValue() |
114
|
|
|
{ |
115
|
|
|
if (!in_array($this->mode, ['markers', 'lines', 'lines+markers', null])) { |
116
|
|
|
throw new \Exception('Invalid trace mode : can be either "markers", "lines" or "markers+line" ; got "' . $this->mode . '"'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
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.