GD PHP动态饼图生成问题
我使用图像填充弧函数创建了带有 GD 扩展的动态饼图。我通过 HTTP GET 变量向此脚本传递最多 11 个值。第一个值 n 是后面的值的数量。 n1、n2、n3 等是数据本身。这些是整数百分比。目标是制作一个图表,以图形形式显示饼图中的百分比。
当我运行此命令时:
piechart.php?n=2&n1=20&n2=80
我只得到一个黑框。有什么想法吗?如有任何问题请询问 - 谢谢!
<?php
// var load
$size=500;
//HTTP GET vars:
//N is the number of sections we have
//n1 is the percentage for part1
//n2 is the percentage for part2, and so on
$num=$_GET["n"];
//want to make a hard limit at 10 different sections
$num=min($num,10);
$percents;
$angles;
$angles[0]=0;
$percents[0]=NULL;
//load percents array. First value is NULL
for ($c=1;$c<=$num;$c++)
{
$percents[$c]=(int)$_GET["n".$c];
$angles[$c]=(int)round((($percents[$c-1]+$percents[$c])/100)*360);
}
$angles[$num]=360;
//create image
$half=round($size/2);
$image = imagecreatetruecolor($size, $size);
// colors
$colorR=array(0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x74, 0x33, 0xFF, 0x33, 0x66);
$colorG=array(0x00, 0x80, 0x00, 0xA5, 0xFF, 0xFF, 0x99, 0x00, 0x33, 0xFF);
$colorB=array(0xFF, 0x00, 0x00, 0x00, 0x00, 0xD4, 0xFF, 0x66, 0x99, 0x33);
// shaded colors
$darkColorR=array(0x00, 0x00, 0x80, 0x80, 0x80, 0x3A, 0x1A, 0x80, 0x1A, 0x33);
$darkColorG=array(0x00, 0x40, 0x00, 0x52, 0x80, 0x80, 0x4C, 0x00, 0x1A, 0x80);
$darkColorB=array(0x80, 0x00, 0x00, 0x00, 0x00, 0x6A, 0x80, 0x33, 0x4C, 0x1A);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
// make the 3D effect
$tempDarkColor;
for ($i = $half+round(.1*$half); $i > $half; $i--)
{
for ($j=0;$j<(count($angles)-1)&&$angles[$j]<180;$j++)
{
if ($darkColorR[$j]==NULL||$darkColorG[$j]==NULL||$darkColorB[$j]==NULL)
{
//if one of the colors is missing, set the entire color to white
$darkColorR[$j]=0x00;
$darkColorG[$j]=0x00;
$darkColorB[$j]=0x00;
}
$tempDarkColor[$j]= imagecolorallocate($image, $darkColorR[$j],$darkColorG[$j],$darkColorB[$j]);
imagefilledarc($image, $half, $i, $size, $half, $angles[$j], $angles[$j+1] , $tempDarkColor[$j], IMG_ARC_PIE);
}
}
//make the image
$imageColor;
for ($k=0;$k<(count($angles)-1);$k++)
{
if ($darkColorR[$k]==NULL||$darkColorG[$k]==NULL||$darkColorB[$k]==NULL)
{
//if one of the colors is missing, set the entire color to white
$colorR[$k]=0x00;
$colorG[$k]=0x00;
$colorB[$k]=0x00;
}
$tempColor[$k]= imagecolorallocate($image, $darkColorR[$k],$darkColorG[$k],$darkColorB[$k]);
imagefilledarc($image, $half, $half, $size, $half, $angles[$k], $angles[$k+1] , $tempColor[$k], IMG_ARC_PIE);
}
// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
I've created a dynamic pie chart with the GD extension using the image filled arc function. I pass to this script, via HTTP GET variables, up to 11 values. the first value, n, is the number of values that follow. n1,n2,n3, etc. are the data itself. These are integer precentages. The goal is to make a chart that will show the percentages in a pie chart in graphical form.
When I run this as:
piechart.php?n=2&n1=20&n2=80
I only get a black box. Any ideas? Any questions please ask - thanks!
<?php
// var load
$size=500;
//HTTP GET vars:
//N is the number of sections we have
//n1 is the percentage for part1
//n2 is the percentage for part2, and so on
$num=$_GET["n"];
//want to make a hard limit at 10 different sections
$num=min($num,10);
$percents;
$angles;
$angles[0]=0;
$percents[0]=NULL;
//load percents array. First value is NULL
for ($c=1;$c<=$num;$c++)
{
$percents[$c]=(int)$_GET["n".$c];
$angles[$c]=(int)round((($percents[$c-1]+$percents[$c])/100)*360);
}
$angles[$num]=360;
//create image
$half=round($size/2);
$image = imagecreatetruecolor($size, $size);
// colors
$colorR=array(0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x74, 0x33, 0xFF, 0x33, 0x66);
$colorG=array(0x00, 0x80, 0x00, 0xA5, 0xFF, 0xFF, 0x99, 0x00, 0x33, 0xFF);
$colorB=array(0xFF, 0x00, 0x00, 0x00, 0x00, 0xD4, 0xFF, 0x66, 0x99, 0x33);
// shaded colors
$darkColorR=array(0x00, 0x00, 0x80, 0x80, 0x80, 0x3A, 0x1A, 0x80, 0x1A, 0x33);
$darkColorG=array(0x00, 0x40, 0x00, 0x52, 0x80, 0x80, 0x4C, 0x00, 0x1A, 0x80);
$darkColorB=array(0x80, 0x00, 0x00, 0x00, 0x00, 0x6A, 0x80, 0x33, 0x4C, 0x1A);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
// make the 3D effect
$tempDarkColor;
for ($i = $half+round(.1*$half); $i > $half; $i--)
{
for ($j=0;$j<(count($angles)-1)&&$angles[$j]<180;$j++)
{
if ($darkColorR[$j]==NULL||$darkColorG[$j]==NULL||$darkColorB[$j]==NULL)
{
//if one of the colors is missing, set the entire color to white
$darkColorR[$j]=0x00;
$darkColorG[$j]=0x00;
$darkColorB[$j]=0x00;
}
$tempDarkColor[$j]= imagecolorallocate($image, $darkColorR[$j],$darkColorG[$j],$darkColorB[$j]);
imagefilledarc($image, $half, $i, $size, $half, $angles[$j], $angles[$j+1] , $tempDarkColor[$j], IMG_ARC_PIE);
}
}
//make the image
$imageColor;
for ($k=0;$k<(count($angles)-1);$k++)
{
if ($darkColorR[$k]==NULL||$darkColorG[$k]==NULL||$darkColorB[$k]==NULL)
{
//if one of the colors is missing, set the entire color to white
$colorR[$k]=0x00;
$colorG[$k]=0x00;
$colorB[$k]=0x00;
}
$tempColor[$k]= imagecolorallocate($image, $darkColorR[$k],$darkColorG[$k],$darkColorB[$k]);
imagefilledarc($image, $half, $half, $size, $half, $angles[$k], $angles[$k+1] , $tempColor[$k], IMG_ARC_PIE);
}
// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是出错的地方:
在 PHP 中,你不能使用 $var == NULL 来将值与 NULL 进行比较,你必须使用 is_null 函数:
它的作用是获取 $darkColorR[$i] 的值并检查它是否为 NULL。我认为在您的情况下,您正在尝试检查数组是否具有该特定索引的条目,这是完全不同的。您可以使用 isset 函数来检查数组是否具有给定索引的条目:
将它们放在一起给出以下代码:
This is where it goes wrong:
In PHP you can't compare values to NULL with $var == NULL, you have to use the is_null function:
What this does is get the value of $darkColorR[$i] and check whether it is NULL. I think in your case you are trying to check whether the array has an entry for that specific index, which is something different entirely. You can use the isset function to check if an array has an entry with a given index:
Putting this together gives the following code:
与您的问题无关,但您可以查看 Google Chart API,它是用它更容易创建图表。
nothing to do with your problem but you might look into the Google Chart API, it would be more easy to create chart with it.