0

次のコードは、pchart を使用してグラフを生成するために使用しているものです。グラフの一部を生成できました。現時点でのグラフは次のようなります。入りました。私がやりたいことは、手動でデータを入力せずに 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;

どこが間違っていますか?

ありがとう

4

2 に答える 2

1

回答者ごとに 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'])); 
}

この特定のグラフ作成アプリがどのように機能するかはわかりませんが、これである程度のアイデアが得られるはずです。

于 2012-04-07T23:50:53.697 に答える
0

DBからデータを引き出す前に、addPointsメソッドにデータを渡そうとしています。データも少し奇妙に使用しているようです。行の潜在的な値を列名として使用しています。@nnicholspostのようなものを採用する必要があります。

メソッドの2番目のオプションとして何を渡そうとしていますか?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"); 
?>
于 2012-04-07T23:59:19.193 に答える