From c5b49bb839f472487073d8b6508436aa4e0cff42 Mon Sep 17 00:00:00 2001 From: Martynas Mickevicius Date: Tue, 22 Jun 2010 14:24:23 +0300 Subject: [PATCH] added stacked bar chart --- libraries/chart.lib.php | 74 ++++++++++++++++++++++---- libraries/chart/pma_pchart_bar.php | 2 +- libraries/chart/pma_pchart_stacked.php | 67 +++++++++++++++++++++++ 3 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 libraries/chart/pma_pchart_stacked.php diff --git a/libraries/chart.lib.php b/libraries/chart.lib.php index 3a4b50315..e36b338fb 100644 --- a/libraries/chart.lib.php +++ b/libraries/chart.lib.php @@ -4,6 +4,7 @@ require_once './libraries/chart/pma_ofc_pie.php'; require_once './libraries/chart/pma_pChart_pie.php'; require_once './libraries/chart/pma_pChart_bar.php'; +require_once './libraries/chart/pma_pChart_stacked.php'; /** * Chart functions used to generate various types @@ -57,19 +58,72 @@ function PMA_chart_profiling($data) function PMA_chart_results($data) { $chartData = array(); + $chart = null; + $chartTitle = __('Query results'); - // loop through the rows - foreach ($data as $row) { - - // loop through the columns in the row - foreach ($row as $key => $value) { - $chartData[$key][] = $value; - } + if (!isset($data[0])) { + // empty data + return; + } + + if (count($data[0]) == 2) { + // 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; + } + } + + $chart = new PMA_pChart_bar($chartTitle, $chartData); + } + 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. + $keys = array_keys($data[0]); + $xAxisKey = $keys[0]; + $yAxisKey = $keys[1]; + $seriesKey = $keys[2]; + + // get all the series labels + $seriesLabels = array(); + foreach ($data as $row) { + $seriesLabels[] = $row[$seriesKey]; + } + $seriesLabels = array_unique($seriesLabels); + + // loop through the rows + $currentXLabel = $data[0][$xAxisKey]; + foreach ($data as $row) { + + // save the label + // use the same as the value to get rid of duplicate results + $chartData[$xAxisKey][$row[$xAxisKey]] = $row[$xAxisKey]; + + // make sure to set value to every serie + $currentSeriesLabel = (string)$row[$seriesKey]; + foreach ($seriesLabels as $seriesLabelsValue) { + if ($currentSeriesLabel == $seriesLabelsValue) { + // the value os for this serie + $chartData[$yAxisKey][$seriesLabelsValue][$row[$xAxisKey]] = (int)$row[$yAxisKey]; + } + else if (!isset($chartData[$yAxisKey][$seriesLabelsValue][$row[$xAxisKey]])) { + // if the value for this serie is not set, set it to 0 + $chartData[$yAxisKey][$seriesLabelsValue][$row[$xAxisKey]] = 0; + } + } + } + + $chart = new PMA_pChart_stacked($chartTitle, $chartData); + } + else { + // unknown data + return; } - $chart = new PMA_pChart_bar( - __('Query results'), - $chartData); echo $chart->toString(); } diff --git a/libraries/chart/pma_pchart_bar.php b/libraries/chart/pma_pchart_bar.php index 9e4d88e17..39e385a47 100644 --- a/libraries/chart/pma_pchart_bar.php +++ b/libraries/chart/pma_pchart_bar.php @@ -44,7 +44,7 @@ class PMA_pChart_bar extends PMA_pChart_Chart $this->chart->drawStackedBarGraph($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),70); // Draw the title - $this->chart->drawTextBox(0,0,$this->width,$this->labelHeight,$titleText,0,255,255,255,ALIGN_CENTER,TRUE,0,0,0,30); + $this->chart->drawTextBox(0,0,$this->width,$this->labelHeight,$this->titleText,0,255,255,255,ALIGN_CENTER,TRUE,0,0,0,30); $this->chart->addBorder(2); } diff --git a/libraries/chart/pma_pchart_stacked.php b/libraries/chart/pma_pchart_stacked.php new file mode 100644 index 000000000..b1b541091 --- /dev/null +++ b/libraries/chart/pma_pchart_stacked.php @@ -0,0 +1,67 @@ +data); + $keys = array_keys($this->data); + + // Dataset definition + $this->dataSet = new pData; + $this->dataSet->AddPoint($values[0], "Keys"); + + $i = 0; + foreach ($values[1] as $seriesName => $seriesData) { + $this->dataSet->AddPoint($seriesData, "Values".$i); + $this->dataSet->SetSerieName($seriesName, "Values".$i); + $i++; + } + $this->dataSet->AddAllSeries(); + + $this->dataSet->RemoveSerie("Keys"); + $this->dataSet->SetAbsciseLabelSerie("Keys"); + + $this->dataSet->SetXAxisName($keys[0]); + $this->dataSet->SetYAxisName($keys[1]); + } + + protected function prepareChart() + { + // Initialise the graph + $this->chart = new pChart($this->width, $this->height); + $this->chart->drawGraphAreaGradient(132,173,131,50,TARGET_BACKGROUND); + + $this->chart->setFontProperties($this->fontPath.'tahoma.ttf', 8); + + $legendSize = $this->chart->getLegendBoxSize($this->dataSet->GetDataDescription()); + + $this->chart->setGraphArea($this->areaMargins[3],$this->labelHeight + $this->areaMargins[0],$this->width - $this->areaMargins[1] - $legendSize[0],$this->height - $this->areaMargins[2]); + $this->chart->drawGraphArea(213,217,221,FALSE); + $this->chart->drawScale($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),SCALE_ADDALLSTART0,213,217,221,TRUE,0,2,TRUE); + $this->chart->drawGraphAreaGradient(163,203,167,50); + $this->chart->drawGrid(4,TRUE,230,230,230,20); + + // Draw the bar chart + $this->chart->drawStackedBarGraph($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),70); + + // Draw the title + $this->chart->drawTextBox(0,0,$this->width,$this->labelHeight,$this->titleText,0,255,255,255,ALIGN_CENTER,TRUE,0,0,0,30); + + // Draw the legend + $this->chart->drawLegend($this->width - $this->areaMargins[1] - $legendSize[0] + $this->legendLeftMargin,$this->labelHeight + $this->areaMargins[0],$this->dataSet->GetDataDescription(),250,250,250,50,50,50); + + $this->chart->addBorder(2); + } +} + +?>