|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* A query printer for bar, line, pie and donut chart on aggregated values |
|
5
|
|
|
* using the jqPlot JavaScript library. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 1.8 |
|
8
|
|
|
* |
|
9
|
|
|
* @file SRF_jqPlotChart.php |
|
10
|
|
|
* @ingroup SemanticResultFormats |
|
11
|
|
|
* @licence GNU GPL v2 or later |
|
12
|
|
|
* |
|
13
|
|
|
* @author mwjames |
|
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
15
|
|
|
* @author Yaron Koren |
|
16
|
|
|
* @author Sanyam Goyal |
|
17
|
|
|
*/ |
|
18
|
|
|
class SRFjqPlotChart extends SRFjqPlot { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Corresponding message name |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
public function getName() { |
|
25
|
|
|
return wfMessage( 'srf-printername-jqplotchart' )->text(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Prepare data output |
|
30
|
|
|
* |
|
31
|
|
|
* @since 1.8 |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $data label => value |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function getFormatOutput( array $data ) { |
|
36
|
|
|
|
|
37
|
|
|
static $statNr = 0; |
|
38
|
|
|
$chartID = 'jqplot-' . $this->params['charttype'] . '-' . ++$statNr; |
|
39
|
|
|
|
|
40
|
|
|
$this->isHTML = true; |
|
41
|
|
|
|
|
42
|
|
|
// Prepare data objects |
|
43
|
|
|
if ( in_array( $this->params['charttype'], [ 'bar', 'line' ] ) ) { |
|
44
|
|
|
// Parse bar relevant data |
|
45
|
|
|
$dataObject = $this->prepareBarData( $data ); |
|
46
|
|
|
} elseif ( in_array( $this->params['charttype'], [ 'pie', 'donut' ] ) ){ |
|
47
|
|
|
//Parse pie/donut relevant data |
|
48
|
|
|
$dataObject = $this->preparePieData( $data ); |
|
49
|
|
|
} else { |
|
50
|
|
|
// Return with an error |
|
51
|
|
|
return Html::rawElement( 'span', [ |
|
52
|
|
|
'class' => "error" |
|
53
|
|
|
], wfMessage( 'srf-error-missing-layout' )->inContentLanguage()->text() |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Encode data objects |
|
58
|
|
|
$requireHeadItem = [ $chartID => FormatJson::encode( $dataObject ) ]; |
|
59
|
|
|
SMWOutputs::requireHeadItem( $chartID, Skin::makeVariablesScript($requireHeadItem ) ); |
|
60
|
|
|
|
|
61
|
|
|
// Processing placeholder |
|
62
|
|
|
$processing = SRFUtils::htmlProcessingElement( $this->isHTML ); |
|
63
|
|
|
|
|
64
|
|
|
// Ensure right conversion |
|
65
|
|
|
$width = strstr( $this->params['width'] ,"%") ? $this->params['width'] : $this->params['width'] . 'px'; |
|
66
|
|
|
|
|
67
|
|
|
// Chart/graph placeholder |
|
68
|
|
|
$chart = Html::rawElement( 'div', [ |
|
69
|
|
|
'id' => $chartID, |
|
70
|
|
|
'class' => 'container', |
|
71
|
|
|
'style' => "display:none; width: {$width}; height: {$this->params['height']}px;" |
|
72
|
|
|
], null |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
// Beautify class selector |
|
76
|
|
|
$class = $this->params['charttype'] ? '-' . $this->params['charttype'] : ''; |
|
77
|
|
|
$class = $this->params['class'] ? $class . ' ' . $this->params['class'] : $class . ' jqplot-common'; |
|
78
|
|
|
|
|
79
|
|
|
// Chart/graph wrappper |
|
80
|
|
|
return Html::rawElement( 'div', [ |
|
81
|
|
|
'class' => 'srf-jqplot' . $class, |
|
82
|
|
|
], $processing . $chart |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Prepare pie/donut chart specific data and parameters |
|
88
|
|
|
* |
|
89
|
|
|
* @since 1.8 |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $rawdata label => value |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
private function preparePieData( $rawdata ) { |
|
95
|
|
|
|
|
96
|
|
|
// Init |
|
97
|
|
|
$mode = 'single'; |
|
98
|
|
|
|
|
99
|
|
|
// Reorganize the data in accordance with the pie chart req. |
|
100
|
|
|
foreach ( $rawdata as $name => $value ) { |
|
101
|
|
|
if ( $value >= $this->params['min'] ) { |
|
102
|
|
|
$data[] = [ $name , $value ]; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ( $this->params['charttype'] === 'donut' ) { |
|
107
|
|
|
SMWOutputs::requireResource( 'ext.srf.jqplot.donut' ); |
|
108
|
|
|
} else { |
|
109
|
|
|
SMWOutputs::requireResource( 'ext.srf.jqplot.pie' ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return [ |
|
113
|
|
|
'data' => [ $data ], |
|
|
|
|
|
|
114
|
|
|
'renderer' => $this->params['charttype'], |
|
115
|
|
|
'mode' => $mode, |
|
116
|
|
|
'parameters' => $this->addCommonOptions() |
|
117
|
|
|
]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Prepare bar/line chart specific data and parameters |
|
122
|
|
|
* |
|
123
|
|
|
* Data can be an array of y values, or an array of [label, value] pairs; |
|
124
|
|
|
* While labels are used only on the first series with labels on |
|
125
|
|
|
* subsequent series being ignored |
|
126
|
|
|
* |
|
127
|
|
|
* @since 1.8 |
|
128
|
|
|
* |
|
129
|
|
|
* @param array $rawdata label => value |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
private function prepareBarData( $rawdata ) { |
|
133
|
|
|
|
|
134
|
|
|
// Init |
|
135
|
|
|
$total = 0; |
|
136
|
|
|
$mode = 'single'; |
|
137
|
|
|
|
|
138
|
|
|
// Find min and max values to determine the graphs axis parameter |
|
139
|
|
|
$maxValue = count( $rawdata ) == 0 ? 0 : max( $rawdata ); |
|
140
|
|
|
|
|
141
|
|
|
if ( $this->params['min'] === false ) { |
|
142
|
|
|
$minValue = count( $rawdata ) == 0 ? 0 : min( $rawdata ); |
|
143
|
|
|
} else { |
|
144
|
|
|
$minValue = $this->params['min']; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// Get number ticks |
|
148
|
|
|
$data['numbersticks'] = SRFjqPlot::getNumbersTicks( $minValue, $maxValue ); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
// Reorganize the data in accordance with the bar/line chart req. |
|
151
|
|
|
foreach ( $rawdata as $key => $value ) { |
|
152
|
|
|
if ( $value >= $this->params['min'] ) { |
|
153
|
|
|
$data['series'][] = [ $key, $value ]; |
|
154
|
|
|
$total = $total + $value; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// Bar/line module |
|
159
|
|
|
SMWOutputs::requireResource( 'ext.srf.jqplot.bar' ); |
|
160
|
|
|
|
|
161
|
|
|
// Highlighter plugin |
|
162
|
|
|
if ( $this->params['highlighter'] ) { |
|
163
|
|
|
SMWOutputs::requireResource( 'ext.srf.jqplot.highlighter' ); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Pointlabels plugin |
|
167
|
|
|
if ( in_array( $this->params['datalabels'], [ 'value', 'label', 'percent' ] ) ) { |
|
168
|
|
|
SMWOutputs::requireResource( 'ext.srf.jqplot.pointlabels' ); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return [ |
|
172
|
|
|
'data' => [ $data['series'] ], |
|
173
|
|
|
'ticks' => $data['numbersticks'], |
|
174
|
|
|
'labels' => array_keys( $data['series'] ), |
|
175
|
|
|
'numbers' => array_values( $data['series'] ), |
|
176
|
|
|
'max' => $maxValue, |
|
177
|
|
|
'total' => $total, |
|
178
|
|
|
'mode' => $mode, |
|
179
|
|
|
'series' => [], |
|
180
|
|
|
'renderer' => $this->params['charttype'], |
|
181
|
|
|
'parameters'=> $this->addCommonOptions() |
|
182
|
|
|
]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* jqPlot common parameters |
|
187
|
|
|
* |
|
188
|
|
|
* @since 1.8 |
|
189
|
|
|
* |
|
190
|
|
|
*/ |
|
191
|
|
|
private function addCommonOptions() { |
|
192
|
|
|
|
|
193
|
|
|
// Series colour |
|
194
|
|
|
$seriescolors = $this->params['chartcolor'] !== '' ? array_filter( explode( ",", $this->params['chartcolor'] ) ): null; |
|
195
|
|
|
|
|
196
|
|
|
return [ |
|
197
|
|
|
'numbersaxislabel' => $this->params['numbersaxislabel'], |
|
198
|
|
|
'labelaxislabel' => $this->params['labelaxislabel'], |
|
199
|
|
|
'charttitle' => $this->params['charttitle'], |
|
200
|
|
|
'charttext' => $this->params['charttext'], |
|
201
|
|
|
'theme' => $this->params['theme'] ? $this->params['theme'] : null, |
|
202
|
|
|
'ticklabels' => $this->params['ticklabels'], |
|
203
|
|
|
'highlighter' => $this->params['highlighter'], |
|
204
|
|
|
'direction' => $this->params['direction'], |
|
205
|
|
|
'smoothlines' => $this->params['smoothlines'], |
|
206
|
|
|
'filling' => $this->params['filling'], |
|
207
|
|
|
'datalabels' => $this->params['datalabels'], |
|
208
|
|
|
'valueformat' => $this->params['valueformat'], |
|
209
|
|
|
'chartlegend' => $this->params['chartlegend'] !== '' ? $this->params['chartlegend'] : 'none', |
|
210
|
|
|
'colorscheme' => $this->params['colorscheme'] !== '' ? $this->params['colorscheme'] : null , |
|
211
|
|
|
'pointlabels' => $this->params['datalabels'] === 'none' ? false : $this->params['datalabels'], |
|
212
|
|
|
'grid' => $this->params['theme'] === 'vector' ? [ 'borderColor' => '#a7d7f9' ] : ( $this->params['theme'] === 'simple' ? [ 'borderColor' => '#ddd' ] : null ), |
|
213
|
|
|
'seriescolors' => $seriescolors |
|
214
|
|
|
]; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @see SMWResultPrinter::getParamDefinitions |
|
219
|
|
|
* |
|
220
|
|
|
* @since 1.8 |
|
221
|
|
|
* |
|
222
|
|
|
* @param $definitions array of IParamDefinition |
|
223
|
|
|
* |
|
224
|
|
|
* @return array of IParamDefinition|array |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getParamDefinitions( array $definitions ) { |
|
227
|
|
|
$params = self::getCommonParams(); |
|
228
|
|
|
|
|
229
|
|
|
$params['charttype'] = [ |
|
230
|
|
|
'message' => 'srf-paramdesc-charttype', |
|
231
|
|
|
'default' => 'bar', |
|
232
|
|
|
'values' => [ 'bar', 'line', 'pie', 'donut' ], |
|
233
|
|
|
]; |
|
234
|
|
|
|
|
235
|
|
|
return array_merge( parent::getParamDefinitions( $definitions ), $params ); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.