added the webui to the tarball

This commit is contained in:
SJ
2012-02-08 23:14:28 +01:00
parent 79cdeed1b6
commit 1211e9a39c
272 changed files with 26456 additions and 11 deletions

View File

@ -0,0 +1,183 @@
<?php
/* Libchart - PHP chart library
* Copyright (C) 2005-2010 Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Base abstract class for rendering both horizontal and vertical bar charts.
*
* @author Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*/
abstract class BarChart extends Chart {
protected $bound;
protected $axis;
protected $hasSeveralSerie;
/**
* Creates a new bar chart.
*
* @param integer width of the image
* @param integer height of the image
*/
protected function BarChart($width, $height) {
parent::Chart($width, $height);
// Initialize the bounds
$this->bound = new Bound();
$this->bound->setLowerBound(0);
}
/**
* Compute the axis.
*/
protected function computeAxis() {
$this->axis = new Axis($this->bound->getYMinValue(), $this->bound->getYMaxValue());
$this->axis->computeBoundaries();
}
/**
* Create the image.
*/
protected function createImage() {
parent::createImage();
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
$primitive = $this->plot->getPrimitive();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
// Aqua-like background
for ($i = $graphArea->y1; $i < $graphArea->y2; $i++) {
$color = $palette->backgroundColor[($i + 3) % 4];
$primitive->line($graphArea->x1, $i, $graphArea->x2, $i, $color);
}
// Axis
imagerectangle($img, $graphArea->x1 - 1, $graphArea->y1, $graphArea->x1, $graphArea->y2, $palette->axisColor[0]->getColor($img));
imagerectangle($img, $graphArea->x1 - 1, $graphArea->y2, $graphArea->x2, $graphArea->y2 + 1, $palette->axisColor[0]->getColor($img));
}
/**
* Returns true if the data set has some data.
* @param minNumberOfPoint Minimum number of points (1 for bars, 2 for lines).
*
* @return true if data set empty
*/
protected function isEmptyDataSet($minNumberOfPoint) {
if ($this->dataSet instanceof XYDataSet) {
$pointList = $this->dataSet->getPointList();
$pointCount = count($pointList);
return $pointCount < $minNumberOfPoint;
} else if ($this->dataSet instanceof XYSeriesDataSet) {
$serieList = $this->dataSet->getSerieList();
reset($serieList);
if (count($serieList) > 0) {
$serie = current($serieList);
$pointList = $serie->getPointList();
$pointCount = count($pointList);
return $pointCount < $minNumberOfPoint;
}
} else {
die("Error: unknown dataset type");
}
}
/**
* Checks the data model before rendering the graph.
*/
protected function checkDataModel() {
// Check if a dataset was defined
if (!$this->dataSet) {
die("Error: No dataset defined.");
}
// Bar charts accept both XYDataSet and XYSeriesDataSet
if ($this->dataSet instanceof XYDataSet) {
// The dataset contains only one serie
$this->hasSeveralSerie = false;
} else if ($this->dataSet instanceof XYSeriesDataSet) {
// Check if each series has the same number of points
unset($lastPointCount);
$serieList = $this->dataSet->getSerieList();
for ($i = 0; $i < count($serieList); $i++) {
$serie = $serieList[$i];
$pointCount = count($serie->getPointList());
if (isset($lastPointCount) && $pointCount != $lastPointCount) {
die("Error: serie <" . $i . "> doesn't have the same number of points as last serie (last one: <" . $lastPointCount. ">, this one: <" . $pointCount. ">).");
}
$lastPointCount = $pointCount;
}
// The dataset contains several series
$this->hasSeveralSerie = true;
} else {
die("Error: Bar chart accept only XYDataSet and XYSeriesDataSet");
}
}
/**
* Return the data as a series list (for consistency).
*
* @return List of series
*/
protected function getDataAsSerieList() {
// Get the data as a series list
$serieList = null;
if ($this->dataSet instanceof XYSeriesDataSet) {
$serieList = $this->dataSet->getSerieList();
} else if ($this->dataSet instanceof XYDataSet) {
$serieList = array();
array_push($serieList, $this->dataSet);
}
return $serieList;
}
/**
* Return the first serie of the list, or the dataSet itself if there is no serie.
*
* @return XYDataSet
*/
protected function getFirstSerieOfList() {
$pointList = null;
if ($this->dataSet instanceof XYSeriesDataSet) {
// For a series dataset, print the legend from the first serie
$serieList = $this->dataSet->getSerieList();
reset($serieList);
$serie = current($serieList);
$pointList = $serie->getPointList();
} else if ($this->dataSet instanceof XYDataSet) {
$pointList = $this->dataSet->getPointList();
}
return $pointList;
}
/**
* Retourns the bound.
*
* @return bound Bound
*/
public function getBound() {
return $this->bound;
}
}
?>

View File

@ -0,0 +1,102 @@
<?php
/* Libchart - PHP chart library
* Copyright (C) 2005-2010 Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*! \mainpage Libchart
*
* This is the reference API, automatically compiled by <a href="http://www.stack.nl/~dimitri/doxygen/">Doxygen</a>.
* You can find here information that is not covered by the <a href="../samplecode/">tutorial</a>.
*
*/
/**
* Base chart class.
*
* @author Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*/
abstract class Chart {
/**
* The data set.
*/
protected $dataSet;
/**
* Plot (holds graphical attributes).
*/
protected $plot;
/**
* Abstract constructor of Chart.
*
* @param integer width of the image
* @param integer height of the image
*/
protected function Chart($width, $height) {
// Creates the plot
$this->plot = new Plot($width, $height);
$this->plot->setTitle("Untitled chart");
//$this->plot->setLogoFileName(dirname(__FILE__) . "/../../../images/PoweredBy.png");
}
/**
* Checks the data model before rendering the graph.
*/
protected function checkDataModel() {
// Check if a dataset was defined
if (!$this->dataSet) {
die("Error: No dataset defined.");
}
// Maybe no points are defined, but that's ok. This will yield and empty graph with default boundaries.
}
/**
* Create the image.
*/
protected function createImage() {
$this->plot->createImage();
}
/**
* Sets the data set.
*
* @param dataSet The data set
*/
public function setDataSet($dataSet) {
$this->dataSet = $dataSet;
}
/**
* Return the plot.
*
* @return plot
*/
public function getPlot() {
return $this->plot;
}
/**
* Sets the title.
*
* @param string New title
*/
public function setTitle($title) {
$this->plot->setTitle($title);
}
}
?>

View File

@ -0,0 +1,217 @@
<?php
/* Libchart - PHP chart library
* Copyright (C) 2005-2010 Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Horizontal bar chart
*
* @author Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*/
class HorizontalBarChart extends BarChart {
/**
* Ratio of empty space beside the bars.
*/
private $emptyToFullRatio;
/**
* Creates a new horizontal bar chart.
*
* @param integer width of the image
* @param integer height of the image
*/
public function HorizontalBarChart($width = 600, $height = 250) {
parent::BarChart($width, $height);
$this->emptyToFullRatio = 1 / 5;
$this->plot->setGraphPadding(new Padding(5, 30, 30, 50));
}
/**
* Computes the layout.
*/
protected function computeLayout() {
if ($this->hasSeveralSerie) {
$this->plot->setHasCaption(true);
}
$this->plot->computeLayout();
}
/**
* Print the axis.
*/
protected function printAxis() {
$minValue = $this->axis->getLowerBoundary();
$maxValue = $this->axis->getUpperBoundary();
$stepValue = $this->axis->getTics();
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
// Horizontal axis
for ($value = $minValue; $value <= $maxValue; $value += $stepValue) {
$x = $graphArea->x1 + ($value - $minValue) * ($graphArea->x2 - $graphArea->x1) / ($this->axis->displayDelta);
imagerectangle($img, $x - 1, $graphArea->y2 + 2, $x, $graphArea->y2 + 3, $palette->axisColor[0]->getColor($img));
imagerectangle($img, $x - 1, $graphArea->y2, $x, $graphArea->y2 + 1, $palette->axisColor[1]->getColor($img));
$text->printText($img, $x, $graphArea->y2 + 5, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_CENTER_ALIGN);
}
// Get first serie of a list
$pointList = $this->getFirstSerieOfList();
// Vertical Axis
$pointCount = count($pointList);
reset($pointList);
$rowHeight = ($graphArea->y2 - $graphArea->y1) / $pointCount;
reset($pointList);
for ($i = 0; $i <= $pointCount; $i++) {
$y = $graphArea->y2 - $i * $rowHeight;
imagerectangle($img, $graphArea->x1 - 3, $y, $graphArea->x1 - 2, $y + 1, $palette->axisColor[0]->getColor($img));
imagerectangle($img, $graphArea->x1 - 1, $y, $graphArea->x1, $y + 1, $palette->axisColor[1]->getColor($img));
if ($i < $pointCount) {
$point = current($pointList);
next($pointList);
$label = $point->getX();
$text->printText($img, $graphArea->x1 - 5, $y - $rowHeight / 2, $this->plot->getTextColor(), $label, $text->fontCondensed, $text->HORIZONTAL_RIGHT_ALIGN | $text->VERTICAL_CENTER_ALIGN);
}
}
}
/**
* Print the bars.
*/
protected function printBar() {
// Get the data as a list of series for consistency
$serieList = $this->getDataAsSerieList();
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
$minValue = $this->axis->getLowerBoundary();
$maxValue = $this->axis->getUpperBoundary();
$stepValue = $this->axis->getTics();
$barColorSet = $palette->barColorSet;
$barColorSet->reset();
$serieCount = count($serieList);
for ($j = 0; $j < $serieCount; $j++) {
$serie = $serieList[$j];
$pointList = $serie->getPointList();
$pointCount = count($pointList);
reset($pointList);
// Get the next color
$color = $barColorSet->currentColor();
$shadowColor = $barColorSet->currentShadowColor();
$barColorSet->next();
$rowHeight = ($graphArea->y2 - $graphArea->y1) / $pointCount;
for ($i = 0; $i < $pointCount; $i++) {
$y = $graphArea->y2 - $i * $rowHeight;
$point = current($pointList);
next($pointList);
$value = $point->getY();
$xmax = $graphArea->x1 + ($value - $minValue) * ($graphArea->x2 - $graphArea->x1) / ($this->axis->displayDelta);
// Bar dimensions
$yWithMargin = $y - $rowHeight * $this->emptyToFullRatio;
$rowWidthWithMargin = $rowHeight * (1 - $this->emptyToFullRatio * 2);
$barWidth = $rowWidthWithMargin / $serieCount;
$barOffset = $barWidth * $j;
$y1 = $yWithMargin - $barWidth - $barOffset;
$y2 = $yWithMargin - $barOffset - 1;
// Text
$text->printText($img, $xmax + 5, $y2 - $barWidth / 2, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->VERTICAL_CENTER_ALIGN);
imagefilledrectangle($img, $graphArea->x1 + 1, $y1, $xmax, $y2, $shadowColor->getColor($img));
// Prevents drawing a small box when x = 0
if ($graphArea->x1 != $xmax) {
imagefilledrectangle($img, $graphArea->x1 + 2, $y1 + 1, $xmax - 4, $y2, $color->getColor($img));
}
}
}
}
/**
* Renders the caption.
*/
protected function printCaption() {
// Get the list of labels
$labelList = $this->dataSet->getTitleList();
// Create the caption
$caption = new Caption();
$caption->setPlot($this->plot);
$caption->setLabelList($labelList);
$palette = $this->plot->getPalette();
$barColorSet = $palette->barColorSet;
$caption->setColorSet($barColorSet);
// Render the caption
$caption->render();
}
/**
* Render the chart image.
*
* @param string name of the file to render the image to (optional)
*/
public function render($fileName = null) {
// Check the data model
$this->checkDataModel();
$this->bound->computeBound($this->dataSet);
$this->computeAxis();
$this->computeLayout();
$this->createImage();
$this->plot->printLogo();
$this->plot->printTitle();
if (!$this->isEmptyDataSet(1)) {
$this->printAxis();
$this->printBar();
if ($this->hasSeveralSerie) {
$this->printCaption();
}
}
$this->plot->render($fileName);
}
}
?>

View File

@ -0,0 +1,200 @@
<?php
/* Libchart - PHP chart library
* Copyright (C) 2005-2010 Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Line chart.
*
* @author Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*/
class LineChart extends BarChart {
/**
* Creates a new line chart.
* Line charts allow for XYDataSet and XYSeriesDataSet in order to plot several lines.
*
* @param integer width of the image
* @param integer height of the image
*/
public function LineChart($width = 600, $height = 250) {
parent::BarChart($width, $height);
$this->plot->setGraphPadding(new Padding(5, 30, 50, 50));
}
/**
* Computes the layout.
*/
protected function computeLayout() {
if ($this->hasSeveralSerie) {
$this->plot->setHasCaption(true);
}
$this->plot->computeLayout();
}
/**
* Print the axis.
*/
protected function printAxis() {
$minValue = $this->axis->getLowerBoundary();
$maxValue = $this->axis->getUpperBoundary();
$stepValue = $this->axis->getTics();
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
// Vertical axis
for ($value = $minValue; $value <= $maxValue; $value += $stepValue) {
$y = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
imagerectangle($img, $graphArea->x1 - 3, $y, $graphArea->x1 - 2, $y + 1, $palette->axisColor[0]->getColor($img));
imagerectangle($img, $graphArea->x1 - 1, $y, $graphArea->x1, $y + 1, $palette->axisColor[1]->getColor($img));
$text->printText($img, $graphArea->x1 - 5, $y, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_RIGHT_ALIGN | $text->VERTICAL_CENTER_ALIGN);
}
// Get first serie of a list
$pointList = $this->getFirstSerieOfList();
// Horizontal Axis
$pointCount = count($pointList);
reset($pointList);
$columnWidth = ($graphArea->x2 - $graphArea->x1) / ($pointCount - 1);
for ($i = 0; $i < $pointCount; $i++) {
$x = $graphArea->x1 + $i * $columnWidth;
imagerectangle($img, $x - 1, $graphArea->y2 + 2, $x, $graphArea->y2 + 3, $palette->axisColor[0]->getColor($img));
imagerectangle($img, $x - 1, $graphArea->y2, $x, $graphArea->y2 + 1, $palette->axisColor[1]->getColor($img));
$point = current($pointList);
next($pointList);
$label = $point->getX();
$text->printDiagonal($img, $x - 5, $graphArea->y2 + 10, $this->plot->getTextColor(), $label);
}
}
/**
* Print the lines.
*/
protected function printLine() {
$minValue = $this->axis->getLowerBoundary();
$maxValue = $this->axis->getUpperBoundary();
// Get the data as a list of series for consistency
$serieList = $this->getDataAsSerieList();
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
$primitive = $this->plot->getPrimitive();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
$lineColorSet = $palette->lineColorSet;
$lineColorSet->reset();
for ($j = 0; $j < count($serieList); $j++) {
$serie = $serieList[$j];
$pointList = $serie->getPointList();
$pointCount = count($pointList);
reset($pointList);
$columnWidth = ($graphArea->x2 - $graphArea->x1) / ($pointCount - 1);
$lineColor = $lineColorSet->currentColor();
$lineColorShadow = $lineColorSet->currentShadowColor();
$lineColorSet->next();
$x1 = null;
$y1 = null;
for ($i = 0; $i < $pointCount; $i++) {
$x2 = $graphArea->x1 + $i * $columnWidth;
$point = current($pointList);
next($pointList);
$value = $point->getY();
$y2 = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
// Draw line
if ($x1) {
$primitive->line($x1, $y1, $x2, $y2, $lineColor, 4);
$primitive->line($x1, $y1 - 1, $x2, $y2 - 1, $lineColorShadow, 2);
}
$x1 = $x2;
$y1 = $y2;
}
}
}
/**
* Renders the caption.
*/
protected function printCaption() {
// Get the list of labels
$labelList = $this->dataSet->getTitleList();
// Create the caption
$caption = new Caption();
$caption->setPlot($this->plot);
$caption->setLabelList($labelList);
$palette = $this->plot->getPalette();
$lineColorSet = $palette->lineColorSet;
$caption->setColorSet($lineColorSet);
// Render the caption
$caption->render();
}
/**
* Render the chart image.
*
* @param string name of the file to render the image to (optional)
*/
public function render($fileName = null) {
// Check the data model
$this->checkDataModel();
$this->bound->computeBound($this->dataSet);
$this->computeAxis();
$this->computeLayout();
$this->createImage();
$this->plot->printLogo();
$this->plot->printTitle();
if (!$this->isEmptyDataSet(2)) {
$this->printAxis();
$this->printLine();
if ($this->hasSeveralSerie) {
$this->printCaption();
}
}
$this->plot->render($fileName);
}
}
?>

View File

@ -0,0 +1,261 @@
<?php
/* Libchart - PHP chart library
* Copyright (C) 2005-2010 Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Pie chart.
*
* @author Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*/
class PieChart extends Chart {
protected $pieCenterX;
protected $pieCenterY;
/**
* Constructor of a pie chart.
*
* @param integer width of the image
* @param integer height of the image
*/
public function PieChart($width = 600, $height = 250) {
parent::Chart($width, $height);
$this->plot->setGraphPadding(new Padding(15, 10, 30, 30));
}
/**
* Computes the layout.
*/
protected function computeLayout() {
$this->plot->setHasCaption(true);
$this->plot->computeLayout();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
// Compute the coordinates of the pie
$this->pieCenterX = $graphArea->x1 + ($graphArea->x2 - $graphArea->x1) / 2;
$this->pieCenterY = $graphArea->y1 + ($graphArea->y2 - $graphArea->y1) / 2;
$this->pieWidth = round(($graphArea->x2 - $graphArea->x1) * 4 / 5);
$this->pieHeight = round(($graphArea->y2 - $graphArea->y1) * 3.7 / 5);
$this->pieDepth = round($this->pieWidth * 0.05);
}
/**
* Compare two sampling point values, order from biggest to lowest value.
*
* @param double first value
* @param double second value
* @return integer result of the comparison
*/
protected function sortPie($v1, $v2) {
return $v1[0] == $v2[0] ? 0 :
$v1[0] > $v2[0] ? -1 :
1;
}
/**
* Compute pie values in percentage and sort them.
*/
protected function computePercent() {
$this->total = 0;
$this->percent = array();
$pointList = $this->dataSet->getPointList();
foreach ($pointList as $point) {
$this->total += $point->getY();
}
foreach ($pointList as $point) {
$percent = $this->total == 0 ? 0 : 100 * $point->getY() / $this->total;
array_push($this->percent, array($percent, $point));
}
usort($this->percent, array("PieChart", "sortPie"));
}
/**
* Creates the pie chart image.
*/
protected function createImage() {
parent::createImage();
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$primitive = $this->plot->getPrimitive();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
// Legend box
$primitive->outlinedBox($graphArea->x1, $graphArea->y1, $graphArea->x2, $graphArea->y2, $palette->axisColor[0], $palette->axisColor[1]);
// Aqua-like background
for ($i = $graphArea->y1 + 2; $i < $graphArea->y2 - 1; $i++) {
$color = $palette->backgroundColor[($i + 3) % 4];
$primitive->line($graphArea->x1 + 2, $i, $graphArea->x2 - 2, $i, $color);
}
}
/**
* Renders the caption.
*/
protected function printCaption() {
// Create a list of labels
$labelList = array();
foreach($this->percent as $percent) {
list($percent, $point) = $percent;
$label = $point->getX();
array_push($labelList, $label);
}
// Create the caption
$caption = new Caption();
$caption->setPlot($this->plot);
$caption->setLabelList($labelList);
$palette = $this->plot->getPalette();
$pieColorSet = $palette->pieColorSet;
$caption->setColorSet($pieColorSet);
// Render the caption
$caption->render();
}
/**
* Draw a 2D disc.
*
* @param integer Center coordinate (y)
* @param array Colors for each portion
* @param bitfield Drawing mode
*/
protected function drawDisc($cy, $colorArray, $mode) {
// Get graphical obects
$img = $this->plot->getImg();
$i = 0;
$oldAngle = 0;
$percentTotal = 0;
foreach ($this->percent as $a) {
list ($percent, $point) = $a;
// If value is null, don't draw this arc
if ($percent <= 0) {
continue;
}
$color = $colorArray[$i % count($colorArray)];
$percentTotal += $percent;
$newAngle = $percentTotal * 360 / 100;
// imagefilledarc doesn't like null values (#1)
if ($newAngle - $oldAngle >= 1) {
imagefilledarc($img, $this->pieCenterX, $cy, $this->pieWidth, $this->pieHeight, $oldAngle, $newAngle, $color->getColor($img), $mode);
}
$oldAngle = $newAngle;
$i++;
}
}
/**
* Print the percentage text.
*/
protected function drawPercent() {
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
$primitive = $this->plot->getPrimitive();
$angle1 = 0;
$percentTotal = 0;
foreach ($this->percent as $a) {
list ($percent, $point) = $a;
// If value is null, the arc isn't drawn, no need to display percent
if ($percent <= 0) {
continue;
}
$percentTotal += $percent;
$angle2 = $percentTotal * 2 * M_PI / 100;
$angle = $angle1 + ($angle2 - $angle1) / 2;
$label = number_format($percent) . "%";
$x = cos($angle) * ($this->pieWidth + 35) / 2 + $this->pieCenterX;
$y = sin($angle) * ($this->pieHeight + 35) / 2 + $this->pieCenterY;
$text->printText($img, $x, $y, $this->plot->getTextColor(), $label, $text->fontCondensed, $text->HORIZONTAL_CENTER_ALIGN | $text->VERTICAL_CENTER_ALIGN);
$angle1 = $angle2;
}
}
/**
* Print the pie chart.
*/
protected function printPie() {
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
$primitive = $this->plot->getPrimitive();
// Get the pie color set
$pieColorSet = $palette->pieColorSet;
$pieColorSet->reset();
// Silhouette
for ($cy = $this->pieCenterY + $this->pieDepth / 2; $cy >= $this->pieCenterY - $this->pieDepth / 2; $cy--) {
$this->drawDisc($cy, $palette->pieColorSet->shadowColorList, IMG_ARC_EDGED);
}
// Top
$this->drawDisc($this->pieCenterY - $this->pieDepth / 2, $palette->pieColorSet->colorList, IMG_ARC_PIE);
// Top Outline
$this->drawPercent();
}
/**
* Render the chart image.
*
* @param string name of the file to render the image to (optional)
*/
public function render($fileName = null) {
$this->computePercent();
$this->computeLayout();
$this->createImage();
$this->plot->printLogo();
$this->plot->printTitle();
$this->printPie();
$this->printCaption();
$this->plot->render($fileName);
}
}
?>

View File

@ -0,0 +1,217 @@
<?php
/* Libchart - PHP chart library
* Copyright (C) 2005-2010 Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Chart composed of vertical bars.
*
* @author Jean-Marc Tr<54>meaux (jm.tremeaux at gmail.com)
*/
class VerticalBarChart extends BarChart {
/**
* Ratio of empty space beside the bars.
*/
private $emptyToFullRatio;
/**
* Creates a new vertical bar chart
*
* @param integer width of the image
* @param integer height of the image
*/
public function VerticalBarChart($width = 600, $height = 250) {
parent::BarChart($width, $height);
$this->emptyToFullRatio = 1 / 5;
$this->plot->setGraphPadding(new Padding(5, 30, 50, 50));
}
/**
* Computes the layout.
*/
protected function computeLayout() {
if ($this->hasSeveralSerie) {
$this->plot->setHasCaption(true);
}
$this->plot->computeLayout();
}
/**
* Print the horizontal and veritcal axis.
*/
protected function printAxis() {
$minValue = $this->axis->getLowerBoundary();
$maxValue = $this->axis->getUpperBoundary();
$stepValue = $this->axis->getTics();
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
// Vertical axis
for ($value = $minValue; $value <= $maxValue; $value += $stepValue) {
$y = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
imagerectangle($img, $graphArea->x1 - 3, $y, $graphArea->x1 - 2, $y + 1, $palette->axisColor[0]->getColor($img));
imagerectangle($img, $graphArea->x1 - 1, $y, $graphArea->x1, $y + 1, $palette->axisColor[1]->getColor($img));
$text->printText($img, $graphArea->x1 - 5, $y, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_RIGHT_ALIGN | $text->VERTICAL_CENTER_ALIGN);
}
// Get first serie of a list
$pointList = $this->getFirstSerieOfList();
// Horizontal Axis
$pointCount = count($pointList);
reset($pointList);
$columnWidth = ($graphArea->x2 - $graphArea->x1) / $pointCount;
for ($i = 0; $i <= $pointCount; $i++) {
$x = $graphArea->x1 + $i * $columnWidth;
imagerectangle($img, $x - 1, $graphArea->y2 + 2, $x, $graphArea->y2 + 3, $palette->axisColor[0]->getColor($img));
imagerectangle($img, $x - 1, $graphArea->y2, $x, $graphArea->y2 + 1, $palette->axisColor[1]->getColor($img));
if ($i < $pointCount) {
$point = current($pointList);
next($pointList);
$label = $point->getX();
$text->printDiagonal($img, $x + $columnWidth * 1 / 3, $graphArea->y2 + 10, $this->plot->getTextColor(), $label);
}
}
}
/**
* Print the bars.
*/
protected function printBar() {
// Get the data as a list of series for consistency
$serieList = $this->getDataAsSerieList();
// Get graphical obects
$img = $this->plot->getImg();
$palette = $this->plot->getPalette();
$text = $this->plot->getText();
// Get the graph area
$graphArea = $this->plot->getGraphArea();
$barColorSet = $palette->barColorSet;
$barColorSet->reset();
$minValue = $this->axis->getLowerBoundary();
$maxValue = $this->axis->getUpperBoundary();
$stepValue = $this->axis->getTics();
$serieCount = count($serieList);
for ($j = 0; $j < $serieCount; $j++) {
$serie = $serieList[$j];
$pointList = $serie->getPointList();
$pointCount = count($pointList);
reset($pointList);
// Get the next color
$color = $barColorSet->currentColor();
$shadowColor = $barColorSet->currentShadowColor();
$barColorSet->next();
$columnWidth = ($graphArea->x2 - $graphArea->x1) / $pointCount;
for ($i = 0; $i < $pointCount; $i++) {
$x = $graphArea->x1 + $i * $columnWidth;
$point = current($pointList);
next($pointList);
$value = $point->getY();
$ymin = $graphArea->y2 - ($value - $minValue) * ($graphArea->y2 - $graphArea->y1) / ($this->axis->displayDelta);
// Bar dimensions
$xWithMargin = $x + $columnWidth * $this->emptyToFullRatio;
$columnWidthWithMargin = $columnWidth * (1 - $this->emptyToFullRatio * 2);
$barWidth = $columnWidthWithMargin / $serieCount;
$barOffset = $barWidth * $j;
$x1 = $xWithMargin + $barOffset;
$x2 = $xWithMargin + $barWidth + $barOffset - 1;
// Text
$text->printText($img, $x1 + $barWidth / 2 , $ymin - 5, $this->plot->getTextColor(), $value, $text->fontCondensed, $text->HORIZONTAL_CENTER_ALIGN | $text->VERTICAL_BOTTOM_ALIGN);
// Vertical bar
imagefilledrectangle($img, $x1, $ymin, $x2, $graphArea->y2 - 1, $shadowColor->getColor($img));
// Prevents drawing a small box when y = 0
if ($ymin != $graphArea->y2) {
imagefilledrectangle($img, $x1 + 1, $ymin + 1, $x2 - 4, $graphArea->y2 - 1, $color->getColor($img));
}
}
}
}
/**
* Renders the caption.
*/
protected function printCaption() {
// Get the list of labels
$labelList = $this->dataSet->getTitleList();
// Create the caption
$caption = new Caption();
$caption->setPlot($this->plot);
$caption->setLabelList($labelList);
$palette = $this->plot->getPalette();
$barColorSet = $palette->barColorSet;
$caption->setColorSet($barColorSet);
// Render the caption
$caption->render();
}
/**
* Render the chart image.
*
* @param string name of the file to render the image to (optional)
*/
public function render($fileName = null) {
// Check the data model
$this->checkDataModel();
$this->bound->computeBound($this->dataSet);
$this->computeAxis();
$this->computeLayout();
$this->createImage();
$this->plot->printLogo();
$this->plot->printTitle();
if (!$this->isEmptyDataSet(1)) {
$this->printAxis();
$this->printBar();
if ($this->hasSeveralSerie) {
$this->printCaption();
}
}
$this->plot->render($fileName);
}
}
?>