jpgraph_gantt.php

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

PHP
1,445
字号
    function SetColor($aColor) {
	$this->iColor = $aColor;
    }
	
    function HasTabs() {
	return substr_count($this->iText,"\t") > 0;
    }
	
    // Get number of tabs in string
    function GetNbrTabs() {
	substr_count($this->iText,"\t");
    }
	
    // Set alignment
    function Align($aHAlign,$aVAlign="bottom") {
	$this->iHAlign=$aHAlign;
	$this->iVAlign=$aVAlign;
    }
	
    // Specify font
    function SetFont($aFFamily,$aFStyle=FS_NORMAL,$aFSize=10) {
	$this->iFFamily = $aFFamily;
	$this->iFStyle	 = $aFStyle;
	$this->iFSize	 = $aFSize;
    }
	
    // Get width of text. If text contains several columns separated by
    // tabs then return both the total width as well as an array with a 
    // width for each column.
    function GetWidth($aImg,$aUseTabs=false,$aTabExtraMargin=1.1) {
	if( strlen($this->iText)== 0 ) return;
	$tmp = split("\t",$this->iText);
	$aImg->SetFont($this->iFFamily,$this->iFStyle,$this->iFSize);
	if( count($tmp) <= 1 || !$aUseTabs ) {
	    return $aImg->GetTextWidth($this->iText);
	}
	else {
	    $tot=0;
	    for($i=0; $i<count($tmp); ++$i) {
		$res[$i] = $aImg->GetTextWidth($tmp[$i]);
		$tot += $res[$i]*$aTabExtraMargin;
	    }
	    return array($tot,$res);
	}
    }
	
    // Get total height of text
    function GetHeight($aImg) {
	$aImg->SetFont($this->iFFamily,$this->iFStyle,$this->iFSize);
	return $aImg->GetFontHeight();
    }
	
    // Unhide/hide the text	
    function Show($aShow) {
	$this->iShow=$aShow;
    }
	
    // Stroke text at (x,y) coordinates. If the text contains tabs then the
    // x parameter should be an array of positions to be used for each successive
    // tab mark. If no array is supplied then the tabs will be ignored.
    function Stroke($aImg,$aX,$aY) {
	if( $this->iShow ) {
	    $aImg->SetColor($this->iColor);
	    $aImg->SetFont($this->iFFamily,$this->iFStyle,$this->iFSize);
	    $aImg->SetTextAlign($this->iHAlign,$this->iVAlign);			
	    if( $this->GetNbrTabs() <= 1 || !is_array($aX) ) {
				// Get rid of any "\t" characters and stroke string
		$aImg->StrokeText($aX,$aY,str_replace("\t"," ",$this->iText));
	    }
	    else {
		$tmp = split("\t",$this->iText);
		$n = min(count($tmp),count($aX));
		for($i=0; $i<$n; ++$i) {
		    $aImg->StrokeText($aX[$i],$aY,$tmp[$i]);
		}	
	    }
	}
    }
}

//===================================================
// CLASS HeaderProperty
// Description: Data encapsulating class to hold property 
// for each type of the scale headers
//===================================================
class HeaderProperty {
    var $iTitleVertMargin=3,$iFFamily=FF_FONT0,$iFStyle=FS_NORMAL,$iFSize=8;
    var $iFrameColor="black",$iFrameWeight=1;
    var $iShowLabels=true,$iShowGrid=true;
    var $iBackgroundColor="white";
    var $iWeekendBackgroundColor="lightgray",$iSundayTextColor="red"; // these are only used with day scale
    var $iTextColor="black";
    var $iLabelFormStr="%d";
    var $grid,$iStyle=0;

//---------------
// CONSTRUCTOR	
    function HeaderProperty() {
	$this->grid = new LineProperty();
    }

//---------------
// PUBLIC METHODS		
    function Show($aShow) {
	$this->iShowLabels = $aShow;
    }
	
    function SetFont($aFFamily,$aFStyle=FS_NORMAL,$aFSize=10) {
	$this->iFFamily = $aFFamily;
	$this->iFStyle	 = $aFStyle;
	$this->iFSize	 = $aFSize;
    }

    function SetFontColor($aColor) {
	$this->iTextColor = $aColor;
    }
	
    function GetFontHeight($aImg) {
	$aImg->SetFont($this->iFFamily,$this->iFStyle,$this->iFSize);
	return $aImg->GetFontHeight();
    }

    function GetFontWidth($aImg) {
	$aImg->SetFont($this->iFFamily,$this->iFStyle,$this->iFSize);
	return $aImg->GetFontWidth();
    }
	
    function SetStyle($aStyle) {
	$this->iStyle = $aStyle;
    }
	
    function SetBackgroundColor($aColor) {
	$this->iBackgroundColor=$aColor;
    }

    function SetFrameWeight($aWeight) {
	$this->iFrameWeight=$aWeight;
    }

    function SetFrameColor($aColor) {
	$this->iFrameColor=$aColor;
    }
	
    // Only used by day scale
    function SetWeekendColor($aColor) {
	$this->iWeekendBackgroundColor=$aColor;
    }
	
    // Only used by day scale
    function SetSundayFontColor($aColor) {
	$this->iSundayTextColor=$aColor;
    }
	
    function SetTitleVertMargin($aMargin) {
	$this->iTitleVertMargin=$aMargin;
    }
	
    function SetLabelFormatString($aStr) {
	$this->iLabelFormStr=$aStr;
    }
}

//===================================================
// CLASS GanttScale
// Description: Responsible for calculating and showing
// the scale in a gantt chart. This includes providing methods for
// converting dates to position in the chart as well as stroking the
// date headers (days, week, etc).
//===================================================
class GanttScale {
    var $day,$week,$month,$year;
    var $divider,$dividerh,$tableTitle;
    var $iStartDate=-1,$iEndDate=-1;	
    // Number of gantt bar position (n.b not necessariliy the same as the number of bars)
    // we could have on bar in position 1, and one bar in position 5 then there are two
    // bars but the number of bar positions is 5
    var $iVertLines=-1;	
    // The width of the labels (defaults to the widest of all labels)
    var $iLabelWidth;	
    // Out image to stroke the scale to
    var $iImg;	
    var $iTableHeaderBackgroundColor="white",$iTableHeaderFrameColor="black";
    var $iTableHeaderFrameWeight=1;
    var $iAvailableHeight=-1,$iVertSpacing=-1,$iVertHeaderSize=-1;
    var $iDateLocale;
    var $iVertLayout=GANTT_EVEN;
    var $iTopPlotMargin=10,$iBottomPlotMargin=15;
    var $iUsePlotWeekendBackground=true;
	
//---------------
// CONSTRUCTOR	
    function GanttScale(&$aImg) {
	$this->iImg = &$aImg;		
	$this->iDateLocale = new DateLocale();
	$this->day = new HeaderProperty();
	$this->day->grid->SetColor("gray");

	$this->week = new HeaderProperty();
	$this->week->SetLabelFormatString("w%d");
	$this->week->SetFont(FF_FONT1);

	$this->month = new HeaderProperty();
	$this->month->SetFont(FF_FONT1,FS_BOLD);

	$this->year = new HeaderProperty();
	$this->year->SetFont(FF_FONT1,FS_BOLD);		
		
	$this->divider=new LineProperty();
	$this->dividerh=new LineProperty();		
	$this->tableTitle=new TextProperty();
    }
	
//---------------
// PUBLIC METHODS	
    // Specify what headers should be visible
    function ShowHeaders($aFlg) {
	$this->day->Show($aFlg & GANTT_HDAY);
	$this->week->Show($aFlg & GANTT_HWEEK);
	$this->month->Show($aFlg & GANTT_HMONTH);
	$this->year->Show($aFlg & GANTT_HYEAR);

	// Make some default settings of gridlines whihc makes sense
	if( $aFlg & GANTT_HWEEK ) {
	    $this->month->grid->Show(false);
	    $this->year->grid->Show(false);
	}
    }
	
    // Should the weekend background stretch all the way down in the plotarea
    function UseWeekendBackground($aShow) {
	$this->iUsePlotWeekendBackground = $aShow;
    }
	
    // Have a range been specified?
    function IsRangeSet() {
	return $this->iStartDate!=-1 && $this->iEndDate!=-1;
    }
	
    // Should the layout be from top or even?
    function SetVertLayout($aLayout) {
	$this->iVertLayout = $aLayout;
    }
	
    // Which locale should be used?
    function SetDateLocale($aLocale) {
	$this->iDateLocale->Set($aLocale);
    }
	
    // Number of days we are showing
    function GetNumberOfDays() {
	return round(($this->iEndDate-$this->iStartDate)/SECPERDAY)+1;
    }
	
    // The widthj of the actual plot area
    function GetPlotWidth() {
	$img=$this->iImg;
	return $img->width - $img->left_margin - $img->right_margin;
    }

    // Specify the width of the titles(labels) for the activities
    // (This is by default set to the minimum width enought for the
    // widest title)
    function SetLabelWidth($aLabelWidth) {
	$this->iLabelWidth=$aLabelWidth;
    }
	
    // Do we show day scale?
    function IsDisplayDay() {
	return $this->day->iShowLabels;
    }
	
    // Do we show week scale?
    function IsDisplayWeek() {
	return $this->week->iShowLabels;
    }
	
    // Do we show month scale?
    function IsDisplayMonth() {
	return $this->month->iShowLabels;
    }
	
    // Do we show year scale?
    function IsDisplayYear() {
	return $this->year->iShowLabels;
    }

    // Specify spacing (in percent of bar height) between activity bars
    function SetVertSpacing($aSpacing) {
	$this->iVertSpacing = $aSpacing;
    }

    // Specify scale min and max date either as timestamp or as date strings
    // Always round to the nearest week boundary
    function SetRange($aMin,$aMax) {
	$this->iStartDate = $this->NormalizeDate($aMin);
	$this->iEndDate = $this->NormalizeDate($aMax);
		
	// Get day in week Sun=0
	$ds=strftime("%w",$this->iStartDate);
	$de=strftime("%w",$this->iEndDate);
		
	if( $ds==0 ) $ds=7;
	if( $de==0 ) $de=7;
	
	// We want to start on Monday
	$this->iStartDate -= SECPERDAY*($ds-1);
		
	// We want to end on a Sunday
	$this->iEndDate += SECPERDAY*(7-$de);
    }

    // Specify background for the table title area (upper left corner of the table)	
    function SetTableTitleBackground($aColor) {
	$this->iTableHeaderBackgroundColor = $aColor;
    }

///////////////////////////////////////
// PRIVATE Methods
	
    // Determine the height of all the scale headers combined
    function GetHeaderHeight() {
	$img=$this->iImg;
	$height=1;
	if( $this->day->iShowLabels ) {
	    $height += $this->day->GetFontHeight($img);
	    $height += $this->day->iTitleVertMargin;
	}
	if( $this->week->iShowLabels ) {
	    $height += $this->week->GetFontHeight($img);
	    $height += $this->week->iTitleVertMargin;
	}
	if( $this->month->iShowLabels ) {
	    $height += $this->month->GetFontHeight($img);
	    $height += $this->month->iTitleVertMargin;
	}
	if( $this->year->iShowLabels ) {
	    $height += $this->year->GetFontHeight($img);
	    $height += $this->year->iTitleVertMargin;
	}
	return $height;
    }
	
    // Get width (in pisels) for a single day
    function GetDayWidth() {
	return ($this->GetPlotWidth()-$this->iLabelWidth+1)/$this->GetNumberOfDays();	
    }

    // Nuber of days in a year
    function GetNumDaysInYear($aYear) {
	if( $this->IsLeap($aYear) )
	    return 366;
	else
	    return 365;
    }
	
    // Get day number in year	
    function GetDayNbrInYear($aDate) {
	return 0+strftime("%j",$aDate);
    }
	
    // Get week number 
    function GetWeekNbr($aDate) {

⌨️ 快捷键说明

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