php统计数据作折线图 php处理excel图表( 二 )


$recWidth = ; // 小矩形的宽
$recHeight = ; // 小矩形的高
$space = ; // 小矩形与条形图的间距
$tmpWidth = ;
// 设置图片的宽、高
$lineChartWidth = $lineWidth*count($this-xdata) + $chartLeft - $lineWidth/. ;
// 两个系列数据以上的加上小矩形的宽
if($ydataNum) {
$tmpWidth = $this-arrayLengthMax($this-seriesName)**/ + $space + $recWidth + + $margin;
}
$this-width = $lineChartWidth + $tmpWidth;
$this-height = ;
$this-image = imagecreatetruecolor($this-width ,$this-height); // 准备画布
$this-bgcolor = imagecolorallocate($this-image,,,); // 图片的背景颜色
// 设置条形图的颜色
$color = array();
foreach($this-color as $col) {
$col = substr($col,,strlen($col)-);
$red = hexdec(substr($col,,));
$green = hexdec(substr($col,,));
$blue = hexdec(substr($col,,));
$color[] = imagecolorallocate($this-image ,$red, $green, $blue);
}
// 设置线段的颜色、字体的颜色、字体的路径
$lineColor = imagecolorallocate($this-image ,xcc,xcc,xcc);
$fontColor = imagecolorallocate($this-image, x,xf,xf);
$fontPath = 'font/simsun.ttc';
imagefill($this-image,,,$this-bgcolor); // 绘画背景
// 绘画图的分短线与左右边线
for($i = ; $i; $i++ ) {
imageline($this-image,$chartLeft-,$lineY-$barHeightMulti*$max//$multi*$i,$lineChartWidth,$lineY-$barHeightMulti*$max//$multi*$i,$lineColor);
imagestring($this-image,,,$lineY-$barHeightMulti*$max//$multi*$i-,floor($max/*$i),$fontColor);
}
imageline($this-image,$chartLeft-,,$chartLeft-,$lineY,$lineColor);
imageline($this-image,$lineChartWidth-,,$lineChartWidth-,$lineY,$lineColor);
$style = array($lineColor,$lineColor,$lineColor,$lineColor,$lineColor,$this-bgcolor,$this-bgcolor,$this-bgcolor,$this-bgcolor,$this-bgcolor);
imagesetstyle($this-image,$style);
// 绘制折线图的分隔线(虚线)
foreach($this-xdata as $key = $val) {
$lineX = $chartLeft + + $lineWidth*$key;
imageline($this-image,$lineX,,$lineX,$lineY,IMG_COLOR_STYLED);
}
// 绘画图的折线
foreach($this-ydata as $key = $val) {
if($ydataNum == ) {
// 一个系列数据时
if($key == count($this-ydata) - ) break;
$lineX = $chartLeft + + $lineWidth*$key;
$lineY = $lineY-$barHeightMulti*($this-ydata[$key+])/$multi;
// 画折线
if($key == count($this-ydata) - ) {
imagefilledellipse($this-image,$lineX+$lineWidth,$lineY,,,$color[]);
}
imageline($this-image,$lineX,$lineY-$barHeightMulti*$val/$multi,$lineX+$lineWidth,$lineY,$color[]);
imagefilledellipse($this-image,$lineX,$lineY-$barHeightMulti*$val/$multi,,,$color[]);
}elseif($ydataNum) {
// 多个系列的数据时
foreach($val as $ckey = $cval) {
if($ckey == count($val) - ) break;
$lineX = $chartLeft + + $lineWidth*$ckey;
$lineY = $lineY-$barHeightMulti*($val[$ckey+])/$multi;
// 画折线
if($ckey == count($val) - ) {
imagefilledellipse($this-image,$lineX+$lineWidth,$lineY,,,$color[$key%count($this-color)]);
}
imageline($this-image,$lineX,$lineY-$barHeightMulti*$cval/$multi,$lineX+$lineWidth,$lineY,$color[$key%count($this-color)]);
imagefilledellipse($this-image,$lineX,$lineY-$barHeightMulti*$cval/$multi,,,$color[$key%count($this-color)]);
}
}
}
// 绘画条形图的x坐标的值
foreach($this-xdata as $key = $val) {
$lineX = $chartLeft + $lineWidth*$key + $lineWidth/ - ;
imagettftext($this-image,,-,$lineX,$lineY+,$fontColor,$fontPath,$this-xdata[$key]);
}
// 两个系列数据以上时绘制小矩形及之后文字说明
if($ydataNum) {
$x = $lineChartWidth + $space;
$y = ;
foreach($this-seriesName as $key = $val) {
imagefilledrectangle($this-image,$x,$y,$x+$recWidth,$y+$recHeight,$color[$key%count($this-color)]);

推荐阅读