jpgraph_bar.php

来自「eGroupWare is a multi-user, web-based gr」· PHP 代码 · 共 743 行 · 第 1/2 页

PHP
743
字号
	if( $this->abswidth > -1 )
	    $abswidth=$this->abswidth;
	else
	    $abswidth=round($this->width*$xscale->scale_factor,0);
			
		
	for($i=0; $i<$numbars; $i++) {
	    if( $exist_x ) $x=$this->coords[1][$i];
	    else $x=$i;
			
	    $x=$xscale->Translate($x);
			
	    if($this->align=="center")
		$x -= $abswidth/2;
	    elseif($this->align=="right")
		$x -= $abswidth;			
	    $pts=array(
		$x,$zp,
		$x,$yscale->Translate($this->coords[0][$i]),
		$x+$abswidth,$yscale->Translate($this->coords[0][$i]),
		$x+$abswidth,$zp);
	    if( $this->grad ) {
		$grad = new Gradient($img);
		$grad->FilledRectangle($pts[2],$pts[3],
		$pts[6],$pts[7],
		$this->grad_fromcolor,$this->grad_tocolor,$this->grad_style);				
	    }
	    elseif( !empty($this->fill_color) ) {
		if(is_array($this->fill_color)) {
		    $img->PushColor($this->fill_color[$i % count($this->fill_color)]);
		} else {
		    $img->PushColor($this->fill_color);
		}
		$img->FilledPolygon($pts);
		$img->PopColor();
	    }
			
	    // Remember value of this bar
	    $val=$this->coords[0][$i];
			
	    if( $this->bar_shadow && $val != 0 ) {
		$ssh = $this->bar_shadow_hsize;
		$ssv = $this->bar_shadow_vsize;
				// Create points to create a "upper-right" shadow
		if( $val > 0 ) {
		    $sp[0]=$pts[6];		$sp[1]=$pts[7];
		    $sp[2]=$pts[4];		$sp[3]=$pts[5];
		    $sp[4]=$pts[2];		$sp[5]=$pts[3];
		    $sp[6]=$pts[2]+$ssh;	$sp[7]=$pts[3]-$ssv;
		    $sp[8]=$pts[4]+$ssh;	$sp[9]=$pts[5]-$ssv;
		    $sp[10]=$pts[6]+$ssh;	$sp[11]=$pts[7]-$ssv;
		} 
		elseif( $val < 0 ) {
		    $sp[0]=$pts[4];		$sp[1]=$pts[5];
		    $sp[2]=$pts[6];		$sp[3]=$pts[7];
		    $sp[4]=$pts[0];	$sp[5]=$pts[1];
		    $sp[6]=$pts[0]+$ssh;	$sp[7]=$pts[1]-$ssv;
		    $sp[8]=$pts[6]+$ssh;	$sp[9]=$pts[7]-$ssv;
		    $sp[10]=$pts[4]+$ssh;	$sp[11]=$pts[5]-$ssv;
		}
				
		$img->PushColor($this->bar_shadow_color);
		$img->FilledPolygon($sp);
		$img->PopColor();
	    }
			
	    // Stroke the outline of the bar
	    if( is_array($this->color) )
		$img->SetColor($this->color[$i % count($this->color)]);
	    else
		$img->SetColor($this->color);
	    $img->SetLineWeight($this->weight);
	    $img->Polygon($pts);
					
	    if( $this->show_value) {
		$sval=sprintf($this->show_value_format,$val);
		$txt = new Text($sval);
		$txt->SetFont($this->show_value_ff,$this->show_value_fs,$this->show_value_fsize);
		$txt->SetColor($this->show_value_color);
		$x=$pts[2]+($pts[4]-$pts[2])/2;
		if($this->bar_shadow)
		    $x += $ssh;
				
		if( $val >= 0 ) {
		    $txt->Pos($x,$pts[3]-$this->show_value_margin);
		    $txt->Align("center","bottom");
		}
		else {
		    $txt->Pos($x,$pts[3]+$this->show_value_margin);
		    $txt->Align("center","top");
		}
		$txt->SetOrientation($this->show_value_angle);
		$txt->Stroke($img);			
	    }
			
	    // Create the client side image map
	    $this->csimareas.= "<area shape=\"rect\" coords=\"";
	    // Hmmm, this is fishy.  Fixes a bug in Opera whereby if Y2<Y1 or X2<X1 the csim doesn't work
	    if ($pts[3] < $pts[7]) {
		if ($pts[2] < $pts[6]) 
		    $this->csimareas .= "$pts[2], $pts[3], $pts[6], $pts[7]\"";
		else 
		    $this->csimareas .= "$pts[6], $pts[3], $pts[2], $pts[7]\"";
	    } else {
		if ($pts[2] < $pts[6]) 
		    $this->csimareas .= "$pts[2], $pts[7], $pts[6], $pts[3]\"";
		else 
		    $this->csimareas .= "$pts[6], $pts[7], $pts[2], $pts[3]\"";
	    }
	    if( !empty($this->csimtargets[$i]) )
		$this->csimareas .= " href=\"".$this->csimtargets[$i]."\"";
	    if( !empty($this->csimalts[$i]) ) {
		$sval=sprintf($this->csimalts[$i],$this->coords[0][$i]);
		$this->csimareas .= " alt=\"$sval\" title=\"$sval\" ";
	    }
	    $this->csimareas .= ">\r\n";
	}
	return true;
    }
} // Class

//===================================================
// CLASS GroupBarPlot
// Description: Produce grouped bar plots
//===================================================
class GroupBarPlot extends BarPlot {
    var $plots;
    var $width=0.7;
    var $nbrplots=0;
    var $numpoints;
//---------------
// CONSTRUCTOR
    function GroupBarPlot($plots) {
	$this->plots = $plots;
	$this->nbrplots = count($plots);
	$this->numpoints = $plots[0]->numpoints;
    }

//---------------
// PUBLIC METHODS	
    function Legend(&$graph) {
	$n = count($this->plots);
	for($i=0; $i<$n; ++$i)
	    $this->plots[$i]->Legend($graph);
    }
	
    function Min() {
	list($xmin,$ymin) = $this->plots[0]->Min();
	$n = count($this->plots);
	for($i=0; $i<$n; ++$i) {
	    list($xm,$ym) = $this->plots[$i]->Min();
	    $xmin = max($xmin,$xm);
	    $ymin = min($ymin,$ym);
	}
	return array($xmin,$ymin);		
    }
	
    function Max() {
	list($xmax,$ymax) = $this->plots[0]->Max();
	$n = count($this->plots);
	for($i=0; $i<$n; ++$i) {
	    list($xm,$ym) = $this->plots[$i]->Max();
	    $xmax = max($xmax,$xm);
	    $ymax = max($ymax,$ym);
	}
	return array($xmax,$ymax);
    }
	
    function GetCSIMareas() {
	$n = count($this->plots);
	for($i=0; $i<$n; ++$i) {
	    $csimareas.= $this->plots[$i]->csimareas;
	}
	return $csimareas;
    }
	
    // Stroke all the bars next to each other
    function Stroke(&$img,&$xscale,&$yscale) { 
	$tmp=$xscale->off;
	$n = count($this->plots);
	for( $i=0; $i<$n; ++$i ) {
	    $this->plots[$i]->ymin=$this->ybase;
	    $this->plots[$i]->SetWidth($this->width/$this->nbrplots);
	    $xscale->off = $tmp+$i*round($xscale->ticks->major_step*$xscale->scale_factor*$this->width/$this->nbrplots);
	    $this->plots[$i]->Stroke($img,$xscale,$yscale);
	}
	$xscale->off=$tmp;
    }
} // Class

//===================================================
// CLASS AccBarPlot
// Description: Produce accumulated bar plots
//===================================================
class AccBarPlot extends BarPlot {
    var $plots=null,$nbrplots=0,$numpoints=0;
//---------------
// CONSTRUCTOR
    function AccBarPlot($plots) {
	$this->plots = $plots;
	$this->nbrplots = count($plots);
	$this->numpoints = $plots[0]->numpoints;		
    }

//---------------
// PUBLIC METHODS	
    function Legend(&$graph) {
	$n = count($this->plots);
	for( $i=0; $i<$n; ++$i ) 
	    $this->plots[$i]->Legend($graph);
    }

    function Max() {
	list($xmax) = $this->plots[0]->Max();
	$nmax=0;
	for($i=0; $i<count($this->plots); ++$i) {
	    $n = count($this->plots[$i]->coords[0]);
	    $nmax = max($nmax,$n);
	    list($x) = $this->plots[$i]->Max();
	    $xmax = Max($xmax,$x);
	}
	for( $i = 0; $i < $nmax; $i++ ) {
	    // Get y-value for bar $i by adding the
	    // individual bars from all the plots added.
	    // It would be wrong to just add the
	    // individual plots max y-value since that
	    // would in most cases give to large y-value.
	    $y=$this->plots[0]->coords[0][$i];
	    for( $j = 1; $j < $this->nbrplots; $j++ ) {
		$y += $this->plots[ $j ]->coords[0][$i];
	    }
	    $ymax[$i] = $y;
	}
	$ymax = max($ymax);

	// Bar always start at baseline
	if( $ymax <= $this->ybase ) 
	    $ymax = $this->ybase;
	return array($xmax,$ymax);
    }

    function Min() {
	$nmax=0;
	list($xmin,$ysetmin) = $this->plots[0]->Min();
	for($i=0; $i<count($this->plots); ++$i) {
	    $n = count($this->plots[$i]->coords[0]);
	    $nmax = max($nmax,$n);
	    list($x,$y) = $this->plots[$i]->Min();
	    $xmin = Min($xmin,$x);
	    $ysetmin = Min($y,$ysetmin);
	}
	for( $i = 0; $i < $nmax; $i++ ) {
	    // Get y-value for bar $i by adding the
	    // individual bars from all the plots added.
	    // It would be wrong to just add the
	    // individual plots max y-value since that
	    // would in most cases give to large y-value.
	    $y=$this->plots[0]->coords[0][$i];
	    for( $j = 1; $j < $this->nbrplots; $j++ ) {
		$y += $this->plots[ $j ]->coords[0][$i];
	    }
	    $ymin[$i] = $y;
	}
	$ymin = Min($ysetmin,Min($ymin));
	// Bar always start at baseline
	if( $ymin >= $this->ybase )
	    $ymin = $this->ybase;
	return array($xmin,$ymin);
    }

    // Method description
    function Stroke(&$img,&$xscale,&$yscale) {
	$img->SetLineWeight($this->weight);
	for($i=0; $i<$this->numpoints-1; $i++) {
	    $accy = 0;
	    $accy_neg = 0; 
	    for($j=0; $j<$this->nbrplots; ++$j ) {				
		$img->SetColor($this->plots[$j]->color);
		if ($this->plots[$j]->coords[0][$i] > 0) {
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy);
		    $accyt=$yscale->Translate($accy);
		    $accy+=$this->plots[$j]->coords[0][$i];
		} else {
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy_neg);
		    $accyt=$yscale->Translate($accy_neg);
		    $accy_neg+=$this->plots[$j]->coords[0][$i];
		}				
				
		$xt=$xscale->Translate($i);
		$abswidth=round($this->width*$xscale->scale_factor,0);
		$pts=array($xt,$accyt,$xt,$yt,$xt+$abswidth,$yt,$xt+$abswidth,$accyt);
				
		if( $this->plots[$j]->grad ) {
		    $grad = new Gradient($img);
		    $grad->FilledRectangle(
			$pts[2],$pts[3],
			$pts[6],$pts[7],
			$this->plots[$j]->grad_fromcolor,
			$this->plots[$j]->grad_tocolor,
			$this->plots[$j]->grad_style);				
		} elseif ($this->plots[$j]->fill_color ) {
		    $img->SetColor($this->plots[$j]->fill_color);
		    $img->FilledPolygon($pts,4);
		    $img->SetColor($this->plots[$j]->color);
		}				  

		if( $this->bar_shadow ) {
		    $ssh = $this->bar_shadow_hsize;
		    $ssv = $this->bar_shadow_vsize;
		    // Create points to create a "upper-right" shadow
		    $sp[0]=$pts[6];		$sp[1]=$pts[7];
		    $sp[2]=$pts[4];		$sp[3]=$pts[5];
		    $sp[4]=$pts[2];	$sp[5]=$pts[3];
		    $sp[6]=$pts[2]+$ssh;	$sp[7]=$pts[3]-$ssv;
		    $sp[8]=$pts[4]+$ssh;	$sp[9]=$pts[5]-$ssv;
		    $sp[10]=$pts[6]+$ssh;	$sp[11]=$pts[7]-$ssv;
		    $img->SetColor($this->bar_shadow_color);
		    $img->FilledPolygon($sp,4);
		}

		if( $i < count($this->plots[$j]->csimtargets) ) {
		    $this->csimareas.= "<area shape=\"rect\" coords=\"";
		    // Hmmm, this is fishy.  Fixes a bug in Opera whereby if Y2<Y1 or X2<X1 the csim doesn't work
		    // This means that the area MUST specify top left and bottom right corners
		    if ($pts[3] < $pts[7]) {
			if ($pts[2] < $pts[6]) {
			    $this->csimareas.= "$pts[2], $pts[3], $pts[6], $pts[7]\"";
			} else {
			    $this->csimareas.= "$pts[6], $pts[3], $pts[2], $pts[7]\"";
			}
		    } else {
			if ($pts[2] < $pts[6]) {
			    $this->csimareas.= "$pts[2], $pts[7], $pts[6], $pts[3]\"";
			} else {
			    $this->csimareas.= "$pts[6], $pts[7], $pts[2], $pts[3]\"";
			}
		    }
		    $this->csimareas.= " href=\"".$this->plots[$j]->csimtargets[$i]."\"";
		    if( !empty($this->plots[$j]->csimalts[$i]) ) {
			$sval=sprintf($this->plots[$j]->csimalts[$i],$this->plots[$j]->coords[0][$i]);										
			$this->csimareas .= " alt=\"$sval\" title=\"$sval\" ";
		    }
		    $this->csimareas .= ">\r\n";				
		}
				
		$img->Polygon($pts,4);
	    }
			
	    $yt=$yscale->Translate($accy);
			
	    if( $this->show_value) {
		$sval=sprintf($this->show_value_format,$accy);
		$txt = new Text($sval);
		$txt->SetFont($this->show_value_ff,$this->show_value_fs,$this->show_value_fsize);
		$txt->SetColor($this->show_value_color);
		$x=$pts[2]+($pts[4]-$pts[2])/2;
		if($this->bar_shadow)
		    $x += $ssh;
		$txt->Pos($x,$yt-$this->show_value_margin);
		$txt->Align("center","bottom");
		$txt->SetOrientation($this->show_value_angle);
		$txt->Stroke($img);			
	    }			
	}
	return true;
    }
} // Class

/* EOF */
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?