added stacked bar chart

This commit is contained in:
Martynas Mickevicius
2010-06-22 14:24:23 +03:00
parent 12d875dadc
commit c5b49bb839
3 changed files with 132 additions and 11 deletions

View File

@@ -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');
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(
__('Query results'),
$chartData);
$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;
}
echo $chart->toString();
}

View File

@@ -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);
}

View File

@@ -0,0 +1,67 @@
<?php
require_once 'pma_pchart_chart.php';
class PMA_pChart_stacked extends PMA_pChart_Chart
{
private $labelHeight = 20;
// as in CSS (top, right, bottom, left)
private $areaMargins = array(20, 20, 40, 60);
private $legendLeftMargin = 10;
protected function prepareDataSet()
{
$values = array_values($this->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);
}
}
?>