Moved settings to an array. Form fields to choose settings for query result chart.

This commit is contained in:
Martynas Mickevicius
2010-06-22 15:25:06 +03:00
parent c5b49bb839
commit eeead42990
7 changed files with 199 additions and 89 deletions

View File

@@ -55,7 +55,7 @@ function PMA_chart_profiling($data)
/* /*
* Formats a chart for query results page. * Formats a chart for query results page.
*/ */
function PMA_chart_results($data) function PMA_chart_results($data, &$chartSettings)
{ {
$chartData = array(); $chartData = array();
$chart = null; $chart = null;
@@ -78,7 +78,7 @@ function PMA_chart_results($data)
} }
} }
$chart = new PMA_pChart_bar($chartTitle, $chartData); $chart = new PMA_pChart_bar($chartTitle, $chartData, $chartSettings);
} }
else if (count($data[0]) == 3) { else if (count($data[0]) == 3) {
// Three columns (x axis, y axis, series) in every row. // Three columns (x axis, y axis, series) in every row.
@@ -117,14 +117,15 @@ function PMA_chart_results($data)
} }
} }
$chart = new PMA_pChart_stacked($chartTitle, $chartData); $chart = new PMA_pChart_stacked($chartTitle, $chartData, $chartSettings);
} }
else { else {
// unknown data // unknown data
return; return;
} }
echo $chart->toString(); $chartSettings = $chart->getSettings();
return $chart->toString();
} }
?> ?>

View File

@@ -2,15 +2,19 @@
class PMA_Chart class PMA_Chart
{ {
/*
* The settings array. All the default values are here.
*/
protected $settings = array(
/* /*
* The style of the chart title. * The style of the chart title.
*/ */
protected $titleStyle = 'font-size: 12px; font-weight: bold;'; 'titleStyle' => 'font-size: 12px; font-weight: bold;',
/* /*
* Colors for the different slices in the pie chart. * Colors for the different slices in the pie chart.
*/ */
protected $colors = array( 'colors' => array(
'#BCE02E', '#BCE02E',
'#E0642E', '#E0642E',
'#E0D62E', '#E0D62E',
@@ -30,22 +34,23 @@ class PMA_Chart
'#4C489B', '#4C489B',
'#1D674A', '#1D674A',
'#87C9BF', '#87C9BF',
); ),
/* /*
* Chart background color. * Chart background color.
*/ */
protected $bgColor = '#E5E5E5'; 'bgColor' => '#E5E5E5',
/* /*
* The width of the chart. * The width of the chart.
*/ */
protected $width = 520; 'width' => 520,
/* /*
* The height of the chart. * The height of the chart.
*/ */
protected $height = 325; 'height' => 325,
);
function __construct($options = null) function __construct($options = null)
{ {
@@ -64,17 +69,37 @@ class PMA_Chart
if (is_null($options)) if (is_null($options))
return; return;
if (isset($options['bgColor'])) $this->settings = array_merge($this->settings, $options);
$this->bgColor = $options['bgColor']; }
if (isset($options['width']))
$this->width = $options['width']; function getTitleStyle()
if (isset($options['height'])) {
$this->height = $options['height']; return $this->settings['titleStyle'];
}
function getColors()
{
return $this->settings['colors'];
}
function getWidth()
{
return $this->settings['width'];
}
function getHeight()
{
return $this->settings['height'];
} }
function getBgColorComp($component) function getBgColorComp($component)
{ {
return hexdec(substr($this->bgColor, ($component * 2) + 1, 2)); return hexdec(substr($this->settings['bgColor'], ($component * 2) + 1, 2));
}
function getSettings()
{
return $this->settings;
} }
} }

View File

@@ -2,12 +2,22 @@
require_once 'pma_pchart_chart.php'; require_once 'pma_pchart_chart.php';
define('TOP', 0);
define('RIGHT', 1);
define('BOTTOM', 2);
define('LEFT', 3);
class PMA_pChart_bar extends PMA_pChart_Chart class PMA_pChart_bar extends PMA_pChart_Chart
{ {
private $labelHeight = 20; public function __construct($titleText, $data, $options = null)
{
parent::__construct($titleText, $data, $options);
$this->settings['labelHeight'] = 20;
// as in CSS (top, right, bottom, left) // as in CSS (top, right, bottom, left)
private $areaMargins = array(20, 20, 40, 60); $this->settings['areaMargins'] = array(20, 20, 40, 60);
}
protected function prepareDataSet() protected function prepareDataSet()
{ {
@@ -30,11 +40,16 @@ class PMA_pChart_bar extends PMA_pChart_Chart
protected function prepareChart() protected function prepareChart()
{ {
// Initialise the graph // Initialise the graph
$this->chart = new pChart($this->width, $this->height); $this->chart = new pChart($this->getWidth(), $this->getHeight());
$this->chart->drawGraphAreaGradient(132,173,131,50,TARGET_BACKGROUND); $this->chart->drawGraphAreaGradient(132,173,131,50,TARGET_BACKGROUND);
$this->chart->setFontProperties($this->fontPath.'tahoma.ttf', 8); $this->chart->setFontProperties($this->getFontPath().'tahoma.ttf', 8);
$this->chart->setGraphArea($this->areaMargins[3],$this->labelHeight + $this->areaMargins[0],$this->width - $this->areaMargins[1],$this->height - $this->areaMargins[2]); $this->chart->setGraphArea(
$this->getAreaMargin(LEFT),
$this->getLabelHeight() + $this->getAreaMargin(TOP),
$this->getWidth() - $this->getAreaMargin(RIGHT),
$this->getHeight() - $this->getAreaMargin(BOTTOM)
);
$this->chart->drawGraphArea(213,217,221,FALSE); $this->chart->drawGraphArea(213,217,221,FALSE);
$this->chart->drawScale($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),SCALE_ADDALL,213,217,221,TRUE,0,2,TRUE); $this->chart->drawScale($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),SCALE_ADDALL,213,217,221,TRUE,0,2,TRUE);
$this->chart->drawGraphAreaGradient(163,203,167,50); $this->chart->drawGraphAreaGradient(163,203,167,50);
@@ -44,10 +59,20 @@ class PMA_pChart_bar extends PMA_pChart_Chart
$this->chart->drawStackedBarGraph($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),70); $this->chart->drawStackedBarGraph($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),70);
// Draw the title // 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); $this->chart->drawTextBox(0,0,$this->getWidth(),$this->getLabelHeight(),$this->titleText,0,255,255,255,ALIGN_CENTER,TRUE,0,0,0,30);
$this->chart->addBorder(2); $this->chart->addBorder(2);
} }
protected function getLabelHeight()
{
return $this->settings['labelHeight'];
}
protected function getAreaMargin($side)
{
return $this->settings['areaMargins'][$side];
}
} }
?> ?>

View File

@@ -18,14 +18,14 @@ abstract class PMA_pChart_Chart extends PMA_Chart
protected $imageEncoded; protected $imageEncoded;
protected $fontPath = './libraries/chart/pChart/fonts/';
public function __construct($titleText, $data, $options = null) public function __construct($titleText, $data, $options = null)
{ {
parent::__construct($options); parent::__construct($options);
$this->titleText = $titleText; $this->titleText = $titleText;
$this->data = $data; $this->data = $data;
$this->settings['fontPath'] = './libraries/chart/pChart/fonts/';
} }
abstract protected function prepareDataSet(); abstract protected function prepareDataSet();
@@ -49,6 +49,11 @@ abstract class PMA_pChart_Chart extends PMA_Chart
return '<img id="pChartPicture1" src="data:image/png;base64,'.$this->imageEncoded.'" />'; return '<img id="pChartPicture1" src="data:image/png;base64,'.$this->imageEncoded.'" />';
} }
protected function getFontPath()
{
return $this->settings['fontPath'];
}
} }
?> ?>

View File

@@ -4,8 +4,13 @@ require_once 'pma_pchart_chart.php';
class PMA_pChart_Pie extends PMA_pChart_Chart class PMA_pChart_Pie extends PMA_pChart_Chart
{ {
private $border1Width = 7; public function __construct($titleText, $data, $options = null)
private $border2Width = 5; {
parent::__construct($titleText, $data, $options);
$this->settings['border1Width'] = 7;
$this->settings['border2Width'] = 8;
}
protected function prepareDataSet() protected function prepareDataSet()
{ {
@@ -20,8 +25,8 @@ class PMA_pChart_Pie extends PMA_pChart_Chart
protected function prepareChart() protected function prepareChart()
{ {
// Initialise the graph // Initialise the graph
$this->chart = new pChart($this->width, $this->height); $this->chart = new pChart($this->getWidth(), $this->getHeight());
foreach ($this->colors as $key => $color) { foreach ($this->getColors() as $key => $color) {
$this->chart->setColorPalette( $this->chart->setColorPalette(
$key, $key,
hexdec(substr($color, 1, 2)), hexdec(substr($color, 1, 2)),
@@ -29,22 +34,22 @@ class PMA_pChart_Pie extends PMA_pChart_Chart
hexdec(substr($color, 5, 2)) hexdec(substr($color, 5, 2))
); );
} }
$this->chart->setFontProperties($this->fontPath.'tahoma.ttf', 8); $this->chart->setFontProperties($this->getFontPath().'tahoma.ttf', 8);
$this->chart->drawFilledRoundedRectangle( $this->chart->drawFilledRoundedRectangle(
$this->border1Width, $this->getBorder1Width(),
$this->border1Width, $this->getBorder1Width(),
$this->width - $this->border1Width, $this->getWidth() - $this->getBorder1Width(),
$this->height - $this->border1Width, $this->getHeight() - $this->getBorder1Width(),
5, 5,
$this->getBgColorComp(0), $this->getBgColorComp(0),
$this->getBgColorComp(1), $this->getBgColorComp(1),
$this->getBgColorComp(2) $this->getBgColorComp(2)
); );
$this->chart->drawRoundedRectangle( $this->chart->drawRoundedRectangle(
$this->border1Width, $this->getBorder2Width(),
$this->border1Width, $this->getBorder2Width(),
$this->width - $this->border1Width, $this->getWidth() - $this->getBorder2Width(),
$this->height - $this->border1Width, $this->getHeight() - $this->getBorder2Width(),
5,0,0,0); 5,0,0,0);
// Draw the pie chart // Draw the pie chart
@@ -58,6 +63,16 @@ class PMA_pChart_Pie extends PMA_pChart_Chart
$this->chart->drawTitle(20,20,$this->titleText,0,0,0); $this->chart->drawTitle(20,20,$this->titleText,0,0,0);
$this->chart->drawPieLegend(350,15,$this->dataSet->GetData(),$this->dataSet->GetDataDescription(),250,250,250); $this->chart->drawPieLegend(350,15,$this->dataSet->GetData(),$this->dataSet->GetDataDescription(),250,250,250);
} }
protected function getBorder1Width()
{
return $this->settings['border1Width'];
}
protected function getBorder2Width()
{
return $this->settings['border2Width'];
}
} }
?> ?>

View File

@@ -1,15 +1,15 @@
<?php <?php
require_once 'pma_pchart_chart.php'; require_once 'pma_pchart_bar.php';
class PMA_pChart_stacked extends PMA_pChart_Chart class PMA_pChart_stacked extends PMA_pChart_bar
{ {
private $labelHeight = 20; public function __construct($titleText, $data, $options = null)
{
parent::__construct($titleText, $data, $options);
// as in CSS (top, right, bottom, left) $this->settings['legendLeftMargin'] = 10;
private $areaMargins = array(20, 20, 40, 60); }
private $legendLeftMargin = 10;
protected function prepareDataSet() protected function prepareDataSet()
{ {
@@ -38,14 +38,19 @@ class PMA_pChart_stacked extends PMA_pChart_Chart
protected function prepareChart() protected function prepareChart()
{ {
// Initialise the graph // Initialise the graph
$this->chart = new pChart($this->width, $this->height); $this->chart = new pChart($this->getWidth(), $this->getHeight());
$this->chart->drawGraphAreaGradient(132,173,131,50,TARGET_BACKGROUND); $this->chart->drawGraphAreaGradient(132,173,131,50,TARGET_BACKGROUND);
$this->chart->setFontProperties($this->fontPath.'tahoma.ttf', 8); $this->chart->setFontProperties($this->getFontPath().'tahoma.ttf', 8);
$legendSize = $this->chart->getLegendBoxSize($this->dataSet->GetDataDescription()); $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->setGraphArea(
$this->getAreaMargin(LEFT),
$this->getLabelHeight() + $this->getAreaMargin(TOP),
$this->getWidth() - $this->getAreaMargin(RIGHT) - $legendSize[0],
$this->getHeight() - $this->getAreaMargin(BOTTOM)
);
$this->chart->drawGraphArea(213,217,221,FALSE); $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->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->drawGraphAreaGradient(163,203,167,50);
@@ -55,13 +60,23 @@ class PMA_pChart_stacked extends PMA_pChart_Chart
$this->chart->drawStackedBarGraph($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),70); $this->chart->drawStackedBarGraph($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),70);
// Draw the title // 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); $this->chart->drawTextBox(0,0,$this->getWidth(),$this->getLabelHeight(),$this->titleText,0,255,255,255,ALIGN_CENTER,TRUE,0,0,0,30);
// Draw the legend // 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->drawLegend(
$this->getWidth() - $this->getAreaMargin(RIGHT) - $legendSize[0] + $this->getLegendMargin(LEFT),
$this->getLabelHeight() + $this->getAreaMargin(TOP),
$this->dataSet->GetDataDescription(),
250,250,250,50,50,50
);
$this->chart->addBorder(2); $this->chart->addBorder(2);
} }
protected function getLegendMargin($side)
{
return $this->settings['legendLeftMargin'];
}
} }
?> ?>

View File

@@ -45,6 +45,15 @@ while ($row = PMA_DBI_fetch_assoc($result)) {
}*/ }*/
} }
// get settings if any posted
$chartSettings = array();
if (PMA_isValid($_REQUEST['chartSettings'], 'array')) {
$chartSettings = $_REQUEST['chartSettings'];
}
// get the chart and settings used to generate chart
$chart = PMA_chart_results($data, $chartSettings);
/** /**
* Displays top menu links * Displays top menu links
* We use db links because a chart is not necessarily on a single table * We use db links because a chart is not necessarily on a single table
@@ -59,17 +68,32 @@ $url_params['reload'] = 1;
* Displays the page * Displays the page
*/ */
?> ?>
<!-- CREATE VIEW options --> <!-- Display Chart options -->
<div id="div_view_options"> <div id="div_view_options">
<form method="post" action="view_create.php"> <form method="post" action="tbl_chart.php">
<?php echo PMA_generate_common_hidden_inputs($url_params); ?> <?php echo PMA_generate_common_hidden_inputs($url_params); ?>
<fieldset> <fieldset>
<legend><?php echo __('Display chart'); ?></legend> <legend><?php echo __('Display chart'); ?></legend>
<?php echo PMA_chart_results($data); ?>
<div style="float: right">
<?php echo $chart; ?>
</div>
<input type="hidden" name="sql_query" id="sql_query" value="<?php echo $sql_query; ?>" />
<table>
<tr><td><label for="width"><?php echo __("Width"); ?></label></td>
<td><input type="text" name="chartSettings[width]" id="width" value="<?php echo $chartSettings['width']; ?>" /></td>
</tr>
<tr><td><label for="height"><?php echo __("Height"); ?></label></td>
<td><input type="text" name="chartSettings[height]" id="height" value="<?php echo $chartSettings['height']; ?>" /></td>
</tr>
</table>
</fieldset> </fieldset>
<fieldset class="tblFooters"> <fieldset class="tblFooters">
<input type="submit" name="displayChart" value="<?php echo __('Go'); ?>" /> <input type="submit" name="displayChart" value="<?php echo __('Redraw'); ?>" />
</fieldset> </fieldset>
</form> </form>
</div> </div>