2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Chart functions used to generate various types of charts.
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';
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
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(
40 array('titleText' => __('Query statistics'))
42 $chartCode = $chart->toString();
43 PMA_handle_chart_err($chart->getErrors());
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)
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(
63 array('titleText' => __('Query execution time comparison (in microseconds)'))
65 $chartCode = $chart->toString();
66 PMA_handle_chart_err($chart->getErrors());
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)
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])) {
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];
126 $chartData[$values[1]] = $values[0];
130 $chartSettings['legend'] = true;
131 $chart = new PMA_pChart_pie($chartData, $chartSettings);
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']) {
152 $chart = new PMA_pChart_single_bar($chartData, $chartSettings);
155 $chart = new PMA_pChart_single_line($chartData, $chartSettings);
158 $chart = new PMA_pChart_single_radar($chartData, $chartSettings);
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) {
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']) {
209 // determine the bar chart type
210 switch ($chartSettings['barType']) {
213 $chart = new PMA_pChart_stacked_bar($chartData, $chartSettings);
216 $chart = new PMA_pChart_multi_bar($chartData, $chartSettings);
222 $chart = new PMA_pChart_multi_line($chartData, $chartSettings);
225 $chart = new PMA_pChart_multi_radar($chartData, $chartSettings);
230 // unknown data format
234 $chartCode = $chart->toString();
235 $chartSettings = $chart->getSettings();
236 $chartErrors = $chart->getErrors();
237 PMA_handle_chart_err($chartErrors);
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.'));