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())); }
<?php /* Example13: A 2D exploded pie graph */ // Standard inclusions require_once "../lib/pData.php"; require_once "../lib/pChart.php"; require_once '../lib/GDCanvas.php'; require_once '../lib/BackgroundStyle.php'; require_once '../lib/PieChart.php'; // Definitions $DataSet = new pData(); $Canvas = new GDCanvas(300, 200); $Chart = new PieChart(300, 200, $Canvas); // Dataset $DataSet->AddPoints(array(10, 2, 3, 5, 3), "Serie1"); $DataSet->AddPoints(array("Jan", "Feb", "Mar", "Apr", "May"), "Serie2"); $DataSet->AddAllSeries(); $DataSet->SetAbscissaLabelSeries("Serie2"); // Initialise the graph $Chart->setFontProperties("../Fonts/tahoma.ttf", 8); // Draw the pie chart $shadowProperties = ShadowProperties::FromSettings(2, 2, new Color(200)); $Chart->drawFlatPieGraphWithShadow($DataSet->GetData(), $DataSet->GetDataDescription(), 120, 100, 60, PIE_PERCENTAGE, 8, 0, $shadowProperties); $Chart->drawPieLegend(230, 15, $DataSet->GetData(), $DataSet->GetDataDescription(), new Color(250)); $Chart->Render("Example13.png"); header("Content-Type:image/png"); readfile("Example13.png");
/** * This function draw a pseudo-3D pie chart * @param pData * @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 bool Enhance colors? * @param int Skew * @param int Height of the splices * @param int Distance between the splices * @param int number of decimals * @param ShadowProperties * @access public */ public function drawPieGraph(pData $data, $XPos, $YPos, $Radius = 100, $DrawLabels = PIE_NOLABEL, $EnhanceColors = TRUE, $Skew = 60, $SpliceHeight = 20, $SpliceDistance = 0, $Decimals = 0, ShadowProperties $shadowProperties = null) { if ($shadowProperties == null) { $shadowProperties = ShadowProperties::FromDefaults(); } /* Validate the Data and DataDescription array */ $this->validateDataDescription("drawPieGraph", $data->getDataDescription(), FALSE); $this->validateData("drawPieGraph", $data->getData()); /* Determine pie sum */ $Series = 0; $PieSum = 0; $rPieSum = 0; foreach ($data->getDataDescription()->values as $ColName) { if ($ColName != $data->getDataDescription()->getPosition()) { $Series++; $dataArray = $data->getData(); foreach (array_keys($dataArray) as $Key) { if (isset($dataArray[$Key][$ColName])) { if ($dataArray[$Key][$ColName] == 0) { $iValues[] = 0; $rValues[] = 0; $iLabels[] = $dataArray[$Key][$data->getDataDescription()->getPosition()]; } else { $PieSum += $dataArray[$Key][$ColName]; $iValues[] = $dataArray[$Key][$ColName]; $iLabels[] = $dataArray[$Key][$data->getDataDescription()->getPosition()]; $rValues[] = $dataArray[$Key][$ColName]; $rPieSum += $dataArray[$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 */ $SpliceDistanceRatio = $SpliceDistance; $SkewHeight = $Radius * $Skew / 100; $SpliceRatio = (360 - $SpliceDistanceRatio * count($iValues)) / $PieSum; $SplicePercent = 100 / $PieSum; $rSplicePercent = 100 / $rPieSum; /* Calculate all polygons */ $Angle = 0; $CDev = 5; $TopPlots = ""; $BotPlots = ""; $aTopPlots = ""; $aBotPlots = ""; foreach ($iValues as $Key => $Value) { $XCenterPos = cos(($Angle - $CDev + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * M_PI / 180) * $SpliceDistance + $XPos; $YCenterPos = sin(($Angle - $CDev + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * M_PI / 180) * $SpliceDistance + $YPos; $XCenterPos2 = cos(($Angle + $CDev + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * M_PI / 180) * $SpliceDistance + $XPos; $YCenterPos2 = sin(($Angle + $CDev + ($Value * $SpliceRatio + $SpliceDistanceRatio) / 2) * M_PI / 180) * $SpliceDistance + $YPos; $TopPlots[$Key][] = round($XCenterPos); $BotPlots[$Key][] = round($XCenterPos); $TopPlots[$Key][] = round($YCenterPos); $BotPlots[$Key][] = round($YCenterPos + $SpliceHeight); $aTopPlots[$Key][] = $XCenterPos; $aBotPlots[$Key][] = $XCenterPos; $aTopPlots[$Key][] = $YCenterPos; $aBotPlots[$Key][] = $YCenterPos + $SpliceHeight; /* Process labels position & size */ $Caption = ""; if (!($DrawLabels == PIE_NOLABEL)) { $TAngle = $Angle + $Value * $SpliceRatio / 2; if ($DrawLabels == PIE_PERCENTAGE) { $Caption = round($rValues[$Key] * pow(10, $Decimals) * $rSplicePercent) / pow(10, $Decimals) . "%"; } elseif ($DrawLabels == PIE_LABELS) { $Caption = $iLabels[$Key]; } elseif ($DrawLabels == PIE_PERCENTAGE_LABEL) { $Caption = $iLabels[$Key] . "\r\n" . round($Value * pow(10, $Decimals) * $SplicePercent) / pow(10, $Decimals) . "%"; } $Position = imageftbbox($this->FontSize, 0, $this->FontName, $Caption); $TextWidth = $Position[2] - $Position[0]; $TextHeight = abs($Position[1]) + abs($Position[3]); $TX = cos($TAngle * M_PI / 180) * ($Radius + 10) + $XPos; if ($TAngle > 0 && $TAngle < 180) { $TY = sin($TAngle * M_PI / 180) * ($SkewHeight + 10) + $YPos + $SpliceHeight + 4; } else { $TY = sin($TAngle * M_PI / 180) * ($SkewHeight + 4) + $YPos - $TextHeight / 2; } if ($TAngle > 90 && $TAngle < 270) { $TX = $TX - $TextWidth; } $this->canvas->drawText($this->FontSize, 0, new Point($TX, $TY), new Color(70, 70, 70), $this->FontName, $Caption, $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) * $SkewHeight + $YPos; $TopPlots[$Key][] = round($TopX); $BotPlots[$Key][] = round($TopX); $TopPlots[$Key][] = round($TopY); $BotPlots[$Key][] = round($TopY + $SpliceHeight); $aTopPlots[$Key][] = $TopX; $aBotPlots[$Key][] = $TopX; $aTopPlots[$Key][] = $TopY; $aBotPlots[$Key][] = $TopY + $SpliceHeight; } $TopPlots[$Key][] = round($XCenterPos2); $BotPlots[$Key][] = round($XCenterPos2); $TopPlots[$Key][] = round($YCenterPos2); $BotPlots[$Key][] = round($YCenterPos2 + $SpliceHeight); $aTopPlots[$Key][] = $XCenterPos2; $aBotPlots[$Key][] = $XCenterPos2; $aTopPlots[$Key][] = $YCenterPos2; $aBotPlots[$Key][] = $YCenterPos2 + $SpliceHeight; $Angle = $iAngle + $SpliceDistanceRatio; } $this->drawPieGraphBottomPolygons($iValues, $BotPlots, $EnhanceColors, $aBotPlots, $shadowProperties); $this->drawPieGraphLayers($iValues, $TopPlots, $EnhanceColors, $SpliceHeight, $this->palette, $shadowProperties); $this->drawPieGraphTopPolygons($iValues, $TopPlots, $EnhanceColors, $aTopPlots, $shadowProperties); }
/** * Instantiate a new ShadowProperties with the same settings as * the one passed in. Essentially this is a clone method. * * @todo clone appears to be a reserved word in PHP, is there * actually any special clone functionality? I can't RTFM at the * moment as the internet connection is down. */ public static function Copy(ShadowProperties $other) { $copy = ShadowProperties::FromSettings($other->xDistance, $other->yDistance, $other->color, $other->alpha, $other->blur); $copy->active = $other->active; return $copy; }
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); } } }