public function drawAntialiasPixel(Point $point, Color $color, ShadowProperties $shadowProperties, $alpha = 100) { /* Process shadows */ if ($shadowProperties->active) { $this->drawAntialiasPixel(new Point($point->getX() + $shadowProperties->xDistance, $point->getY() + $shadowProperties->yDistance), $shadowProperties->color, ShadowProperties::NoShadow(), $shadowProperties->alpha); if ($shadowProperties->blur != 0) { $AlphaDecay = $shadowProperties->alpha / $shadowProperties->blur; for ($i = 1; $i <= $shadowProperties->blur; $i++) { $this->drawAntialiasPixel(new Point($point->getX() + $shadowProperties->xDistance - $i / 2, $point->getY() + $shadowProperties->yDistance - $i / 2), $shadowProperties->color, ShadowProperties::NoShadow(), $shadowProperties->alpha - $AlphaDecay * $i); } for ($i = 1; $i <= $shadowProperties->blur; $i++) { $this->drawAntialiasPixel(new Point($point->getX() + $shadowProperties->xDistance + $i / 2, $point->getY() + $shadowProperties->yDistance + $i / 2), $shadowProperties->color, ShadowProperties::NoShadow(), $shadowProperties->alpha - $AlphaDecay * $i); } } } $Xi = floor($point->getX()); $Yi = floor($point->getY()); if ($Xi == $point->getX() && $Yi == $point->getY()) { if ($alpha == 100) { $C_Aliased = $this->allocateColor($color); imagesetpixel($this->picture, $point->getX(), $point->getY(), $C_Aliased); } else { $this->drawAlphaPixel($point, $alpha, $color); } } else { $Alpha1 = (1 - ($point->getX() - $Xi)) * (1 - ($point->getY() - $Yi)) * 100 / 100 * $alpha; if ($Alpha1 > $this->antialiasQuality) { $this->drawAlphaPixel(new Point($Xi, $Yi), $Alpha1, $color); } $Alpha2 = ($point->getX() - $Xi) * (1 - ($point->getY() - $Yi)) * 100 / 100 * $alpha; if ($Alpha2 > $this->antialiasQuality) { $this->drawAlphaPixel(new Point($Xi + 1, $Yi), $Alpha2, $color); } $Alpha3 = (1 - ($point->getX() - $Xi)) * ($point->getY() - $Yi) * 100 / 100 * $alpha; if ($Alpha3 > $this->antialiasQuality) { $this->drawAlphaPixel(new Point($Xi, $Yi + 1), $Alpha3, $color); } $Alpha4 = ($point->getX() - $Xi) * ($point->getY() - $Yi) * 100 / 100 * $alpha; if ($Alpha4 > $this->antialiasQuality) { $this->drawAlphaPixel(new Point($Xi + 1, $Yi + 1), $Alpha4, $color); } } }
public function testDrawArea() { // Dataset definition $DataSet = new pData(); $DataSet->addPoints(array(10, 9.4, 7.7, 5, 1.7, -1.7, -5, -7.7, -9.4, -10, -9.4, -7.7, -5, -1.8, 1.7), "Serie1"); $DataSet->addPoints(array(0, 3.4, 6.4, 8.699999999999999, 9.800000000000001, 9.800000000000001, 8.699999999999999, 6.4, 3.4, 0, -3.4, -6.4, -8.6, -9.800000000000001, -9.9), "Serie2"); $DataSet->addPoints(array(7.1, 9.1, 10, 9.699999999999999, 8.199999999999999, 5.7, 2.6, -0.9, -4.2, -7.1, -9.1, -10, -9.699999999999999, -8.199999999999999, -5.8), "Serie3"); $DataSet->addPoints(array("Jan", "Jan", "Jan", "Feb", "Feb", "Feb", "Mar", "Mar", "Mar", "Apr", "Apr", "Apr", "May", "May", "May"), "Serie4"); $DataSet->AddAllSeries(); $DataSet->SetAbscissaLabelSeries("Serie4"); $DataSet->SetSeriesName("Max Average", "Serie1"); $DataSet->SetSeriesName("Min Average", "Serie2"); $DataSet->SetSeriesName("Temperature", "Serie3"); $DataSet->SetYAxisName("Temperature"); $DataSet->SetXAxisName("Month of the year"); // Initialise the graph $canvas = new TestCanvas(); $Test = new pChart(700, 230, $canvas); $Test->reportWarnings("GD"); $Test->setFixedScale(-12, 12, 5); $Test->setFontProperties("Fonts/tahoma.ttf", 8); $Test->setGraphArea(65, 30, 570, 185); $canvas->drawFilledRoundedRectangle(new Point(7, 7), new Point(693, 223), 5, new Color(240), 1, 0, ShadowProperties::NoShadow()); $backgroundStyle = new BackgroundStyle(new Color(255), TRUE); $Test->drawGraphBackground($backgroundStyle); $scaleStyle = new ScaleStyle(SCALE_NORMAL, new Color(150)); $Test->drawScale($DataSet, $scaleStyle, 0, 2, TRUE, 3); $Test->drawGrid(new GridStyle(4, TRUE, new Color(230), 50)); // Draw the 0 line $Test->setFontProperties("Fonts/tahoma.ttf", 6); $Test->drawTreshold(0, new Color(143, 55, 72), TRUE, TRUE); // Draw the area $DataSet->RemoveSeries("Serie4"); $Test->drawArea($DataSet->GetData(), "Serie1", "Serie2", new Color(239, 238, 227), 50); $DataSet->RemoveSeries("Serie3"); $Test->drawLineGraph($DataSet->GetData(), $DataSet->GetDataDescription()); // Draw the line graph $Test->setLineStyle(1, 6); $DataSet->RemoveAllSeries(); $DataSet->AddSeries("Serie3"); $Test->drawLineGraph($DataSet->GetData(), $DataSet->GetDataDescription()); $Test->drawPlotGraph($DataSet->GetData(), $DataSet->GetDataDescription(), 3, 2, new Color(255)); // Write values on Serie3 $Test->setFontProperties("Fonts/tahoma.ttf", 8); $Test->writeValues($DataSet->GetData(), $DataSet->GetDataDescription(), "Serie3"); // Finish the graph $Test->setFontProperties("Fonts/tahoma.ttf", 8); $Test->drawLegend(590, 90, $DataSet->GetDataDescription(), new Color(255)); $Test->setFontProperties("Fonts/tahoma.ttf", 10); $Test->drawTitle(60, 22, "example 15", new Color(50), 585); // Add an image $Test->drawFromPNG(dirname(__FILE__) . "/../Sample/logo.png", 584, 35); $this->assertEquals('53b34d556af518230b7556e19349bd94', md5($canvas->getActionLog())); }
/** * This function draw a flat pie chart * @param array Data (PieChart->getData()) * @param array Description (PieChart->getDataDescription()) * @param int X-Position of the Center * @param int Y-Position of the Center * @param int Radius of the cake * @param const int Draw the Labels to the pies? PIE_LABELS, PIE_NOLABEL, PIE_PERCENTAGE, PIE_PERCENATGE_LABEL * @param int Distance between the splices * @param int number of decimals * @param ShadowProperties * @access public */ public function drawBasicPieGraph($Data, $DataDescription, $XPos, $YPos, $Radius = 100, $DrawLabels = PIE_NOLABEL, Color $color = null, $Decimals = 0, ShadowProperties $shadowProperties = null) { if ($shadowProperties == null) { $shadowProperties = ShadowProperties::NoShadow(); } if (empty($DataDescription->values)) { throw new Exception("No values available in data description in drawBasicPieGraph()"); } if ($color == null) { $color = new Color(255, 255, 255); } /* Validate the Data and DataDescription array */ $this->validateDataDescription("drawBasicPieGraph", $DataDescription, FALSE); $this->validateData("drawBasicPieGraph", $Data); /* Determine pie sum */ $Series = 0; $PieSum = 0; foreach ($DataDescription->values as $Key2 => $ColName) { if ($ColName != $DataDescription->getPosition()) { $Series++; foreach (array_keys($Data) as $Key) { if (isset($Data[$Key][$ColName])) { $PieSum = $PieSum + $Data[$Key][$ColName]; } $iValues[] = $Data[$Key][$ColName]; } } } /* Validate serie */ if ($Series != 1) { throw new Exception("Pie chart can only accept one serie of data."); } /** @todo Proper exception type needed here */ $SpliceRatio = 360 / $PieSum; $SplicePercent = 100 / $PieSum; /* Calculate all polygons */ $Angle = 0; $TopPlots = ""; foreach ($iValues as $Key => $Value) { $TopPlots[$Key][] = $XPos; $TopPlots[$Key][] = $YPos; /* Process labels position & size */ $this->processLabelsPositionAndSize($DrawLabels, $Angle, $Value, $SpliceRatio, $SplicePercent, 0, $Decimals, $Radius, $XPos, $YPos, $shadowProperties); /* Process pie slices */ for ($iAngle = $Angle; $iAngle <= $Angle + $Value * $SpliceRatio; $iAngle = $iAngle + 0.5) { $TopX = cos($iAngle * M_PI / 180) * $Radius + $XPos; $TopY = sin($iAngle * M_PI / 180) * $Radius + $YPos; $TopPlots[$Key][] = $TopX; $TopPlots[$Key][] = $TopY; } $TopPlots[$Key][] = $XPos; $TopPlots[$Key][] = $YPos; $Angle = $iAngle; } $PolyPlots = $TopPlots; /* Set array values type to float --- PHP Bug with * imagefilledpolygon casting to integer */ foreach ($TopPlots as $Key => $Value) { foreach (array_keys($TopPlots[$Key]) as $Key2) { settype($TopPlots[$Key][$Key2], "float"); } } /* Draw Top polygons */ foreach ($PolyPlots as $Key => $Value) { $this->canvas->drawFilledPolygon($PolyPlots[$Key], (count($PolyPlots[$Key]) + 1) / 2, $this->palette->colors[$Key]); } $this->canvas->drawCircle(new Point($XPos - 0.5, $YPos - 0.5), $Radius, $color, $shadowProperties); $this->canvas->drawCircle(new Point($XPos - 0.5, $YPos - 0.5), $Radius + 0.5, $color, $shadowProperties); /* Draw Top polygons */ foreach ($TopPlots as $Key => $Value) { for ($j = 0; $j <= count($TopPlots[$Key]) - 4; $j = $j + 2) { $this->canvas->drawLine(new Point($TopPlots[$Key][$j], $TopPlots[$Key][$j + 1]), new Point($TopPlots[$Key][$j + 2], $TopPlots[$Key][$j + 3]), $color, $this->LineWidth, $this->LineDotSize, $shadowProperties); } } }
require_once "../lib/CSVImporter.php"; require_once '../lib/GDCanvas.php'; require_once '../lib/BackgroundStyle.php'; // Definitions $DataSet = new pData(); $canvas = new GDCanvas(700, 230); $Chart = new pChart(700, 230, $canvas); // Dataset definition CSVImporter::ImportFromCSV($DataSet, "../sample/datawithtitle.csv", ",", array(1, 2, 3), TRUE, 0); $DataSet->AddAllSeries(); $DataSet->setAbscissaLabelSeries(); // Initialise the graph $Chart->setFontProperties("../Fonts/tahoma.ttf", 8); $Chart->setGraphArea(60, 30, 680, 200); $canvas->drawFilledRoundedRectangle(new Point(7, 7), new Point(693, 223), 5, new Color(240), 1, 0, ShadowProperties::NoShadow()); $canvas->drawRoundedRectangle(new Point(5, 5), new Point(695, 225), 5, new Color(230), 1, 0, ShadowProperties::NoShadow()); $Chart->drawGraphBackground(new BackgroundStyle(new Color(255), TRUE)); $Chart->drawScale($DataSet, ScaleStyle::DefaultStyle(), 0, 2); $Chart->drawGrid(new GridStyle(4, TRUE, new Color(230, 230, 230), TRUE)); // Draw the 0 line $Chart->setFontProperties("../Fonts/tahoma.ttf", 6); $Chart->drawTreshold(0, new Color(143, 55, 72), TRUE, TRUE); // Draw the area $Chart->drawArea($DataSet, "Serie 1", "Serie 3", new Color(239, 238, 227)); // Draw the line graph $Chart->drawLineGraph($DataSet->GetData(), $DataSet->GetDataDescription()); $Chart->drawPlotGraph($DataSet->GetData(), $DataSet->GetDataDescription(), 3, 2, new Color(255)); // Finish the graph $Chart->setFontProperties("../Fonts/tahoma.ttf", 8); $Chart->drawLegend(65, 35, $DataSet->GetDataDescription(), new Color(250)); $Chart->setFontProperties("../Fonts/tahoma.ttf", 10);
$DataSet = new pData(); $Canvas = new GDCanvas(700, 230); $Chart = new pChart(700, 230, $Canvas); // Dataset $DataSet->AddPoints(array(1, 4, -3, 2, -3, 3, 2, 1, 0, 7, 4), "Serie1"); $DataSet->AddPoints(array(3, 3, -4, 1, -2, 2, 1, 0, -1, 6, 3), "Serie2"); $DataSet->AddPoints(array(4, 1, 2, -1, -4, -2, 3, 2, 1, 2, 2), "Serie3"); $DataSet->AddAllSeries(); $DataSet->SetAbscissaLabelSeries(); $DataSet->SetSeriesName("January", "Serie1"); $DataSet->SetSeriesName("February", "Serie2"); $DataSet->SetSeriesName("March", "Serie3"); // Initialise the graph $Chart->setFontProperties("../Fonts/tahoma.ttf", 8); $Chart->setGraphArea(50, 30, 680, 200); $Canvas->drawFilledRoundedRectangle(new Point(7, 7), new Point(693, 223), 5, new Color(240), 1, 0, ShadowProperties::NoShadow()); $Chart->drawGraphBackground(new BackgroundStyle(new Color(255), TRUE)); $Chart->drawScale($DataSet, ScaleStyle::DefaultStyle(), 0, 2, TRUE); $Chart->drawGrid(new GridStyle(4, TRUE, new Color(230), 80)); // Draw the 0 line $Chart->setFontProperties("../Fonts/tahoma.ttf", 6); $Chart->drawTreshold(0, new Color(143, 55, 72), TRUE, TRUE); // Draw the bar graph $Chart->drawBarGraph($DataSet->GetData(), $DataSet->GetDataDescription(), 80); // Finish the graph $Chart->setFontProperties("../Fonts/tahoma.ttf", 8); $Chart->drawLegend(596, 150, $DataSet->GetDataDescription(), new Color(255)); $Chart->setFontProperties("../Fonts/tahoma.ttf", 10); $Chart->drawTitle(50, 22, "Example 12", new Color(50), 585); $Chart->Render("Example12.png"); header("Content-Type:image/png");
/** * Print all error messages on the CLI or graphically */ function printErrors($Mode = "CLI") { if (count($this->Errors) == 0) { return 0; } if ($Mode == "CLI") { foreach ($this->Errors as $key => $Value) { echo $Value . "\r\n"; } } elseif ($Mode == "GD") { $MaxWidth = 0; foreach ($this->Errors as $key => $Value) { $Position = imageftbbox($this->ErrorFontSize, 0, $this->ErrorFontName, $Value); $TextWidth = $Position[2] - $Position[0]; if ($TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; } } $this->canvas->drawFilledRoundedRectangle(new Point($this->XSize - ($MaxWidth + 20), $this->YSize - (20 + ($this->ErrorFontSize + 4) * count($this->Errors))), new Point($this->XSize - 10, $this->YSize - 10), 6, new Color(233, 185, 185), $this->lineWidth, $this->lineDotSize, $this->shadowProperties); $this->canvas->drawRoundedRectangle(new Point($this->XSize - ($MaxWidth + 20), $this->YSize - (20 + ($this->ErrorFontSize + 4) * count($this->Errors))), new Point($this->XSize - 10, $this->YSize - 10), 6, new Color(193, 145, 145), $this->LineWidth, $this->LineDotSize, $this->shadowProperties); $YPos = $this->YSize - (18 + (count($this->Errors) - 1) * ($this->ErrorFontSize + 4)); foreach ($this->Errors as $key => $Value) { $this->canvas->drawText($this->ErrorFontSize, 0, new Point($this->XSize - ($MaxWidth + 15), $YPos), new Color(133, 85, 85), $this->ErrorFontName, $Value, ShadowProperties::NoShadow()); $YPos = $YPos + ($this->ErrorFontSize + 4); } } }