1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
<?php /* vim: set expandtab sw=4 ts=4 sts=4: */ /** * Chart functions used to generate various types of charts. * @package phpMyAdmin */
/** * */ define('ERR_NO_GD', 0); define('ERR_NO_JSON', 1);
require_once './libraries/chart/pma_pchart_pie.php'; require_once './libraries/chart/pma_pchart_single_bar.php'; require_once './libraries/chart/pma_pchart_multi_bar.php'; require_once './libraries/chart/pma_pchart_stacked_bar.php'; require_once './libraries/chart/pma_pchart_single_line.php'; require_once './libraries/chart/pma_pchart_multi_line.php'; require_once './libraries/chart/pma_pchart_single_radar.php'; require_once './libraries/chart/pma_pchart_multi_radar.php';
/** * Formats a chart for the status page. * @param array $data data for the status chart * @return string HTML and JS code for the chart */ function PMA_chart_status($data) { // format keys which will be shown in the chart $chartData = array(); foreach($data as $dataKey => $dataValue) { $key = ucwords(str_replace(array('Com_', '_'), array('', ' '), $dataKey)); $value = (int)$dataValue; $chartData[$key] = $value; } $chart = new PMA_pChart_Pie( $chartData, array('titleText' => __('Query statistics')) ); $chartCode = $chart->toString(); PMA_handle_chart_err($chart->getErrors()); echo $chartCode; }
/** * Formats a chart for the profiling page. * @param array $data data for the status chart * @return string HTML and JS code for the chart */ function PMA_chart_profiling($data) { $chartData = array(); foreach($data as $dataValue) { $value = (int)($dataValue['Duration'] * 1000000); $key = ucwords($dataValue['Status']); $chartData[$key] = $value; }
$chart = new PMA_pChart_Pie( $chartData, array('titleText' => __('Query execution time comparison (in microseconds)')) ); $chartCode = $chart->toString(); PMA_handle_chart_err($chart->getErrors()); echo $chartCode; }
/** * Formats a chart for the query results page. * @param array $data data for the status chart * @param array $chartSettings settings used to generate the chart * @return string HTML and JS code for the chart */ function PMA_chart_results($data, &$chartSettings) { $chartData = array(); $chart = null;
// set default title if not already set if (empty($chartSettings['titleText'])) { $chartSettings['titleText'] = __('Query results'); }
// set default type if not already set if (empty($chartSettings['type'])) { $chartSettings['type'] = 'bar'; }
// set default type if not already set if (empty($chartSettings['continuous'])) { $chartSettings['continuous'] = 'off'; }
// set default bar type if needed if ($chartSettings['type'] == 'bar' && empty($chartSettings['barType'])) { $chartSettings['barType'] = 'stacked'; }
// default for legend $chartSettings['legend'] = false;
// default for muti series $chartSettings['multi'] = false;
if (!isset($data[0])) { // empty data return __('No data found for the chart.'); }
if (count($data[0]) == 1 || count($data[0]) == 2) { // One or two columns in every row. // This data is suitable for a simple bar chart.
if ($chartSettings['type'] == 'pie') { // loop through the rows, data for pie chart has to be formated // in a different way then in other charts. foreach ($data as $rowKey => $row) { $values = array_values($row);
if (count($row) == 1) { $chartData[$rowKey] = $values[0]; } else { $chartData[$values[1]] = $values[0]; } }
$chartSettings['legend'] = true; $chart = new PMA_pChart_pie($chartData, $chartSettings); } else { // loop through the rows foreach ($data as $rowKey => $row) {
// loop through the columns in the row foreach ($row as $valueKey => $value) { $chartData[$valueKey][] = $value; }
// if only one column, we need to add // placeholder data for x axis if (count($row) == 1) { $chartData[''][] = $rowKey; } }
switch ($chartSettings['type']) { case 'bar': default: $chart = new PMA_pChart_single_bar($chartData, $chartSettings); break; case 'line': $chart = new PMA_pChart_single_line($chartData, $chartSettings); break; case 'radar': $chart = new PMA_pChart_single_radar($chartData, $chartSettings); break; } } } else if (count($data[0]) == 3) { // Three columns (x axis, y axis, series) in every row. // This data is suitable for a stacked bar chart. $chartSettings['multi'] = true;
$keys = array_keys($data[0]); $yAxisKey = $keys[0]; $xAxisKey = $keys[1]; $seriesKey = $keys[2]; // get all the series labels $seriesLabels = array(); foreach ($data as $row) { $seriesLabels[] = $row[$seriesKey]; } $seriesLabels = array_unique($seriesLabels);
// loop through the rows $currentXLabel = $data[0][$xAxisKey]; foreach ($data as $row) {
// save the label // use the same value as the key and the value to get rid of duplicate results $chartData[$xAxisKey][$row[$xAxisKey]] = $row[$xAxisKey];
// make sure to set value to every serie $currentSeriesLabel = (string)$row[$seriesKey]; foreach ($seriesLabels as $seriesLabelsValue) { if ($currentSeriesLabel == $seriesLabelsValue) { // the value os for this serie $chartData[$yAxisKey][$seriesLabelsValue][$row[$xAxisKey]] = (int)$row[$yAxisKey]; } else if (!isset($chartData[$yAxisKey][$seriesLabelsValue][$row[$xAxisKey]])) { // if the value for this serie is not set, set it to 0 $chartData[$yAxisKey][$seriesLabelsValue][$row[$xAxisKey]] = 0; } } }
$chartSettings['legend'] = true;
// determine the chart type switch ($chartSettings['type']) { case 'bar': default:
// determine the bar chart type switch ($chartSettings['barType']) { case 'stacked': default: $chart = new PMA_pChart_stacked_bar($chartData, $chartSettings); break; case 'multi': $chart = new PMA_pChart_multi_bar($chartData, $chartSettings); break; } break; case 'line': $chart = new PMA_pChart_multi_line($chartData, $chartSettings); break; case 'radar': $chart = new PMA_pChart_multi_radar($chartData, $chartSettings); break; } } else { // unknown data format return ''; }
$chartCode = $chart->toString(); $chartSettings = $chart->getSettings(); $chartErrors = $chart->getErrors(); PMA_handle_chart_err($chartErrors); return $chartCode; }
/** * Simple handler of chart errors. * @param array $errors all occured errors */ function PMA_handle_chart_err($errors) { if (in_array(ERR_NO_GD, $errors)) { PMA_warnMissingExtension('GD', false, __('GD extension is needed for charts.')); } else if (in_array(ERR_NO_JSON, $errors)) { PMA_warnMissingExtension('JSON', false, __('JSON encoder is needed for chart tooltips.')); } }
?>
|