2012-04-07 71 views
0

所以下面的代码是我使用的尝试使用pchart生成图表的内容。我已经能够生成一些图。此刻的图形看起来是这样的:当http://befoz.netau.net/charts.php应该看起来像http://befoz.netau.net/chart2.php第二张图是基于我手动输入的值。我想要做的是从mysql数据库中检索数据,而无需手动输入数据,以便动态生成图表。如何检索字段名和列数据,生成图表

这是我的代码。我不确定是否查询或不正确的数组导致图表不生成响应。

<?php  
/* CAT:Bar Chart */ 

/* pChart library inclusions */ 
include("pchart/class/pData.class.php"); 
include("pchart/class/pDraw.class.php"); 
include("pchart/class/pImage.class.php"); 

/* Create and populate the pData object */ 
$myData = new pData(); 
$myData->addPoints(array($Yes,$Result)); 
$myData->addPoints(array($No,$Result)); 
$myData->addPoints(array($Undecided,$Result)); 
$myData->setAxisName(0,"Number of Responses"); 
$myData->addPoints(array("Yes","No","Undecided"),"Types of Responses"); 
$myData->setSerieDescription("Types of Responses","Types of Responses"); 
$myData->setAbscissa("Types of Responses"); 
$myData->setAbscissaName("Types of Responses"); 

/* Connect to the MySQL database */ 

$db = mysql_connect("host", "user", "pass"); 
mysql_select_db("thedatabase",$db); 

/* Build the query that will returns the data to graph */ 

$Requete = "SELECT `Do you have an interest in Green IT` FROM `replies`"; 

$Result = mysql_query($Requete,$db); 
$Yes=""; $No=""; $Undecided=""; 
while($row = mysql_fetch_array($Result)) 
{ 

/* Push the results of the query in an array */ 
$Yes[] = $row["Yes"]; 
$No[] = $row["No"]; 
$Undecided[] = $row["Undecided"]; 
} 

/* Create the pChart object */ 
$myPicture = new pImage(500,500,$myData); 
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100)); 
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20)); 
$myPicture->setFontProperties(array("FontName"=>"pchart/fonts/pf_arma_five.ttf","FontSize"=>6)); 

/* Draw the chart scale */ 
$myPicture->setGraphArea(100,30,480,480); 
$myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10,"Pos"=>SCALE_POS_TOPBOTTOM)); 

/* Turn on shadow computing */ 
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10)); 

/* Create the per bar palette */ 
$Palette = array("0"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100), 
       "1"=>array("R"=>224,"G"=>100,"B"=>46,"Alpha"=>100), 
       "2"=>array("R"=>224,"G"=>214,"B"=>46,"Alpha"=>100), 
       "3"=>array("R"=>46,"G"=>151,"B"=>224,"Alpha"=>100), 
       "4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100), 
       "5"=>array("R"=>224,"G"=>46,"B"=>117,"Alpha"=>100), 
       "6"=>array("R"=>92,"G"=>224,"B"=>46,"Alpha"=>100), 
       "7"=>array("R"=>224,"G"=>176,"B"=>46,"Alpha"=>100)); 

/* Draw the chart */ 
$myPicture->drawBarChart(array("DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"Rounded"=>TRUE,"Surrounding"=>30,"OverrideColors"=>$Palette)); 

/* Write the legend */ 
$myPicture->drawLegend(570,215,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL)); 

/* Render the picture (choose the best way) */ 
$myPicture->autoOutput("pictures/example.drawBarChart.palette.png"); 
?> 

所以我的结构看起来像这样在PHP我管理

ROWS Do you have an interest in Green IT 
9 No 
3 Undecided 
16 Yes 



CREATE TABLE `replies` (
`ID` INTEGER NOT NULL AUTO_INCREMENT, 
`Do you have an interest in Green IT` VARCHAR(50), 
`Do you think Green IT is a good a thing` VARCHAR(50), 
`Would you welcome Green IT if it helped the environment` VARCHAR(255), 
`Would you welcome Green IT if you saved money` VARCHAR(255), 
`Incentive for welcoming Green IT` VARCHAR(50), 
`Is UEL doing enough to implement Green IT` VARCHAR(255), 
`Do you like Green IT Modules` VARCHAR(50), 
`Your rough monthly cost on travel to UEL` VARCHAR(255), 
`Additional comments` VARCHAR(50), 
`Should there be more green modules at UEL` VARCHAR(255), 
`What Year of Study Are you In` VARCHAR(50), 
`If you did not fill in the quesionnaire, why not` VARCHAR(255), 
PRIMARY KEY (`ID`) 
) ENGINE=myisam DEFAULT CHARSET=utf8; 

我要去哪里错了?

感谢

+0

您使用$结果,$是$无和$未定变量填充之前数据 – kappa 2012-04-07 22:24:23

+0

所以我应该移动查询生成的图形过吗? – 2012-04-07 22:29:06

+0

你的桌子的结构是什么?发布CREATE TABLE语句。 – nnichols 2012-04-07 22:50:23

回答

1

假设有每个受访者的一个记录的查询应该是这样的 -

SELECT `Do you have an interest in Green IT`, COUNT(*) AS num 
FROM replies 
GROUP BY `Do you have an interest in Green IT` 

那么由该查询返回的行可以作为数据集中点。如您需要,以便将数据传递到图表类第一运行查询也有人指出 -

<?php  
/* CAT:Bar Chart */ 

/* pChart library inclusions */ 
include("pchart/class/pData.class.php"); 
include("pchart/class/pDraw.class.php"); 
include("pchart/class/pImage.class.php"); 

/* Create and populate the pData object */ 
$myData = new pData(); 
$myData->setAxisName(0,"Number of Responses"); 
$myData->setSerieDescription("Types of Responses","Types of Responses"); 
$myData->setAbscissa("Types of Responses"); 
$myData->setAbscissaName("Types of Responses"); 

/* Connect to the MySQL database */ 

$db = mysql_connect("host", "user", "pass"); 
mysql_select_db("thedatabase",$db); 

/* Build the query that will returns the data to graph */ 

$Requete = "SELECT `Do you have an interest in Green IT` AS resp, COUNT(*) AS num FROM replies GROUP BY resp"; 

$Result = mysql_query($Requete,$db); 

while($row = mysql_fetch_array($Result)) 
{ 
    $myData->addPoints(array($row['resp'], $row['num'])); 
} 

我不知道如何这个特殊的制图应用的工作原理,但是这应该给你一些想法。

+0

感谢你们俩都非常的帮助。尽管我遇到了问题 - 那就是数据在整个酒吧中被多次复制。我试图将其限制为3个小节,但由于我只想为字段“您对绿色IT有兴趣”而不是其他任何内容输出图表,因此难以做到这一点。它确实有效,但是,这只是一个小窍门,我无法让它为每个响应显示单条。即。是(19)否(6)未定(3),而不是说我现在有酒吧众多。谢谢 – 2012-04-08 01:06:37

+0

尝试检查您的查询,通过在PMA中运行它或任何您首选的数据库工具。如果您添加了GROUP BY子句,则每个不同的响应应该只有一条记录。 – nnichols 2012-04-08 01:20:19

0

您试图将数据传递到addPoints方法之前您已从数据库中提取数据。看起来你也有点奇怪地使用这些数据。您将行中的潜在值用作列名称。你需要使用@nnichols post之类的东西。

作为addPoints方法中的第二个选项,您想传递什么? - 您正在传递数据库资源,但我觉得图表应用程序在需要特定数字时会需要该数据 - 这是否合计你想传递的行数?

我也建议重命名表中的列,坚持使用名称has_interest_in_green_it而不是Do you have an interest in Green IT

你需要使用它来讲绕前就使用这种方式作为数据的获取:

<?php  
/* CAT:Bar Chart */ 

/* pChart library inclusions */ 
include("pchart/class/pData.class.php"); 
include("pchart/class/pDraw.class.php"); 
include("pchart/class/pImage.class.php"); 

/* Connect to the MySQL database */ 

$db = mysql_connect("host", "user", "pass"); 
mysql_select_db("thedatabase",$db); 

/* Build the query that will returns the data to graph */ 

$Yes = ""; 
$No = ""; 
$Undecided = ""; 

$Requete = "SELECT `Do you have an interest in Green IT` FROM `replies`"; 

$Result = mysql_query($Requete,$db); 

while($row = mysql_fetch_array($Result)) 
{ 
    /* Push the results of the query in an array */ 
    $Yes[] = $row["Yes"]; 
    $No[] = $row["No"]; 
    $Undecided[] = $row["Undecided"]; 
} 

/* Create and populate the pData object */ 
$myData = new pData(); 
$myData->addPoints(array($Yes,$Result)); 
$myData->addPoints(array($No,$Result)); 
$myData->addPoints(array($Undecided,$Result)); 
$myData->setAxisName(0,"Number of Responses"); 
$myData->addPoints(array("Yes","No","Undecided"),"Types of Responses"); 
$myData->setSerieDescription("Types of Responses","Types of Responses"); 
$myData->setAbscissa("Types of Responses"); 
$myData->setAbscissaName("Types of Responses"); 

/* Create the pChart object */ 
$myPicture = new pImage(500,500,$myData); 
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100)); 
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20)); 
$myPicture->setFontProperties(array("FontName"=>"pchart/fonts/pf_arma_five.ttf","FontSize"=>6)); 

/* Draw the chart scale */ 
$myPicture->setGraphArea(100,30,480,480); 
$myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10,"Pos"=>SCALE_POS_TOPBOTTOM)); 

/* Turn on shadow computing */ 
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10)); 

/* Create the per bar palette */ 
$Palette = array("0"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100), 
       "1"=>array("R"=>224,"G"=>100,"B"=>46,"Alpha"=>100), 
       "2"=>array("R"=>224,"G"=>214,"B"=>46,"Alpha"=>100), 
       "3"=>array("R"=>46,"G"=>151,"B"=>224,"Alpha"=>100), 
       "4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100), 
       "5"=>array("R"=>224,"G"=>46,"B"=>117,"Alpha"=>100), 
       "6"=>array("R"=>92,"G"=>224,"B"=>46,"Alpha"=>100), 
       "7"=>array("R"=>224,"G"=>176,"B"=>46,"Alpha"=>100)); 

/* Draw the chart */ 
$myPicture->drawBarChart(array("DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"Rounded"=>TRUE,"Surrounding"=>30,"OverrideColors"=>$Palette)); 

/* Write the legend */ 
$myPicture->drawLegend(570,215,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL)); 

/* Render the picture (choose the best way) */ 
$myPicture->autoOutput("pictures/example.drawBarChart.palette.png"); 
?>