2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Chart functions used to generate various types of charts.
5 * @author Martynas Mickevicius <mmartynas@gmail.com>
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';
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
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(
41 array('titleText' => __('Query statistics'))
43 $chartCode = $chart->toString();
44 PMA_handle_chart_err($chart->getErrors());
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)
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(
64 array('titleText' => __('Query execution time comparison (in microseconds)'))
66 $chartCode = $chart->toString();
67 PMA_handle_chart_err($chart->getErrors());
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)
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])) {
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];
127 $chartData[$values[1]] = $values[0];
131 $chartSettings['legend'] = true;
132 $chart = new PMA_pChart_pie($chartData, $chartSettings);
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']) {
153 $chart = new PMA_pChart_single_bar($chartData, $chartSettings);
156 $chart = new PMA_pChart_single_line($chartData, $chartSettings);
159 $chart = new PMA_pChart_single_radar($chartData, $chartSettings);
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) {
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']) {
210 // determine the bar chart type
211 switch ($chartSettings['barType']) {
214 $chart = new PMA_pChart_stacked_bar($chartData, $chartSettings);
217 $chart = new PMA_pChart_multi_bar($chartData, $chartSettings);
223 $chart = new PMA_pChart_multi_line($chartData, $chartSettings);
226 $chart = new PMA_pChart_multi_radar($chartData, $chartSettings);
231 // unknown data format
235 $chartCode = $chart->toString();
236 $chartSettings = $chart->getSettings();
237 $chartErrors = $chart->getErrors();
238 PMA_handle_chart_err($chartErrors);
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.');