jpgraph.php
来自「eGroupWare is a multi-user, web-based gr」· PHP 代码 · 共 1,955 行 · 第 1/5 页
PHP
1,955 行
$this->x = $aXAbsPos;
$this->y = $aYAbsPos;
}
//---------------
// PUBLIC METHODS
// Set the string in the text object
function Set($aTxt) {
$this->t = $aTxt;
}
// Specify the position and alignment for the text object
function Pos($aXAbsPos=0,$aYAbsPos=0,$aHAlign="left",$aVAlign="top") {
$this->x = $aXAbsPos;
$this->y = $aYAbsPos;
$this->halign = $aHAlign;
$this->valign = $aVAlign;
}
// Specify alignment for the text
function Align($aHAlign,$aVAlign="top") {
$this->halign = $aHAlign;
$this->valign = $aVAlign;
}
// Specifies the alignment for a multi line text
function ParagraphAlign($aAlign) {
$this->paragraph_align = $aAlign;
}
// Specify that the text should be boxed. fcolor=frame color, bcolor=border color,
// $shadow=drop shadow should be added around the text.
function SetBox($aFrameColor=array(255,255,255),$aBorderColor=array(0,0,0),$aShadow=false) {
if( $aFrameColor==false )
$this->boxed=false;
else
$this->boxed=true;
$this->fcolor=$aFrameColor;
$this->bcolor=$aBorderColor;
$this->shadow=$aShadow;
}
// Hide the text
function Hide($aHide=true) {
$this->hide=$aHide;
}
// This looks ugly since it's not a very orthogonal design
// but I added this "inverse" of Hide() to harmonize
// with some classes which I designed more recently (especially)
// jpgraph_gantt
function Show($aShow=true) {
$this->hide=!$aShow;
}
// Specify font
function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
$this->font_family=$aFamily;
$this->font_style=$aStyle;
$this->font_size=$aSize;
}
// Center the text between $left and $right coordinates
function Center($aLeft,$aRight,$aYAbsPos=false) {
$this->x = $aLeft + ($aRight-$aLeft )/2;
$this->halign = "center";
if( is_numeric($aYAbsPos) )
$this->y = $aYAbsPos;
}
// Set text color
function SetColor($aColor) {
$this->color = $aColor;
}
// Orientation of text. Note only TTF fonts can have an arbitrary angle
function SetOrientation($aDirection=0) {
if( is_numeric($aDirection) )
$this->dir=$aDirection;
elseif( $aDirection=="h" )
$this->dir = 0;
elseif( $aDirection=="v" )
$this->dir = 90;
else JpGraphError::Raise("<b>JpGraph Error:</b> Invalid direction specified for text.");
}
// Total width of text
function GetWidth(&$aImg) {
$aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
return $aImg->GetTextWidth($this->t);
}
// Hight of font
function GetFontHeight(&$aImg) {
$aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
return $aImg->GetFontHeight();
}
function GetTextHeight(&$aImg) {
$aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
return $aImg->GetTextHeight($this->t);
}
// Display text in image
function Stroke(&$aImg,$x=-1,$y=-1) {
if( $x>-1 ) $this->x = $x;
if( $y>-1 ) $this->y = $y;
// If position been given as a fraction of the image size
// calculate the absolute position
if( $this->x < 1 ) $this->x *= $aImg->width;
if( $this->y < 1 ) $this->y *= $aImg->height;
$aImg->PushColor($this->color);
$aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
$aImg->SetTextAlign($this->halign,$this->valign);
if( $this->boxed ) {
if( $this->fcolor=="nofill" ) $this->fcolor=false;
$aImg->StrokeBoxedText($this->x,$this->y,$this->t,
$this->dir,$this->fcolor,$this->bcolor,$this->shadow,
$this->paragraph_align);
}
else {
$aImg->StrokeText($this->x,$this->y,$this->t,$this->dir,
$this->paragraph_align);
}
$aImg->PopColor($this->color);
}
} // Class
//===================================================
// CLASS Grid
// Description: responsible for drawing grid lines in graph
//===================================================
class Grid {
var $img;
var $scale;
var $grid_color=array(196,196,196);
var $type="solid";
var $show=false, $showMinor=false,$weight=1;
//---------------
// CONSTRUCTOR
function Grid(&$aAxis) {
$this->scale = &$aAxis->scale;
$this->img = &$aAxis->img;
}
//---------------
// PUBLIC METHODS
function SetColor($aColor) {
$this->grid_color=$aColor;
}
function SetWeight($aWeight) {
$this->weight=$aWeight;
}
// Specify if grid should be dashed, dotted or solid
function SetLineStyle($aType) {
$this->type = $aType;
}
// Decide if both major and minor grid should be displayed
function Show($aShowMajor=true,$aShowMinor=false) {
$this->show=$aShowMajor;
$this->showMinor=$aShowMinor;
}
// Display the grid
function Stroke() {
if( $this->showMinor )
$this->DoStroke($this->scale->ticks->ticks_pos);
else
$this->DoStroke($this->scale->ticks->maj_ticks_pos);
}
//--------------
// Private methods
// Draw the grid
function DoStroke(&$aTicksPos) {
if( !$this->show )
return;
$this->img->SetColor($this->grid_color);
$this->img->SetLineWeight($this->weight);
$nbrgrids = count($aTicksPos);
if( $this->scale->type=="y" ) {
$xl=$this->img->left_margin;
$xr=$this->img->width-$this->img->right_margin;
for($i=0; $i<$nbrgrids; ++$i) {
$y=$aTicksPos[$i];
if( $this->type == "solid" )
$this->img->Line($xl,$y,$xr,$y);
elseif( $this->type == "dotted" )
$this->img->DashedLine($xl,$y,$xr,$y,1,6);
elseif( $this->type == "dashed" )
$this->img->DashedLine($xl,$y,$xr,$y,2,4);
elseif( $this->type == "longdashed" )
$this->img->DashedLine($xl,$y,$xr,$y,8,6);
}
}
if( $this->scale->type=="x" ) {
$yu=$this->img->top_margin;
$yl=$this->img->height-$this->img->bottom_margin;
$x=$aTicksPos[0];
$limit=$this->img->width-$this->img->right_margin;
$i=0;
// We must also test for limit since we might have
// an offset and the number of ticks is calculated with
// assumption offset==0 so we might end up drawing one
// to many gridlines
while( $x<=$limit && $i<count($aTicksPos)) {
$x=$aTicksPos[$i];
if( $this->type == "solid" )
$this->img->Line($x,$yl,$x,$yu);
elseif( $this->type == "dotted" )
$this->img->DashedLine($x,$yl,$x,$yu,1,6);
elseif( $this->type == "dashed" )
$this->img->DashedLine($x,$yl,$x,$yu,2,4);
elseif( $this->type == "longdashed" )
$this->img->DashedLine($x,$yl,$x,$yu,8,6);
++$i;
}
}
return true;
}
} // Class
//===================================================
// CLASS Axis
// Description: Defines X and Y axis. Notes that at the
// moment the code is not really good since the axis on
// several occasion must know wheter it's an X or Y axis.
// This was a design decision to make the code easier to
// follow.
//===================================================
class Axis {
var $pos = false;
var $weight=1;
var $color=array(0,0,0),$label_color=array(0,0,0);
var $img=null,$scale=null;
var $hide=false;
var $ticks_label=false;
var $show_first_label=true;
var $label_step=1; // Used by a text axis to specify what multiple of major steps
// should be labeled.
var $tick_step=1;
var $labelPos=0; // Which side of the axis should the labels be?
var $title=null,$title_adjust,$title_margin,$title_side=SIDE_LEFT;
var $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12,$label_angle=0;
var $tick_label_margin=6;
//---------------
// CONSTRUCTOR
function Axis(&$img,&$aScale,$color=array(0,0,0)) {
$this->img = &$img;
$this->scale = &$aScale;
$this->color = $color;
$this->title=new Text("");
if( $aScale->type=="y" ) {
$this->title_margin = 25;
$this->title_adjust="middle";
$this->title->SetOrientation(90);
$this->tick_label_margin=6;
}
else {
$this->title_margin = 5;
$this->title_adjust="high";
$this->title->SetOrientation(0);
$this->tick_label_margin=3;
}
}
//---------------
// PUBLIC METHODS
// Utility function to set the direction for tick marks
function SetTickDirection($aDir) {
$this->scale->ticks->SetDirection($aDir);
}
function SetLabelFormatString($aFormStr) {
$this->scale->ticks->SetLabelFormat($aFormStr);
}
function SetLabelFormatCallback($aFuncName) {
$this->scale->ticks->SetFormatCallback($aFuncName);
}
// Don't display the first label
function HideFirstTickLabel($aHide=false) {
$this->show_first_label=$aHide;
}
// Hide the axis
function Hide($aHide=true) {
$this->hide=$aHide;
}
// Weight of axis
function SetWeight($aWeight) {
$this->weight = $aWeight;
}
// Axis color
function SetColor($aColor,$aLabelColor=false) {
$this->color = $aColor;
if( !$aLabelColor ) $this->label_color = $aColor;
else $this->label_color = $aLabelColor;
}
// Title on axis
function SetTitle($aTitle,$aAdjustAlign="high") {
$this->title->Set($aTitle);
$this->title_adjust=$aAdjustAlign;
}
// Specify distance from the axis
function SetTitleMargin($aMargin) {
$this->title_margin=$aMargin;
}
// Specify text labels for the ticks. One label for each data point
function SetTickLabels($aLabelArray) {
$this->ticks_label = $aLabelArray;
}
// How far from the axis should the labels be drawn
function SetTickLabelMargin($aMargin) {
$this->tick_label_margin=$aMargin;
}
// Specify that every $step of the ticks should be displayed starting
// at $start
// DEPRECATED FUNCTION: USE SetTextTickInterval() INSTEAD
function SetTextTicks($step,$start=0) {
JpGraphError::Raise("<b>JpGraph Error:</b> SetTextTicks() is deprecated. Use SetTextTickInterval() instead.");
}
// Specify that every $step of the ticks should be displayed starting
// at $start
function SetTextTickInterval($aStep,$aStart=0) {
$this->scale->ticks->SetTextLabelStart($aStart);
$this->tick_step=$aStep;
}
// Specify that every $step tick mark should have a label
// should be displayed starting
function SetTextLabelInterval($aStep) {
if( $aStep < 1 )
JpGraphError::Raise("<b>JpGraph Error:</b> Text label interval must be specified >= 1.");
$this->label_step=$aStep;
}
// Which side of the axis should the labels be on?
function SetLabelPos($aSidePos) {
$this->labelPos=$aSidePos;
}
// Set the font
function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
$this->font_family = $aFamily;
$this->font_style = $aStyle;
$this->font_size = $aSize;
}
// Which side of the axis should the axis title be?
function SetTitleSide($aSideOfAxis) {
$this->title_side = $aSideOfAxis;
}
// Stroke the axis.
function Stroke($aOtherAxisScale) {
if( $this->hide ) return;
if( is_numeric($this->pos) ) {
$pos=$aOtherAxisScale->Translate($this->pos);
}
else { // Default to minimum of other scale if pos not set
if( $aOtherAxisScale->GetMinVal() >= 0 || $this->pos=="min" ) {
$pos = $aOtherAxisScale->scale_abs[0];
}
else { // If negative set x-axis at 0
$this->pos=0;
$pos=$aOtherAxisScale->Translate(0);
}
}
$this->img->SetLineWeight($this->weight);
$this->img->SetColor($this->color);
$this->img->SetFont($this->font_family,$this->font_style,$this->font_size);
if( $this->scale->type == "x" ) {
$this->img->FilledRectangle($this->img->left_margin,$pos,
$this->img->width-$this->img->right_margin,$pos+$this->weight-1);
$y=$pos+$this->img->GetFontHeight()+$this->title_margin;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?