Jump to content

[SOLVED] Using the GD Class to draw a dynamic Line-Graph


rilana

Recommended Posts


$x1=$originx-$x_gap;
$y1=$originy;
$first_one="yes";
while($nt=mysql_fetch_array($qt)){
//echo "$nt[month], $nt[sales]";
$x2=$x1+$x_gap; // Shifting in X axis
$y2=$y_max-$nt[miv_wert]; // Coordinate of Y axis
$datum= date("F", strtotime($nt['miv_datum']));

imagettftext($im,8,90,$x2+5,$y0+295,$graph_color,$font,$datum);
//Line above is to print month names on the graph



if($first_one=="no"){ // this is to prevent from starting $x1= and $y1=0
imagesetthickness ($im,$th);
imageline ($im,$x1, $y1,$x2,$y2,$text_colorMiv); // Drawing the line between two points
imageline ($im,0,560,300,560,$grid_color);
}
$x1=$x2; // Storing the value for next draw
$y1=$y2;
$first_one="no"; // Now flag is set to allow the drawing
}

Link to comment
Share on other sites

try


$x1=$originx-$x_gap;
$y1=$originy;
$first_one="yes";

$prevDatum = '';
while($nt=mysql_fetch_array($qt)){
    //echo "$nt[month], $nt[sales]";
    $x2=$x1+$x_gap; // Shifting in X axis
    $y2=$y_max-$nt[miv_wert]; // Coordinate of Y axis
    $datum = date("F", strtotime($nt['miv_datum']));
    
    $month = $datum==$prevDatum ? '' : $datum;                           // check for same month name
    $prevDatum = $datum;
    
    imagettftext($im,8,90,$x2+5,$y0+295,$graph_color,$font,$month);
    //Line above is to print month names on the graph



    if($first_one=="no"){ // this is to prevent from starting $x1= and $y1=0
    imagesetthickness ($im,$th);
    imageline ($im,$x1, $y1,$x2,$y2,$text_colorMiv); // Drawing the line between two points
    imageline ($im,0,560,300,560,$grid_color);
    }
    $x1=$x2; // Storing the value for next draw
    $y1=$y2;
    $first_one="no"; // Now flag is set to allow the drawing
}

Link to comment
Share on other sites

it's called "ternary" if you want to look it up.

 

 

$var = <condition> ? <value if condition true> : <value if condition false>;

 

Short for

 

if ($datum == $prevDatum)          // is datum same as previous datum
{
    $month = '';                          // if it is, print '' instead of repeating the month
}
else
{
    $month = $datum;                 // it's different so print new value
}

Link to comment
Share on other sites

ternary was interesting... I have to keep it in mind! I read the tutorial in the front about handling database. It's a tutorial on how to set up a script that allows you to add, edit and delet all in the same script. I am still working on the backend of this thing, and it was easy to set up a form that enters values to the database. But now I am stuck. The tutorial helped me understand a lot, but now that I am trying to change it to my needs, I am having problems. In the end of the script there is a form

echo <<<NEWENTRY
   <tr>
      <td bgcolor = 'gray'></td>
      <td><input type = 'text' miv_datum = 'miv_datum'></td>
        <td><input type = 'text' miv_wert = 'miv_wert'></td>

      <td bgcolor = 'gray'></td>
   </tr><tr>
      <td></td>
      <td align = 'center'><input type = 'submit' value = 'submit'></td>
      <td></td>
   </tr>
</table>
</form>	 

Somehow this submit button is suposed to send the input text value to the post function.

// INSERT: if we have a name to add...
if($_POST['miv_datum']['miv_wert']) {
   // little bit of cleaning...
   $miv_datum = mysql_real_escape_string($_POST['miv_datum']);
   $miv_wert = mysql_real_escape_string($_POST['miv_wert']);


   // insert new name into table
   $sql = "INSERT INTO miv (ID, miv_datum, miv_wert) VALUES ('','$miv_datum','miv_wert')";
   $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
}

Now the tutorial was only with one text-field in the form, so maby I am doing something wrong by adding more values. Am I supused to make a string or something like that? I tryed different things out, but it's not working. Thank's for helping. Rilana

Link to comment
Share on other sites

I am not shoure do you meen this?

 

echo <<<LISTCOLS
<form action = '{$_SERVER['PHP_SELF']}' method = 'post'>
<table border = '1'>
   <tr>
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=ID'>id</td>
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=miv_datum'>miv_datum</td>
  <td><a href = '{$_SERVER['PHP_SELF']}?orderby=miv_wert'>miv_wert</td>

      <td>delete</td>
   </tr>
LISTCOLS;

Link to comment
Share on other sites

Hi, sorry for taking so long. Thank you verry much I am almost done now. I just have one last thing to worry about. After one year the graph will be to wide to continue. I could be deleting entrys from the database, but it would be much cooler if I could say something like: if id == 52 show no more then 52 entrys, for example if there are 53 entrys, show id 2 till 53... But I dont realy know how to set this up. I probably have to change the whole coding arround.... Can you give me a hint? here is the part that need to be changed

 

$x1=$originx-$x_gap;
$y1=$originy;
$first_one="yes";
while($nt=mysql_fetch_array($qt)){
//echo "$nt[month], $nt[sales]";
$x2=$x1+$x_gap; // Shifting in X axis
$y2=$y_max-$nt[miv_wert]; // Coordinate of Y axis
$datum= date("d.m.y", strtotime($nt['miv_datum']));




    
imagettftext($im,8,90,$x2+5,$y0+295,$graph_color,$font,$datum);
//Line above is to print month names on the graph


if($first_one=="no"){ // this is to prevent from starting $x1= and $y1=0
imagesetthickness ($im,$th);
imageline ($im,$x1, $y1,$x2,$y2,$text_colorMiv); // Drawing the line between two points
imageline ($im,0,560,300,560,$grid_color);
}
$x1=$x2; // Storing the value for next draw
$y1=$y2;
$first_one="no"; // Now flag is set to allow the drawing
}

 

thank you

Link to comment
Share on other sites

Change it back at the query where you sect the data and use LIMIT x,y in your SQL, where x is the record to start at and y is the number of records to retrieve.

 

Get a count of the records, then "... ORDER BY date LIMIT (count-52), 52" so you always get the last 52 records on the file.

Link to comment
Share on other sites

Hi, thank you so much for your feedback. I tryed several things, but I am doing something wrong.

this works...

 

$qt=mysql_query("select * from miv LIMIT 0, 10");

 

But as soon as I try to do this:

$qt=mysql_query("select * from miv ORDER BY miv_datum LIMIT (count-52), 52");

 

the output is empty, what am I doing wrong? thank you, Rilana

Link to comment
Share on other sites

ok sorry, I dont get it. It's not working.

 

$res = mysql_query ("SELECT COUNT(*) FROM miv");
$count = mysql_result($res, 0);
if ($count < 52) $count = 52;
$qt=mysql_query("select * from miv ORDER BY miv_datum LIMIT ($count-52), 52");
header ("Content-type: image/jpg");

 

Maby the mysql_fetch_array($qt) does not work this way anymore, could that be a possibility?

 

$x1=$originx-$x_gap;
$y1=$originy;
$first_one="yes";
while($nt=mysql_fetch_array($qt)){
//echo "$nt[month], $nt[sales]";
$x2=$x1+$x_gap; // Shifting in X axis
$y2=$y_max-$nt[miv_wert]; // Coordinate of Y axis
$datum= date("d.m.y", strtotime($nt['miv_datum']));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.