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