From dfe34ad8d4860899c7415c94be5a0db9653860ad Mon Sep 17 00:00:00 2001 From: Martynas Mickevicius Date: Thu, 8 Jul 2010 12:40:03 +0300 Subject: [PATCH] added chart type PIE to the query results charts --- libraries/chart.lib.php | 53 ++++++++++++++++++++---------- libraries/chart/pma_pchart_pie.php | 13 ++++++-- tbl_chart.php | 7 +++- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/libraries/chart.lib.php b/libraries/chart.lib.php index b245b2a4a..7267d6987 100644 --- a/libraries/chart.lib.php +++ b/libraries/chart.lib.php @@ -81,6 +81,12 @@ function PMA_chart_results($data, &$chartSettings) $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.'); @@ -90,29 +96,42 @@ function PMA_chart_results($data, &$chartSettings) // Two columns in every row. // This data is suitable for a simple bar chart. - // loop through the rows - foreach ($data as $row) { - // loop through the columns in the row - foreach ($row as $key => $value) { - $chartData[$key][] = $value; + 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 $row) { + $values = array_values($row); + $chartData[$values[0]] = $values[1]; } - } - $chartSettings['multi'] = false; - - switch ($chartSettings['type']) { - case 'bar': - default: - $chart = new PMA_pChart_single_bar($chartTitle, $chartData, $chartSettings); - break; - case 'line': - $chart = new PMA_pChart_single_line($chartTitle, $chartData, $chartSettings); - break; + $chartSettings['legend'] = true; + $chart = new PMA_pChart_pie($chartTitle, $chartData, $chartSettings); } + else { + // loop through the rows + foreach ($data as $row) { + // loop through the columns in the row + foreach ($row as $key => $value) { + $chartData[$key][] = $value; + } + } + + switch ($chartSettings['type']) { + case 'bar': + default: + $chart = new PMA_pChart_single_bar($chartTitle, $chartData, $chartSettings); + break; + case 'line': + $chart = new PMA_pChart_single_line($chartTitle, $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]); $xAxisKey = $keys[0]; $yAxisKey = $keys[1]; @@ -147,7 +166,7 @@ function PMA_chart_results($data, &$chartSettings) } } - $chartSettings['multi'] = true; + $chartSettings['legend'] = true; // determine the chart type switch ($chartSettings['type']) { diff --git a/libraries/chart/pma_pchart_pie.php b/libraries/chart/pma_pchart_pie.php index 1dbef08d6..5e01d9a1a 100644 --- a/libraries/chart/pma_pchart_pie.php +++ b/libraries/chart/pma_pchart_pie.php @@ -8,7 +8,7 @@ class PMA_pChart_Pie extends PMA_pChart_multi { parent::__construct($titleText, $data, $options); - $this->settings['areaMargins'] = array(20, 20, 20, 10); + $this->setAreaMargins(array(20, 20, 20, 10)); } protected function prepareDataSet() @@ -30,10 +30,19 @@ class PMA_pChart_Pie extends PMA_pChart_multi { parent::drawChart(); + // draw pie chart in the middle of graph area + $middleX = ($this->chart->GArea_X1 + $this->chart->GArea_X2) / 2; + $middleY = ($this->chart->GArea_Y1 + $this->chart->GArea_Y2) / 2; + $this->chart->drawPieGraph( $this->dataSet->GetData(), $this->dataSet->GetDataDescription(), - 180,160,120,PIE_PERCENTAGE,FALSE,60,30,10,1); + $middleX, + // pie graph is skewed. Upper part is shorter than the + // lower part. This is why we set an offset to the + // Y middle coordiantes. + $middleY - 15, + 120,PIE_PERCENTAGE,FALSE,60,30,10,1); } protected function drawLegend() diff --git a/tbl_chart.php b/tbl_chart.php index dffd4fae0..22b587f91 100644 --- a/tbl_chart.php +++ b/tbl_chart.php @@ -91,6 +91,7 @@ $url_params['reload'] = 1; + @@ -98,6 +99,7 @@ $url_params['reload'] = 1; + @@ -108,7 +110,7 @@ $url_params['reload'] = 1; - + @@ -123,6 +125,9 @@ $url_params['reload'] = 1; >Bar >Line + + >Pie +