如何在 PHP 中绘制图形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/645582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:22:06  来源:igfitidea点击:

How to draw a graph in PHP?

phpgraphicsdrawgraph-drawing

提问by johnnaples

Hey I want to draw a graph(Stdent mark distribution) in my site based on PHP. How can I do this?

嘿,我想基于 PHP 在我的站点中绘制一个图表(Stdent 标记分布)。我怎样才能做到这一点?

回答by John McCollum

pChartis another great PHP graphing library.

pChart是另一个很棒的 PHP 图形库。

回答by johnnaples

<?
# ------- The graph values in the form of associative array
$values=array(
    "Jan" => 110,
    "Feb" => 130,
    "Mar" => 215,
    "Apr" => 81,
    "May" => 310,
    "Jun" => 110,
    "Jul" => 190,
    "Aug" => 175,
    "Sep" => 390,
    "Oct" => 286,
    "Nov" => 150,
    "Dec" => 196
);


$img_width=450;
$img_height=300; 
$margins=20;


# ---- Find the size of graph by substracting the size of borders
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2; 
$img=imagecreate($img_width,$img_height);


$bar_width=20;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);


# -------  Define Colors ----------------
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220);

# ------ Create the border around the graph ------

imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);


# ------- Max value is required to adjust the scale -------
$max_value=max($values);
$ratio= $graph_height/$max_value;


# -------- Create scale and draw horizontal lines  --------
$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;

for($i=1;$i<=$horizontal_lines;$i++){
    $y=$img_height - $margins - $horizontal_gap * $i ;
    imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
    $v=intval($horizontal_gap * $i /$ratio);
    imagestring($img,0,5,$y-5,$v,$bar_color);

}


# ----------- Draw the bars here ------
for($i=0;$i< $total_bars; $i++){ 
    # ------ Extract key and value pair from the current pointer position
    list($key,$value)=each($values); 
    $x1= $margins + $gap + $i * ($gap+$bar_width) ;
    $x2= $x1 + $bar_width; 
    $y1=$margins +$graph_height- intval($value * $ratio) ;
    $y2=$img_height-$margins;
    imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);        
    imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}
header("Content-type:image/png");
imagepng($img);
$_REQUEST['asdfad']=234234;

?>

回答by Sukasa

Your best bet is to look up php_gd2. It's a fairly decent image library that comes with PHP (just disabled in php.ini), and not only can you output your finished images in a couple formats, it's got enough functions that you should be able to do up a good graph fairly easily.

最好的办法是查找 php_gd2。这是一个相当不错的 PHP 附带的图像库(只是在 php.ini 中禁用),不仅可以以几种格式输出完成的图像,而且它具有足够的功能,您应该能够相当容易地制作出好的图形.

EDIT: it might help if I gave you a couple useful links:

编辑:如果我给你几个有用的链接可能会有所帮助:

http://www.libgd.org/- You can get the latest php_gd2here
http://ca3.php.net/gd- The php_gdmanual.

http://www.libgd.org/- 你可以在php_gd2这里获得最新的
http://ca3.php.net/gd-php_gd手册。

回答by Bob Fanger

You can use google's chart apito generate charts.

可以使用google的chart api来生成图表。

回答by stukelly

There are a number of libraries available for generating graphs.

有许多库可用于生成图形。

More are listed above and here.

上面和此处列出了更多内容。

回答by dyve

By far the easiest solution is to just use the Google Chart API http://code.google.com/apis/chart/

到目前为止,最简单的解决方案是使用 Google Chart API http://code.google.com/apis/chart/

You can make bar graphs, pie charts, use 3D, and it's as easy as building a url with some parameters. See the simple example below.

您可以制作条形图、饼图、使用 3D,就像使用一些参数构建 url 一样简单。请参阅下面的简单示例。

This Pie Chart is really easy to make

这个饼图真的很容易制作

回答by John Biesnecker

There are also several graphing libraries available for PHP to make your life simpler. JPGraphis a good (non-free) one.

还有几个图形库可用于 PHP,使您的生活更简单。JPGraph是一个很好的(非免费的)。

回答by Johan

Have no idea about gd2, but I have done a similar thing with gd and it was not that hard.

不知道 gd2,但我用 gd 做了类似的事情,并没有那么难。

Go to http://www.php.net/and search for things like

转到http://www.php.net/并搜索诸如

  • ImageCreate
  • imageline
  • imagestring
  • 图像创建
  • 图像线
  • 图像串

It's not as flashy as some of those other solution out there, but since you generate a picture it will work in all browsers. (except lynx... :-) )

它不像其他一些解决方案那么华而不实,但是由于您生成了一张图片,因此它可以在所有浏览器中使用。(猞猁除外... :-) )

/Johan

/约翰



Update:I nearly forgot, don't use jpeg for this type of pictures. The jpeg artefacts will be really annoying, png is a better solution.

更新:我差点忘了,这种类型的图片不要使用 jpeg。jpeg 人工制品会很烦人,png 是更好的解决方案。