【问题标题】:how to retrieve data from field name and column to generate graph如何从字段名称和列中检索数据以生成图形
【发布时间】:2012-04-08 08:20:25
【问题描述】:

所以下面的代码是我用来尝试使用 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 my admin 中看起来像这样

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;

我哪里错了?

谢谢

【问题讨论】:

  • 在填充数据之前,您正在使用 $result、$yes、$no 和 $undecided 变量
  • 所以我应该在生成图表之前移动查询?
  • 你的表是什么结构的?发布 CREATE TABLE 语句。
  • 正如@BenGriffiths 所指出的,您确实应该对您的字段名称做一些事情。使用句子是一个非常糟糕的主意。

标签: php mysql


【解决方案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'])); 
}

我不知道这个特殊的图表应用程序是如何工作的,但这应该会给你一些想法。

【讨论】:

  • 非常感谢大家的帮助。虽然我遇到了问题 - 那就是数据正在跨条复制多次。我试图将其限制为 3 条,但这样做有困难,因为我只想输出“你对绿色 IT 有兴趣吗”字段的图表,而不是其他任何东西。它确实有效,但是,这只是一个小问题,我无法让它为每个响应显示单个条。 IE。是 (19) 否 (6) 未定 (3),而不是我现在拥有的众多酒吧。谢谢
  • 尝试通过在 PMA 或任何您喜欢的数据库工具中运行查询来检查您的查询。如果您添加了 GROUP BY 子句,则每个不同的响应应该只有 1 条记录。
【解决方案2】:

您试图在从数据库中提取数据之前将数据传递给 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"); 
?>

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2021-11-26
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多