1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace CMEN\GoogleChartsBundle\GoogleCharts;
|
4
|
|
|
|
5
|
|
|
use CMEN\GoogleChartsBundle\Exception\GoogleChartsException;
|
6
|
|
|
use CMEN\GoogleChartsBundle\GoogleCharts\Charts\Diff\DiffChart;
|
7
|
|
|
use CMEN\GoogleChartsBundle\GoogleCharts\Options\ChartOptionsDraw;
|
8
|
|
|
|
9
|
|
|
/**
|
10
|
|
|
* @author Christophe Meneses
|
11
|
|
|
*/
|
12
|
|
|
abstract class Chart
|
13
|
|
|
{
|
14
|
|
|
/**
|
15
|
|
|
* @var string
|
16
|
|
|
*/
|
17
|
|
|
protected $elementID;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @var Data
|
21
|
|
|
*/
|
22
|
|
|
protected $data;
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* @var ChartOptionsDraw
|
26
|
|
|
*/
|
27
|
|
|
protected $options;
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @var Events
|
31
|
|
|
*/
|
32
|
|
|
protected $events;
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
public function __construct()
|
36
|
|
|
{
|
37
|
|
|
$this->data = new Data();
|
38
|
|
|
$this->events = new Events($this);
|
39
|
|
|
}
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/**
|
43
|
|
|
* Returns chart's name.
|
44
|
|
|
*
|
45
|
|
|
* @return string
|
46
|
|
|
*/
|
47
|
|
|
public function getName()
|
48
|
|
|
{
|
49
|
|
|
return 'chart' . ucfirst($this->elementID);
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* Returns data chart's name
|
54
|
|
|
*
|
55
|
|
|
* @return string
|
56
|
|
|
*/
|
57
|
|
|
public function getDataName()
|
58
|
|
|
{
|
59
|
|
|
return 'data'. ucfirst($this->getName());
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* Returns options chart's name
|
64
|
|
|
*
|
65
|
|
|
* @return string
|
66
|
|
|
*/
|
67
|
|
|
public function getOptionsName()
|
68
|
|
|
{
|
69
|
|
|
return 'options'. ucfirst($this->getName());
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
/**
|
73
|
|
|
* Returns the chart type.
|
74
|
|
|
*
|
75
|
|
|
* @return string
|
76
|
|
|
*/
|
77
|
|
|
abstract protected function getType();
|
78
|
|
|
|
79
|
|
|
/**
|
80
|
|
|
* Returns the chart package.
|
81
|
|
|
*
|
82
|
|
|
* @return string
|
83
|
|
|
*/
|
84
|
|
|
abstract public function getPackage();
|
85
|
|
|
|
86
|
|
|
/**
|
87
|
|
|
* Returns the instance options.
|
88
|
|
|
*/
|
89
|
|
|
abstract public function getOptions();
|
90
|
|
|
|
91
|
|
|
/**
|
92
|
|
|
* Sets the instance Options
|
93
|
|
|
*
|
94
|
|
|
* @param ChartOptionsDraw $options
|
95
|
|
|
*
|
96
|
|
|
* @return ChartOptionsDraw
|
97
|
|
|
*/
|
98
|
|
|
abstract public function setOptions($options);
|
99
|
|
|
|
100
|
|
|
/**
|
101
|
|
|
* Returns the Javascript of the beginning of the chart (Declaration, data and options).
|
102
|
|
|
*
|
103
|
|
|
* @return string Javascript
|
104
|
|
|
*
|
105
|
|
|
* @throws GoogleChartsException
|
106
|
|
|
*/
|
107
|
|
|
public function startDraw()
|
108
|
|
|
{
|
109
|
|
|
if ($this->elementID === null) {
|
110
|
|
|
throw new GoogleChartsException('Container is not defined.');
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
$js = 'var ' . $this->getName() . ' = new google.visualization.' . $this->getType() .
|
114
|
|
|
'(document.getElementById("' . $this->elementID . '"));';
|
115
|
|
|
|
116
|
|
|
if (!$this instanceof DiffChart) {
|
117
|
|
|
$js .= $this->data->draw($this->getDataName());
|
118
|
|
|
} else {
|
119
|
|
|
$js .= $this->getOldChart()->getData()->draw('old_' . $this->getDataName()) .
|
|
|
|
|
120
|
|
|
$this->getNewChart()->getData()->draw('new_' . $this->getDataName()) .
|
|
|
|
|
121
|
|
|
'var ' . $this->getDataName() . ' = ' . $this->getName() .
|
122
|
|
|
'.computeDiff(old_' . $this->getDataName() . ',
|
123
|
|
|
new_' . $this->getDataName() . ');';
|
124
|
|
|
}
|
125
|
|
|
|
126
|
|
|
$js .= $this->options->draw($this->getOptionsName());
|
127
|
|
|
|
128
|
|
|
return $js;
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
/**
|
132
|
|
|
* Returns the Javascript of the end of the chart (Events and drawing).
|
133
|
|
|
*
|
134
|
|
|
* @return string
|
135
|
|
|
*/
|
136
|
|
|
public function endDraw()
|
137
|
|
|
{
|
138
|
|
|
return $this->events->draw(). $this->getName() .
|
139
|
|
|
'.draw('. $this->getDataName() . ', '. $this->getOptionsName() .');';
|
140
|
|
|
}
|
141
|
|
|
|
142
|
|
|
/**
|
143
|
|
|
* @param string $elementID
|
144
|
|
|
*
|
145
|
|
|
* @return $this
|
146
|
|
|
*/
|
147
|
|
|
public function setElementID($elementID)
|
148
|
|
|
{
|
149
|
|
|
$this->elementID = $elementID;
|
150
|
|
|
|
151
|
|
|
return $this;
|
152
|
|
|
}
|
153
|
|
|
|
154
|
|
|
/**
|
155
|
|
|
* @return string
|
156
|
|
|
*/
|
157
|
|
|
public function getElementID()
|
158
|
|
|
{
|
159
|
|
|
return $this->elementID;
|
160
|
|
|
}
|
161
|
|
|
|
162
|
|
|
/**
|
163
|
|
|
* @return Data
|
164
|
|
|
*/
|
165
|
|
|
public function getData()
|
166
|
|
|
{
|
167
|
|
|
return $this->data;
|
168
|
|
|
}
|
169
|
|
|
|
170
|
|
|
/**
|
171
|
|
|
* @return Events
|
172
|
|
|
*/
|
173
|
|
|
public function getEvents()
|
174
|
|
|
{
|
175
|
|
|
return $this->events;
|
176
|
|
|
}
|
177
|
|
|
}
|
178
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: