超强PHP GD BarClass
时间:2006-01-12 来源:wleige
//-------------------------------------
//构造函数
//-------------------------------------
function Bar()
{
$this->fileName = "3dbar.dat";
$this->fontSize = 5;
$this->imageBorderSpace = 5;
$this->barFaceW = 10;
$this->barSideW = 10;
$this->bgFaceColor = "#9999FF";
$this->bgSideColor = "#ACACFF";
$this->bgFloodColor = "#ECECFF";
$this->barFaceColor = "#FFCCCC";
$this->barSideColor = "#FF9999";
$this->barTopColor = "#FFE8E8";
$this->labelFontColor = "#330099";
$this->barFontColor = "#000000";
$this->imageColor = "#FFFFFF";
$this->borderColor = "#CCCCFF";
$this->barFontPos = "top";
$this->barFontWay = "down";
$this->angle = 45;
$this->lineCount = 10;
$this->lineH = 20;
$this->imageType = "png";
}
//-------------------------------------
//设定数据文件名
//-------------------------------------
function setFileName($name)
{
$this->fileName = $name;
}
//-------------------------------------
//设定字体大小
//-------------------------------------
function setFontSize($size)
{
$this->fontSize = $size;
}
//-------------------------------------
//设定图像边距
//-------------------------------------
function setImageBorderSpace($space)
{
$this->imageBorderSpace = $space;
}
//-------------------------------------
//设定柱条正面宽度
//-------------------------------------
function setBarFaceW($width)
{
$this->barFaceW = $width;
}
//-------------------------------------
//设定柱条侧面宽度
//-------------------------------------
function setBarSideW($width)
{
$this->barSideW = $width;
}
//-------------------------------------
//取得原始数据
//-------------------------------------
function setDataArray($xLabelArray, $dataArray, $xPointCount)
{
/*
$file = file($this->fileName);
$this->xPointCount = count($file);
for($i = 0; $i xPointCount; $i++)
{
$line = explode("||",$file[$i]);
$this->xLabelArray[$i] = $line[0];
$this->dataArray[$i] = (int)$line[1];
}
*/
//add by wsjakey 11-20
$this->xLabelArray = $xLabelArray;
$this->dataArray = $dataArray;
$this->xPointCount = $xPointCount;
//end by wsjakey 11-20
}
//-------------------------------------
//取得原始数据中最大数据
//-------------------------------------
function getMaxData()
{
$maxValue = (int)$this->dataArray[0];
for($i = 1; $i dataArray); $i++)
{
if($maxValue dataArray[$i])
{
$maxValue = (int)$this->dataArray[$i];
}
}
return $maxValue;
}
//-------------------------------------
//将16进制颜色值转换成RGB,返回imageColorAllocate值
//-------------------------------------
function getImageColorAllocate($colorStr)
{
$firstChar = subStr($colorStr,0,1);
if($firstChar == "#")
{
$startPos = 1;
}
else
{
$startPos = 0;
}
$R = HexDec(subStr($colorStr,$startPos,2));
$G = HexDec(subStr($colorStr,$startPos+2,2));
$B = HexDec(subStr($colorStr,$startPos+4,2));
return imageColorAllocate($this->image,$R,$G,$B);
}
//-------------------------------------
//设定正面背景颜色
//-------------------------------------
function setBgFaceColor($colorStr)
{
$this->bgFaceColor = $colorStr;
}
//-------------------------------------
//取得正面背景颜色
//-------------------------------------
function getBgFaceColor()
{
return $this->getImageColorAllocate($this->bgFaceColor);
}
//-------------------------------------
//设定侧面背景颜色
//-------------------------------------
function setBgSideColor($colorStr)
{
$this->bgSideColor = $colorStr;
}
//-------------------------------------
//取得侧面背景颜色
//-------------------------------------
function getBgSideColor()
{
return $this->getImageColorAllocate($this->bgSideColor);
}
//-------------------------------------
//设定底面背景颜色
//-------------------------------------
function setBgFloodColor($colorStr)
{
$this->bgFloodColor = $colorStr;
}
//-------------------------------------
//取得底面背景颜色
//-------------------------------------
function getBgFloodColor()
{
return $this->getImageColorAllocate($this->bgFloodColor);
}
//-------------------------------------
//设定柱条正面颜色
//-------------------------------------
function setBarFaceColor($colorStr)
{
$this->barFaceColor = $colorStr;
}
//-------------------------------------
//取得柱条正面颜色
//-------------------------------------
function getBarFaceColor()
{
return $this->getImageColorAllocate($this->barFaceColor);
}
//-------------------------------------
//设定柱条侧面颜色
//-------------------------------------
function setBarSideColor($colorStr)
{
$this->barSideColor = $colorStr;
}
//-------------------------------------
//取得柱条侧面颜色
//-------------------------------------
function getBarSideColor()
{
return $this->getImageColorAllocate($this->barSideColor);
}
//-------------------------------------
//设定柱条顶面颜色
//-------------------------------------
function setBarTopColor($colorStr)
{
$this->barTopColor = $colorStr;
}
//-------------------------------------
//取得柱条顶面颜色
//-------------------------------------
function getBarTopColor()
{
return $this->getImageColorAllocate($this->barTopColor);
}
//-------------------------------------
//设定X,Y轴标签字体颜色
//-------------------------------------
function setLabelFontColor($colorStr)
{
$this->labelFontColor = $colorStr;
}
//-------------------------------------
//取得X,Y轴标签字体颜色
//-------------------------------------
function getLabelFontColor()
{
return $this->getImageColorAllocate($this->labelFontColor);
}
//-------------------------------------
//设定柱条字体颜色
//-------------------------------------
function setBarFontColor($colorStr)
{
$this->barFontColor = $colorStr;
}
//-------------------------------------
//取得柱条字体颜色
//-------------------------------------
function getBarFontColor()
{
return $this->getImageColorAllocate($this->barFontColor);
}
//-------------------------------------
//设定图像背景颜色
//-------------------------------------
function setImageColor($colorStr)
{
$this->imageColor = $colorStr;
}
//-------------------------------------
//取得图像背景颜色
//-------------------------------------
function getImageColor()
{
return $this->getImageColorAllocate($this->imageColor);
}
//-------------------------------------
//设定刻度线颜色
//-------------------------------------
function setBorderColor($colorStr)
{
$this->borderColor = $colorStr;
}
//-------------------------------------
//取得刻度线颜色
//-------------------------------------
function getBorderColor()
{
return $this->getImageColorAllocate($this->borderColor);
}
//-------------------------------------
//设定X,Y夹角
//-------------------------------------
function setAngle($angle)
{
$this->angle = $angle;
}
//-------------------------------------
//设定刻度线数量
//-------------------------------------
function setLineCount($num)
{
$this->lineCount = $num;
}
//-------------------------------------
//设定刻度线间距
//-------------------------------------
function setLineH($height)
{
$this->lineH = $height;
}
//-------------------------------------
//设定输出图片格式
//-------------------------------------
function setImageType($ext)
{
$this->imageType = $ext;
}
//-------------------------------------
//角度转换成弧度
//-------------------------------------
function deg2rad($angle)
{
return $angle * (pi() / 180);
}
//-------------------------------------
//画图形
//-------------------------------------
function draw()
{
//edit by wsjakey 11-20
//$this->setDataArray();
//end edit by wsjakey 11-20
//计算Y轴标签宽度
$fontW = imageFontWidth($this->fontSize);
$labelMaxW = strLen($this->getMaxData()) * $fontW;
//间距
$space = $this->imageBorderSpace;
//图布
$bgSideW = ($this->barSideW + $space) * cos($this->deg2rad($this->angle));
$bgFaceW = $this->barFaceW + $this->barFaceW * 3 * $this->xPointCount + $this->barFaceW;
$imageW = $space + $labelMaxW + $space + $bgSideW + $bgFaceW + $space;
$bgFaceH = $space + $this->lineH * $this->lineCount;
$bgFloodH = ($this->barSideW + $space) * sin($this->deg2rad($this->angle));
$imageH = $space + $bgFaceH + $bgFloodH + $space + imageFontHeight($this->fontSize) + $space;
$this->image = imageCreate($imageW,$imageH);
imageFill($this->image,0,0,$this->getImageColor());
//背景正面
$pointArray1[0] = $space + $labelMaxW + $space + $bgSideW;
$pointArray1[1] = $space;
$pointArray1[2] = $pointArray1[0];
$pointArray1[3] = $pointArray1[1] + $bgFaceH;
$pointArray1[4] = $pointArray1[2] + $bgFaceW;
$pointArray1[5] = $pointArray1[3];
$pointArray1[6] = $pointArray1[4];
$pointArray1[7] = $pointArray1[1];
imageFilledPolygon($this->image,$pointArray1,4,$this->getBgFaceColor());
//背景侧面
$pointArray2[0] = $pointArray1[0];
$pointArray2[1] = $pointArray1[1];
$pointArray2[2] = $pointArray2[0] - $bgSideW;
$pointArray2[3] = $pointArray2[1] + tan($this->deg2rad($this->angle)) * $bgSideW;
$pointArray2[4] = $pointArray2[2];
$pointArray2[5] = $pointArray2[3] + $bgFaceH;
$pointArray2[6] = $pointArray2[0];
$pointArray2[7] = $pointArray2[1] + $bgFaceH;
imageFilledPolygon($this->image,$pointArray2,4,$this->getBgSideColor());
//背景底面
$pointArray3[0] = $pointArray2[6];
$pointArray3[1] = $pointArray2[7];
$pointArray3[2] = $pointArray3[0] - $bgSideW;
$pointArray3[3] = $pointArray3[1] + $bgFloodH;
$pointArray3[4] = $pointArray3[2] + $bgFaceW;
$pointArray3[5] = $pointArray3[3];
$pointArray3[6] = $pointArray3[0] + $bgFaceW;
$pointArray3[7] = $pointArray3[1];
imageFilledPolygon($this->image,$pointArray3,4,$this->getBgFloodColor());
//刻度线,一共 lineCount-1 条
for($i = 1; $i lineCount; $i++)
{
imageLine($this->image,$pointArray1[0],$space * 2 + $this->lineH * $i,$pointArray1[6],$space * 2 + $this->lineH * $i,$this->getBorderColor());
}
//Y轴标签
$stepValue = round($this->getMaxData() / $this->lineCount);
for($i = 0; $i lineCount; $i++)
{
$labelValue = $i * $stepValue;
imageString($this->image,$this->fontSize,$space,$pointArray3[1] - $i * $this->lineH - imageFontHeight($this->fontSize) / 2,$labelValue,$this->getLabelFontColor());
}
//X轴标签
for($i = 0; $i xPointCount; $i++)
{
imageString($this->image,$this->fontSize,$pointArray3[0] + $this->barFaceW + $space + $this->barFaceW * 3 * $i,$pointArray3[3] + $space,$i+1,$this->getLabelFontColor());
}
//画柱条
for($i = 0; $i xPointCount; $i++)
{
if($this->dataArray[$i] != 0)
{
$barH = $this->dataArray[$i] * ($this->lineH / $stepValue);
//正面
$pointArray4[0] = $pointArray3[0] + $this->barFaceW * 2 + $this->barFaceW * 3 * $i - $this->barSideW * cos($this->deg2rad($this->angle));
$pointArray4[1] = $pointArray3[1] + $this->barSideW * sin($this->deg2rad($this->angle));
$pointArray4[2] = $pointArray4[0] + $this->barFaceW;
$pointArray4[3] = $pointArray4[1];
$pointArray4[4] = $pointArray4[2];
$pointArray4[5] = $pointArray4[3] - $barH;
$pointArray4[6] = $pointArray4[0];
$pointArray4[7] = $pointArray4[5];
imageFilledPolygon($this->image,$pointArray4,4,$this->getBarFaceColor());
//侧面
$pointArray5[0] = $pointArray4[2];
$pointArray5[1] = $pointArray4[3];
$pointArray5[2] = $pointArray3[0] + $this->barFaceW * 3 * ($i + 1);
$pointArray5[3] = $pointArray3[1];
$pointArray5[4] = $pointArray5[2];
$pointArray5[5] = $pointArray5[3] - $barH;
$pointArray5[6] = $pointArray5[0];
$pointArray5[7] = $pointArray5[1] - $barH;
imageFilledPolygon($this->image,$pointArray5,4,$this->getBarSideColor());
//顶面
$pointArray6[0] = $pointArray4[0];
$pointArray6[1] = $pointArray5[7];
$pointArray6[2] = $pointArray4[2];
$pointArray6[3] = $pointArray6[1];
$pointArray6[4] = $pointArray5[2];
$pointArray6[5] = $pointArray5[5];
$pointArray6[6] = $pointArray6[4] - $this->barFaceW;
$pointArray6[7] = $pointArray6[5];
imageFilledPolygon($this->image,$pointArray6,4,$this->getBarTopColor());
//条柱标注文字
if($this->barFontPos == "top")
{
$barFontY = $pointArray6[1] - imageFontHeight($this->fontSize);
}
else if($this->barFontPos == "middle")
{
$barFontY = $pointArray6[1] + $barH / 2 - imageFontHeight($this->fontSize);
}
else if($this->barFontPos == "bottom")
{
$barFontY = $pointArray4[1];
}
if($this->barFontWay == "down")
{
imageString($this->image,$this->fontSize,$pointArray4[0],$barFontY,$this->dataArray[$i],$this->getBarFontColor());
}
else if($this->barFontWay == "up")
{
imageStringUp($this->image,$this->fontSize,$pointArray4[0],$barFontY,$this->dataArray[$i],$this->getBarFontColor());
}
}
}
//输出图片
if($this->imageType == "gif")
{
Header("Content-type: image/gif");
imageGif($this->image);
}
else if($this->imageType == "jpeg")
{
Header("Content-type: image/jpeg");
imageJpeg($this->image);
}
else if($this->imageType == "png")
{
Header("Content-type: image/gif");
//imagePng($this->image,$this->fileName.".png");
imagePng($this->image);
}
imageDestroy($this->image);
}
}
$bar = new Bar();
$str = "10:4:8:2:9:6:10";
$dataArray = explode(":", $str);
$xLabelArray = explode(":", $str);
$xPointCount = sizeof($dataArray);
$bar->setDataArray($xLabelArray, $dataArray, $xPointCount);
$bar->draw();
//end by wsjakey 11-20
?>
相关阅读 更多 +