您的位置:首页 > 其它

使用jpgraph画图

2011-05-16 20:56 120 查看
最近完成了一个项目,正好其中有很多地方需要用到php画图的功能,然后对生成的图片,通过jpgraph将生产的html,

使用HTML2PDF类将html转化为pdf,然后通过pdf2swf让生产的pdf转化为swf文件,

同时其中有些地方用到了popen函数,用来打开一个打开进程文件指针,比如:

$handle = popen("nohup php " . PROJECT_PATH. "/cron/Index.php Report);

pclose($handle);

这样可以使用nohup让程序永远后台运行。

其中有些地方同时使用了jpgraph和GD库绘图,这样有利于结合原生的GD功能,例如:

ini_set("display_errors", "On");

//图片原始大小
$baseWidth = 400;
$baseHeight = 400;
$extensionHeight = 50;
//作图位置
$left = 30;
$right = 380;
$top = 32;
$bottom = 392;
$height = 362;
$length = 10;
$lineLength = 5;
$srcFile = Ggzg_Config::get("base.url")."/ggdx/gsggdxHalf/?code=" .
$stockCode;
$picData = GetImageSize($srcFile);
switch ($picData[2]) {
case 1: //图片类型,1是GIF图
$img = ImageCreateFromGIF($srcFile);
break;
case 2: //图片类型,2是JPG图
$img = imagecreatefromjpeg($srcFile);
break;
case 3: //图片类型,3是PNG图
$img = ImageCreateFromPNG($srcFile);
break;
}
//中间变量图片
$tempImg = imagecreate($baseWidth,
$baseHeight + $extensionHeight);
$bg = imagecolorallocate($tempImg, 255, 255, 255);
$im = imagecreatetruecolor($baseWidth, ($baseHeight + $extensionHeight));
//复制图片
imagecopyresized($tempImg, $img, 0, 0, 0, 0, $baseWidth,
$baseHeight, $baseWidth, $baseHeight);
imagecopyresized($im, $tempImg, 0, 0, 0, 0, $baseWidth,
$baseHeight + $extensionHeight, $baseWidth,
$baseHeight + $extensionHeight);
//确定作图位置
$color = imagecolorallocate($tempImg, 200, 200, 200);
//测试位置坐标
//           imagettftext($im, 10, 0,54, 360,
//                $color, TTF_DIR . "simsun.ttc", "正法");
/**
* 确定坐标位置
*/
$count = count($date);
//        print_r($date);exit;
//存放小图标横坐标以及类型的数组
$icon = array();
$interval = ($right - $left) / $count;
//        echo $interval;exit;
for ($i = 0; $i < $count; $i ++) {
for ($j = 0; $j < count($arrow); $j ++) {
if ($date[$i] == $arrow[$j][0]) {
$icon[] = array(
'index' => round($i * $interval),
'type' => $arrow[$j][1]
);
}
}
}
$white = imagecolorallocate($im, 255, 255, 255);
//作图开始
for ($i = 0; $i < count($icon); $i ++) {
$x = $icon[$i]['index']+$left;
$p = array(
$x, $height, ($x + $length), $height, ($x + $length),
($height + $length), $x, ($height + $length)
);
switch ($icon[$i]['type']) {
case 1:
$color = imagecolorallocate($im, 253, 15, 16);
imagefilledpolygon($im, $p, 4, $color);
imageline($im, ($x + 1), ($height + 7), ($x + $lineLength),
($height + 3), $white);
imageline($im, ($x + $lineLength), ($height + 3), ($x + 9),
($height + 7), $white);
break;
case 2:
$color = imagecolorallocate($im, 22, 187, 0);
imagefilledpolygon($im, $p, 4, $color);
imageline($im, ($x + 1), ($height + 3), ($x + $lineLength),
($height + 7), $white);
imageline($im, ($x + $lineLength), ($height + 7), ($x + 9),
($height + 3), $white);
break;
case 3:
$color = imagecolorallocate($im, 255, 192, 0);
imagefilledpolygon($im, $p, 4, $color);
imageline($im, ($x + 1), ($height + 5), ($x + 9),
($height + 5), $white);
break;
}
}
$legendLeft = 40;
$legendRight = 370;
$legendTop = 410;
$legendBottom = 440;
//图例区域
$legendArr = array(
$legendLeft, $legendTop, $legendRight, $legendTop,
$legendRight, $legendBottom, $legendLeft, $legendBottom
);
$bg = imagecolorallocate($im, 231, 247, 255);
imagefilledpolygon($im, $legendArr, 4, $bg);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$sep = 110;
$yBegin = 10 + $legendTop;
for ($i = 0; $i < 3; $i ++) {
$xBegin = $legendLeft + 20 + $i * 110;
$p = array(
$xBegin, $yBegin, ($xBegin + $length), $yBegin,
($xBegin + $length), ($yBegin + $length), $xBegin,
($yBegin + $length)
);
switch ($i) {
case 0:
$color = imagecolorallocate($im, 253, 15, 16);
imagefilledpolygon($im, $p, 4, $color);
imageline($im, ($xBegin + 1), ($yBegin + 7),
($xBegin + $lineLength), ($yBegin + 3), $white);
imageline($im, ($xBegin + $lineLength), ($yBegin + 3),
($xBegin + 9), ($yBegin + 7), $white);
//文字
imagettftext($im, 10, 0, ($xBegin + 12),
($yBegin + 10), $black, TTF_DIR . "simsun.ttc", "仅买入,不卖出");
break;
case 1:
$color = imagecolorallocate($im, 22, 187, 0);
imagefilledpolygon($im, $p, 4, $color);
imageline($im, ($xBegin + 1), ($yBegin + 3),
($xBegin + $lineLength), ($yBegin + 7), $white);
imageline($im, ($xBegin + $lineLength), ($yBegin + 7),
($xBegin + 9), ($yBegin + 3), $white);
imagettftext($im, 10, 0, ($xBegin + 12), ($yBegin + 10),
$black, TTF_DIR . "simsun.ttc", "仅卖出,无买入");
break;
case 2:
$color = imagecolorallocate($im, 255, 192, 0);
imagefilledpolygon($im, $p, 4, $color);
imageline($im, ($xBegin + 1), ($yBegin + 5), ($xBegin + 9),
($yBegin + 5), $white);
imagettftext($im, 10, 0, ($xBegin + 12), ($yBegin + 10),
$black, TTF_DIR . "simsun.ttc", "有买有卖");
break;
}
}
header("content-type:image/gif");
ImageJpeg($im);
imagedestroy($im);
}


这样就可以结合jpgraph了。不过一般情况下不会使用到该类,因为如果有更酷的需要,会使用到js的绘图类库,比如:

FusionChart,和AmChart,这样可以更好的使用flash,更炫!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: