PHPlot Tips †
Change Pie-Chart start angle and drawing direction. †Two public variables are added.
# phplot-5.3.0 pie-chart modification # phplot530-MD01_01 2010/12/06 merlin<merlin@tec-tech.org> --- phplot.php.orig 2010-12-06 16:59:48.000000000 +0900 +++ phplot.php 2010-12-06 17:11:19.000000000 +0900 @@ -82,6 +82,9 @@ public $bar_extra_space = 0.5; // Number of extra bar's worth of space in a group public $bar_width_adjust = 1; // 1 = bars of normal width, must be > 0 + public $pie_startpoint = 0; // Start Angle of Pie Chart 0:Top Clockwise angle + public $pie_direction = 1; // Pie drawing direction 0: Original(CounterClockwise) 1: Clockwise + // Titles public $title_txt = ''; @@ -5139,11 +5142,21 @@ $end_angle += $val; // This method of conversion to integer - truncate after reversing it - was // chosen to match the implicit method of PHPlot<=5.0.4 to get the same slices. - $arc_start_angle = (int)(360 - $start_angle); - $arc_end_angle = (int)(360 - $end_angle); + + if ($this->pie_direction==0){ + $arc_start_angle = (int)($this->pie_startpoint-90-$start_angle); + $arc_end_angle = (int)($this->pie_startpoint-90-$end_angle); + }else{ + $arc_start_angle = (int)($this->pie_startpoint-90+$end_angle); + $arc_end_angle = (int)($this->pie_startpoint-90+$start_angle); + } if ($arc_start_angle > $arc_end_angle) { - $mid_angle = deg2rad($end_angle - ($val / 2)); + if ($this->pie_direction==0){ + $mid_angle = deg2rad(90-$this->pie_startpoint+($end_angle - ($val / 2))); + }else{ + $mid_angle = deg2rad(90-$this->pie_startpoint-($end_angle - ($val / 2))); + } // Draw the slice ImageFilledArc($this->img, $xpos, $ypos+$h, $diameter, $diam2, reference †Automatic color setting of pie-chart label †A public variable is added.
--- phplot.php.orig 2010-12-06 16:59:48.000000000 +0900 +++ phplot.php 2010-12-06 17:12:06.000000000 +0900 @@ -120,6 +120,7 @@ // to set it. Use the 'suffix' argument to Set[XY]LabelType instead. public $data_units_text = ''; // Units text for 'data' labels (i.e: '、', '$', etc.) + public $pie_label_color_mode =2; // Label color mode 0:default 1:Complementary color mode 2:Automatic Black/White mode // Legend public $legend = ''; // An array with legend titles // These variables are unset to take default values: @@ -5163,7 +5164,28 @@ $label_x = $xpos + ($diameter * 1.2 * cos($mid_angle)) * $this->label_scale_position; $label_y = $ypos+$h - ($diam2 * 1.2 * sin($mid_angle)) * $this->label_scale_position; - $this->DrawText($this->fonts['generic'], 0, $label_x, $label_y, $this->ndx_grid_color, + // Set label colors of the pie chart + switch($this->pie_label_color_mode){ + case 1: // Complementary color mode + $color_pie = imagecolorsforindex($this->img, $slicecol); + $color_label_pie=imagecolorallocate($this->img, abs($color_pie[red]-255), + abs($color_pie[green]-255), + abs($color_pie[blue]-255)); + break; + case 2: // Automatic black/white mode + $color_pie = imagecolorsforindex($this->img, $slicecol); + if ($color_pie[red]*299+$color_pie[green]*587+$color_pie[blue]*144>128000) + $color_label_pie=imagecolorallocate($this->img,0,0,0); + else + $color_label_pie=imagecolorallocate($this->img,255,255,255); + break; + default: // Default color mode + $color_label_pie = $this->ndx_grid_color; + break; + } + if ($this->label_scale_position >= 0.5) $color_label_pie = $this->ndx_grid_color; + + $this->DrawText($this->fonts['generic'], 0, $label_x, $label_y, $color_label_pie, $label_txt, 'center', 'center'); } } Example †<?php # PHPlot Example: Pie/text-data require_once 'phplot.php'; $data = array( array('', 100, 100, 200, 100,100,100,100), array('', 150, 100, 150, 100,10,100,100), ); $plot = new PHPlot(800,600); $plot->SetImageBorderType('raised'); $plot->SetDataType('text-data'); $plot->SetDataValues($data); $plot->SetPlotType('pie'); $plot->pie_startpoint=0; $plot->pie_label_color_mode=0; $plot->SetShading(100); $plot->SetUseTTF('True'); $plot->SetFont('generic','benjamingothic.ttf',20); $plot->SetLabelScalePosition(0.25); $plot->DrawGraph(); ?> mode 1 : Complementary color mode mode 2 : Automatic black/white mode reference †
Dynamic Examples (5.5.0) †patches † |