|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CarlosIO\Geckoboard\Widgets; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class LineChart. |
|
7
|
|
|
*/ |
|
8
|
|
|
class LineChart extends Widget |
|
9
|
|
|
{ |
|
10
|
|
|
const DIMENSION_X = 'x'; |
|
11
|
|
|
const DIMENSION_Y = 'y'; |
|
12
|
|
|
|
|
13
|
|
|
const DEFAULT_COLOUR = 'ff9900'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $items; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $colour; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $axis; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set the items property. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $items Set of items to add to the widget |
|
34
|
|
|
* |
|
35
|
|
|
* @return $this |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function setItems(array $items) |
|
38
|
|
|
{ |
|
39
|
2 |
|
foreach ($items as $item) { |
|
40
|
2 |
|
$this->addItem($item); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Return the items attribute. |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function getItems() |
|
52
|
|
|
{ |
|
53
|
2 |
|
if (null === $this->items) { |
|
54
|
1 |
|
$this->items = array(); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
return $this->items; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Add an item to the item list. |
|
62
|
|
|
* |
|
63
|
|
|
* @param \CarlosIO\Geckoboard\Data\Text\Item $item Item to be added |
|
64
|
|
|
* |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function addItem($item) |
|
68
|
|
|
{ |
|
69
|
2 |
|
if (!is_numeric($item)) { |
|
70
|
1 |
|
throw new \InvalidArgumentException(sprintf("Value '%s' must be a numeric value", $item)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
$this->items[] = $item; |
|
74
|
|
|
|
|
75
|
1 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Set the colour of the line in the widget. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $colour Colour of the line in the widget in hexadecimal format |
|
82
|
|
|
* |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
2 |
|
public function setColour($colour) |
|
86
|
|
|
{ |
|
87
|
2 |
|
if (!preg_match('/^[a-f0-9]{6}$/i', $colour)) { |
|
88
|
1 |
|
throw new \InvalidArgumentException(sprintf('Value %s must be a valid hex colour', $colour)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
$this->colour = $colour; |
|
92
|
|
|
|
|
93
|
1 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Return the colour of the line in the widget. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function getColour() |
|
102
|
|
|
{ |
|
103
|
2 |
|
if (null === $this->colour) { |
|
104
|
1 |
|
$this->colour = self::DEFAULT_COLOUR; |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
2 |
|
return $this->colour; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set the elements to appear evenly spread along dimension. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $dimension The dimension where labels will be displayed |
|
114
|
|
|
* @param array $labels Labels displayed in this axis |
|
115
|
|
|
* |
|
116
|
|
|
* @return $this |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public function setAxis($dimension, $labels) |
|
119
|
|
|
{ |
|
120
|
2 |
|
foreach ($labels as $label) { |
|
121
|
2 |
|
$this->addLabel($dimension, $label); |
|
122
|
1 |
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Add a new label to an specific axis. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $dimension The dimension where labels will be displayed |
|
131
|
|
|
* @param mix $label Label displayed in this axis |
|
132
|
|
|
*/ |
|
133
|
2 |
|
protected function addLabel($dimension, $label) |
|
134
|
|
|
{ |
|
135
|
2 |
|
if (!in_array($dimension, array(self::DIMENSION_X, self::DIMENSION_Y))) { |
|
136
|
1 |
|
throw new \InvalidArgumentException(sprintf("Value '%s' is not a valid dimension", $dimension)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
$this->axis[$dimension][] = $label; |
|
140
|
1 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Return axises in a 2D array. |
|
144
|
|
|
*/ |
|
145
|
2 |
|
public function getAxis() |
|
146
|
|
|
{ |
|
147
|
2 |
|
if (null === $this->axis) { |
|
148
|
1 |
|
$this->axis[self::DIMENSION_X] = array(); |
|
149
|
1 |
|
$this->axis[self::DIMENSION_Y] = array(); |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
2 |
|
return $this->axis; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
2 |
|
public function getData() |
|
159
|
|
|
{ |
|
160
|
2 |
|
$axis = $this->getAxis(); |
|
161
|
|
|
|
|
162
|
|
|
return array( |
|
163
|
2 |
|
'item' => $this->getItems(), |
|
164
|
|
|
'settings' => array( |
|
165
|
2 |
|
'axisx' => $axis[self::DIMENSION_X], |
|
166
|
2 |
|
'axisy' => $axis[self::DIMENSION_Y], |
|
167
|
2 |
|
'colour' => $this->getColour(), |
|
168
|
2 |
|
), |
|
169
|
2 |
|
); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|