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