View unanswered posts | View active topics It is currently Wed May 08, 2024 8:06 pm



Reply to topic  [ 2 posts ] 
Data from MySQL - drawLineChart 
Author Message
New pChart user
New pChart user

Joined: Sat Jan 29, 2011 7:29 pm
Posts: 1
Post Data from MySQL - drawLineChart
I'm trying to use this example to show data from a mysql table:
Code:
ID | name | site_w | site_type
1  | alfa |     1     |   type2
2  | beta |     1     |   type1
3  | ypsi |     2     |   type2
4  | rumu |     4     |   type1
5  | klai |     4     |   type1
6  | mars |     4     |   type2
7  | orio |     5     |   type1
8  | falc |     6     |   type1
9  | lion |     6     |   type2
10 | seti |     6     |   type1
11 | sali |     6     |   type2
12 | cotn |     6     |   type1
13 | zark |     6     |   type2

But I'm doing something wrong... This is my code:
Code:
<?php
include("includes/config.php");
/* pChart library inclusions */
include("class/pData.class.php");
include("class/pDraw.class.php");
include("class/pImage.class.php");

function dbq($sql , $assoc = false) {

   $result = mysql_query($sql);
   if ($assoc == true) {
      while ($row = mysql_fetch_assoc($result)) {
         $return[] = $row;
      }
   } else {
      while ($row = mysql_fetch_array($result)) {
         $return[] = $row;
      }
   }
   return $return;
}

function db_getVal($sql) {
   $result = mysql_query($sql) or die($sql);
   $row = mysql_fetch_array($result);
   return $row[0];
}

$a = dbq("select distinct site_w from sites where site_w > 0");
foreach ($a as $arr) {
   $site_week[] = $arr['site_w'];
   $type1[] = db_getVal("select count(*) from sites where site_w = '{$site_week}' and site_type='type1'");
   $type2[] = db_getVal("select count(*) from sites where site_w = '{$site_week}' and site_type='type2'");
}
/* Create and populate the pData object */
$MyData = new pData(); 
//$MyData->addPoints(array(-4,VOID,VOID,12,8,3),"Probe 1");
//$MyData->addPoints(array(3,12,15,8,5,-5),"Probe 2");
//$MyData->addPoints(array(2,7,5,18,19,22),"Probe 3");
$MyData->addPoints($site_week,"Weeks");
$MyData->addPoints($type1,"Type 1");
$MyData->addPoints($type2,"Type 2");
$MyData->setSerieTicks("Type 1",4);
$MyData->setSerieWeight("Type 2",2);
$MyData->setAxisName(0,"Swaps");
$MyData->addPoints($site_week,"Weeks");
$MyData->setSerieDescription("Weeks","Weeks");
$MyData->setAbscissa("Weeks");

/* Create the pChart object */
$myPicture = new pImage(700,230,$MyData);

/* Draw the background */
$Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
$myPicture->drawFilledRectangle(0,0,700,230,$Settings);

/* Overlay with a gradient */
$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
$myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,$Settings);
$myPicture->drawGradientArea(0,0,700,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>80));

/* Add a border to the picture */
$myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0));

/* Write the picture title */
$myPicture->setFontProperties(array("FontName"=>"fonts/Silkscreen.ttf","FontSize"=>6));
$myPicture->drawText(10,13,"drawPlotChart() - draw a plot chart",array("R"=>255,"G"=>255,"B"=>255));

/* Write the chart title */
$myPicture->setFontProperties(array("FontName"=>"fonts/Forgotte.ttf","FontSize"=>11));
$myPicture->drawText(250,55,"Average temperature",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));

/* Draw the scale and the 1st chart */
$myPicture->setGraphArea(60,60,450,190);
$myPicture->drawFilledRectangle(60,60,450,190,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10));
$myPicture->drawScale(array("DrawSubTicks"=>TRUE));
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
$myPicture->setFontProperties(array("FontName"=>"fonts/pf_arma_five.ttf","FontSize"=>6));
$myPicture->drawLineChart(array("DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_AUTO));
$myPicture->setShadow(FALSE);

/* Draw the scale and the 2nd chart */
$myPicture->setGraphArea(500,60,670,190);
$myPicture->drawFilledRectangle(500,60,670,190,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10));
$myPicture->drawScale(array("Pos"=>SCALE_POS_TOPBOTTOM,"DrawSubTicks"=>TRUE));
$myPicture->setShadow(TRUE,array("X"=>-1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
$myPicture->drawLineChart();
$myPicture->setShadow(FALSE);

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

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

I need to chart the number of entries of each type in any week presented at db. Someone can help me?

Thank you


Sun Feb 13, 2011 9:48 pm
Profile
pChart user
pChart user

Joined: Fri Jan 21, 2011 12:58 pm
Posts: 6
Post Re: Data from MySQL - drawLineChart
I got the mySQL to work, but I needed to make sure I added the data to each series on the graph in the while loop. Otherwise the data won't be stored as an array in each series.

$query="SELECT CONCAT(left(whitestar.totalServiceStart, 3),'0s ') AS decade, avg(length) as avlength, avg(breadth) as avbreadth, avg(depth) as avdepth from whitestar WHERE wslServiceStart > 0 GROUP BY decade";
$result = mysql_query($query);
mysql_close();

while( $row = mysql_fetch_array( $result ) )

{

$myData->addPoints($row["avlength"],"Serie1");
$myData->addPoints($row["avbreadth"],"Serie2");
$myData->addPoints($row["avdepth"],"Serie3");
$myData->addPoints($row["decade"],"Decade");

}


Fri Feb 18, 2011 6:43 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron