/** * Draw pie chart * @return unknown */ protected function drawPie() { // prepare font & series $this->_prepareSerie(); $this->_prepareFont(); // init chart params $outer_w = $this->w - 5; // Outer frame witdh $outer_h = $this->h - 5; // Outer frame heigth $inner_w = $this->w - 7; // Inner frame witdh $inner_h = $this->h - 7; // Inner frame heigth $pie_x = intval(($this->w - 150) / 2); // Pie witdh $pie_y = intval(($this->h - 10) / 2); // Pie heigth $pie_r = intval($pie_x - 50); // Pie radius $title_w = $this->w - 200; // Title width $title_h = 50; // Title height $legend_w = $this->w - 120; // Legend width $legend_h = 50; // Legend height // chart styles $flat = isset($this->p['flat']) ? $this->p['flat'] : false; // fill chart $this->chart->drawBackground(255, 255, 255); $this->chart->setFontProperties($this->font, 7); // set font and size $this->chart->drawRoundedRectangle(5, 5, $outer_w, $outer_h, 10, 230, 230, 230); // drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B) $this->chart->drawFilledRoundedRectangle(7, 7, $inner_w, $inner_h, 10, 240, 240, 240); // drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B) // draw the pie chart $this->chart->setFontProperties($this->font, 8); // flat pie if ($flat) { $this->chart->drawFlatPieGraphWithShadow($this->data->GetData(), $this->data->GetDataDescription(), $pie_x, $pie_y, $pie_r, PIE_PERCENTAGE, 10); // 3d pie } else { $this->chart->drawPieGraph($this->data->GetData(), $this->data->GetDataDescription(), $pie_x, $pie_y, $pie_r, PIE_PERCENTAGE, TRUE, 50, 20, 5, 1); } $this->chart->drawPieLegend($legend_w, $legend_h, $this->data->GetData(), $this->data->GetDataDescription(), 250, 250, 250); // add title $this->chart->setFontProperties($this->font, 10); $this->chart->drawTitle(40, 0, $this->title, 50, 50, 50, $title_w, $title_h); // drawTitle($XPos,$YPos,$Value,$R,$G,$B,$XPos2=-1,$YPos2=-1,$Shadow=FALSE) }
function pie($valores, $label, $titulo) { $nombre = tempnam('/tmp', 'g') . '.png'; $DataSet = new pData(); $DataSet->AddPoint($valores, 'Serie1'); $DataSet->AddPoint($label, 'Serie2'); $DataSet->AddAllSeries(); $DataSet->SetAbsciseLabelSerie('Serie2'); $Test = new pChart(300, 200); $Test->setFontProperties(APPPATH . 'libraries/pChart/Fonts/tahoma.ttf', 8); $Test->drawFilledRoundedRectangle(7, 7, 293, 193, 5, 240, 240, 240); $Test->drawRoundedRectangle(5, 5, 295, 195, 5, 230, 230, 230); $Test->AntialiasQuality = 0; $Test->setShadowProperties(2, 2, 200, 200, 200); $Test->drawFlatPieGraphWithShadow($DataSet->GetData(), $DataSet->GetDataDescription(), 120, 110, 60, PIE_PERCENTAGE, 8); $Test->clearShadow(); $Test->drawPieLegend(210, 30, $DataSet->GetData(), $DataSet->GetDataDescription(), 250, 250, 250); $Test->drawTitle(10, 22, $titulo, 50, 50, 50, 300); $Test->Render($nombre); return $nombre; }
<?php /* Example13: A 2D exploded pie graph */ // Standard inclusions include "pChart/pData.class"; include "pChart/pChart.class"; // Dataset definition $DataSet = new pData(); $DataSet->AddPoint(array(10, 2, 3, 5, 3), "Serie1"); $DataSet->AddPoint(array("Jan", "Feb", "Mar", "Apr", "May"), "Serie2"); $DataSet->AddAllSeries(); $DataSet->SetAbsciseLabelSerie("Serie2"); // Initialise the graph $Test = new pChart(300, 200); $Test->setFontProperties("Fonts/tahoma.ttf", 8); $Test->drawFilledRoundedRectangle(7, 7, 293, 193, 5, 240, 240, 240); $Test->drawRoundedRectangle(5, 5, 295, 195, 5, 230, 230, 230); // Draw the pie chart $Test->AntialiasQuality = 0; $Test->setShadowProperties(2, 2, 200, 200, 200); $Test->drawFlatPieGraphWithShadow($DataSet->GetData(), $DataSet->GetDataDescription(), 120, 100, 60, PIE_PERCENTAGE, 8); $Test->clearShadow(); $Test->drawPieLegend(230, 15, $DataSet->GetData(), $DataSet->GetDataDescription(), 250, 250, 250); $Test->Render("example13.png");
$Test->setGraphArea(0, 0, $w, $h); //---------------------------- Set background graphics (R,G,G,1/Y) if ($bg_r != 0 && $bg_g != 0 && $bg_b != 0) { $Test->drawGraphAreaGradient($bg_r, $bg_g, $bg_b, 5); } //---------------------------- Calc center $midX = $w / 2; $midY = $h / 2; //---------------------------- Draw pie if ($_GET['gtype'] == 'pie') { $Test->drawBasicPieGraph($DataSet->GetData(), $DataSet->GetDataDescription(), $midX, $midY, $radius, PIE_PERCENTAGE_LABEL, $cbg, $cbg, $cbg, 1); } elseif ($_GET['gtype'] == 'pie2') { $Test->drawFlatPieGraph($DataSet->GetData(), $DataSet->GetDataDescription(), $midX, $midY, $radius, PIE_PERCENTAGE_LABEL, 5, 1); } elseif ($_GET['gtype'] == 'pie3') { $Test->setShadowProperties(3, 3, $csh, $csh, $csh, 90); $Test->drawFlatPieGraphWithShadow($DataSet->GetData(), $DataSet->GetDataDescription(), $midX, $midY, $radius, PIE_PERCENTAGE_LABEL, 5, 1); } elseif ($_GET['gtype'] == 'pie4') { $Test->drawPieGraph($DataSet->GetData(), $DataSet->GetDataDescription(), $midX, $midY, $radius, PIE_PERCENTAGE_LABEL, TRUE, 60, 20, 5, 1); } else { // } //---------------------------- Border if ($_GET['border'] == '1') { $Test->AddBorder(1, 200, 200, 200); } //---------------------------- Image PNG Header("Content-type:image/png"); imagepng($Test->Picture); //$Test->Render(); //---------------------------- Disconnect $db->Disconnect();
function pieChart($r, $percents, $legend, $imgname = '', $title = '', $bottom_label = '', $config = array()) { $settings = array('Skew' => 90, 'SpliceHeight' => 10, 'SpliceDistance' => 0, 'PieFontSize' => 8, 'TitleFontSize' => 10, 'LegendFontSize' => 8, 'LabelFontSize' => 10, 'PieFontName' => 'tahoma.ttf', 'TitleFontName' => 'tahoma.ttf', 'LegendFontName' => 'tahoma.ttf', 'LabelFontName' => 'tahoma.ttf', 'TitleBGR' => 0, 'TitleBGG' => 0, 'TitleBGB' => 0, 'TitleFGR' => 0, 'TitleFGG' => 0, 'TitleFGB' => 0, 'ImgR' => 240, 'ImgG' => 240, 'ImgB' => 240, 'BorderR' => 0, 'BorderG' => 0, 'BorderB' => 0, 'LegendR' => 250, 'LegendG' => 250, 'LegendB' => 250, 'LabelBGR' => 0, 'LabelBGG' => 0, 'LabelBGB' => 0, 'LabelFGR' => 0, 'LabelFGG' => 0, 'LabelFGB' => 0); // Get the custom settings if (is_array($config)) { foreach ($config as $key => $val) { $settings[$key] = $val; } } if ($settings['SpliceHeight'] == 0) { $settings['SpliceHeight'] = 1; } // avoid division by zero in pChart $PieFontSize = $settings['PieFontSize']; $TitleFontSize = $settings['TitleFontSize']; $LegendFontSize = $settings['LegendFontSize']; $LabelFontSize = $settings['LabelFontSize']; $PieFontName = $this->_ext_path . "/fonts/" . $settings['PieFontName']; $TitleFontName = $this->_ext_path . "/fonts/" . $settings['TitleFontName']; $LegendFontName = $this->_ext_path . "/fonts/" . $settings['LegendFontName']; $LabelFontName = $this->_ext_path . "/fonts/" . $settings['LabelFontName']; $Skew = $settings['Skew']; // Calc legend size $Wmax = 0; $TextHeight = 0; foreach ($legend as $lg) { $Position = imageftbbox($LegendFontSize, 0, $LegendFontName, $lg); $TextWidth = $Position[2] - $Position[0]; $TextHeight = $TextHeight + $Position[1] - $Position[7]; $Wmax = $TextWidth > $Wmax ? $TextWidth : $Wmax; // Maximum width of the legend } $legendW = $Wmax + 30; if (strlen($title) > 0) { $Position = imageftbbox($TitleFontSize, 0, $TitleFontName, $title); $titleWidth = $Position[2] - $Position[0] + 40; $titleHeight = $Position[1] - $Position[7] + 8; } else { $titleWidth = 0; $titleHeight = 0; } if (strlen($bottom_label) > 0) { $Position = imageftbbox($LabelFontSize, 0, $LabelFontName, $bottom_label); $labelWidth = $Position[2] - $Position[0] + 8; $labelHeight = $Position[1] - $Position[7] + 8; } else { $labelWidth = 0; $labelHeight = 0; } // Calculate pie size based on fontsize radius and skew $Position = imageftbbox($PieFontSize, 0, $PieFontName, '199%'); $pieWidth = ($Position[2] - $Position[0]) * 2 + $r * 2; $sin = $Skew != 90 ? abs(sin(deg2rad($Skew))) : 1; // if skewed the height goes with sin($skew) $SpliceHeight = $Skew != 90 ? $settings['SpliceHeight'] : 0; // if skewed add the SpliceHeight $SpliceDistance = $settings['SpliceDistance'] * 2 + 30; $pieHeight = intval(($Position[1] - $Position[7]) * 2 + $r * 2 * $sin + $SpliceHeight); $h = intval($pieHeight + $SpliceDistance * $sin); // Img Height $y = intval(($pieHeight - $SpliceHeight) / 2 + $SpliceDistance / 2); // center y pos $x = intval($pieWidth / 2) + $SpliceDistance / 2; // center x pos $w = $x * 2 + ($legendW > $labelWidth ? $legendW : $labelWidth) + 10; if ($titleWidth > $w) { $w = $titleWidth; } $h2 = $labelHeight + $titleHeight + $TextHeight + 60; // If the legend has big height if ($h2 > $h) { $h = $h2; } //Add pixels for the title $h = $h + $titleHeight + 10; // Real height $y = $y + $titleHeight + 10; // Real center y // Dataset definition $DataSet = new pData(); $DataSet->AddPoint($percents, "Serie1"); $DataSet->AddPoint($legend, "Serie2"); $DataSet->AddAllSeries(); $DataSet->SetAbsciseLabelSerie("Serie2"); // Initialise the graph $Test = new pChart($w, $h); $red = $settings['ImgR']; $g = $settings['ImgG']; $b = $settings['ImgB']; $Test->drawFilledRoundedRectangle(7, 7, $w - 7, $h - 7, 5, $red, $g, $b); $red = $settings['BorderR']; $g = $settings['BorderG']; $b = $settings['BorderB']; $Test->drawRoundedRectangle(5, 5, $w - 5, $h - 5, 5, $red, $g, $b); // Draw the pie chart $Test->setFontProperties($PieFontName, $PieFontSize); if ($Skew != 90) { $Test->drawPieGraph($DataSet->GetData(), $DataSet->GetDataDescription(), $x, $y, $r, PIE_PERCENTAGE, TRUE, $Skew, $settings['SpliceHeight'], $settings['SpliceDistance']); } else { if ($settings['SpliceDistance'] == 0) { $Test->drawFilledCircle($x + 2, $y + 2, $r, 0, 0, 0); // This will draw a shadow under the pie chart $Test->drawBasicPieGraph($DataSet->GetData(), $DataSet->GetDataDescription(), $x, $y, $r, PIE_PERCENTAGE, 255, 255, 218); } else { $Test->setShadowProperties(2, 2, 200, 200, 200); $Test->drawFlatPieGraphWithShadow($DataSet->GetData(), $DataSet->GetDataDescription(), $x, $y, $r, PIE_PERCENTAGE, $settings['SpliceDistance']); } } // Draw the legend $x1 = $x * 2 - 5; $r = $settings['LegendR']; $g = $settings['LegendG']; $b = $settings['LegendB']; $Test->setFontProperties($LegendFontName, $LegendFontSize); $Test->drawPieLegend($x1, $titleHeight + 30, $DataSet->GetData(), $DataSet->GetDataDescription(), $r, $g, $b); //Draw the title if (strlen($title) > 0) { $Test->setFontProperties($TitleFontName, $TitleFontSize); $r = $settings['TitleFGR']; $g = $settings['TitleFGG']; $b = $settings['TitleFGB']; $r1 = $settings['TitleBGR']; $g1 = $settings['TitleBGG']; $b1 = $settings['TitleBGB']; $Test->drawTextBox(6, 6, $w - 5, $titleHeight + 10, $title, 0, $r, $g, $b, ALIGN_CENTER, FALSE, $r1, $g1, $b1, 50); } if (strlen($bottom_label) > 0) { $Test->setFontProperties($LabelFontName, $LabelFontSize); $r = $settings['LabelFGR']; $g = $settings['LabelFGG']; $b = $settings['LabelFGB']; $r1 = $settings['LabelBGR']; $g1 = $settings['LabelBGG']; $b1 = $settings['LabelBGB']; $x1 = $x * 2 - 10; $Test->drawTextBox($x1, $h - $labelHeight - 15, $w - 10, $h - 10, $bottom_label, 0, $r, $g, $b, ALIGN_CENTER, FALSE, $r1, $g1, $b1, 20); } if (strlen($imgname)) { $imgname = $this->_img_path . "/" . $imgname; } else { $this->obj->load->helper('string'); $imgname = $this->_img_path . "/pie-" . random_string('alnum', 16) . ".png"; } $Test->Render($imgname); return array("name" => '/' . $imgname, "w" => $w, "h" => $h); }