I am a newbie in php. I was trying to draw a table with some data fetched from
MySQL table and then, draw a graph based on two columns' data of that table.
But, I could not draw the graph after the table; the table is drawn fine, but
NOT the graph. Here is the code, I am calling this file as process.php.
However, I have added the PHPlot codes just below the table codes.
<?php>include('/tmp/phplot.php');
$var_1 = $_POST['userid'];
$var_2 = $_POST['day'];
$var = trim($var_1);
echo"<tr> </tr>";
echo"<tr> </tr>";
if($var =='-Select-' )
{
echo" User day wasn't selected";
exit;
}
$con = mysql_connect("localhost","root","root"); // (host, user,pwd)
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql", $con);
if ($_POST['day']=='-Select-'){
echo" User day wasn't selected; this is the result for user $var for all the dates:";
$result = mysql_query("SELECT * FROM tblGaitUserData WHERE UserID = $var");
}
else{
echo"Query result for User: $var and Date: $var_2::";
$result = mysql_query("SELECT * FROM tblGaitUserData WHERE UserID = $var and Date = STR_TO_DATE('$var_2','%Y-%m-%d')");
}
echo"<br />";
$numrows=mysql_num_rows($result);
if ($numrows == 0)
{
echo"<h4>Results</h4>";
echo"<p>Sorry, your search: "" . $var . "" and "" . $var_2 . "" returned zero results</p>";
exit;
}
echo"<table border='1'><tr><th>UserID</th><th>Date</th><th>System time</th><th>Height</th><th>Duration</th><th>Length</th><th>Speed</th><th>ANG</th><th>X_start</th><th>Y_start</th></tr>";
while($row = mysql_fetch_array($result))
{
echo"<tr>";
echo"<td>" . $row['UserID'] . "</td>";
echo"<td>" . $row['Date'] . "</td>";
echo"<td>" . $row['System_time'] . "</td>";
echo"<td>" . $row['Height'] . "</td>";
echo"<td>" . $row['Duration'] . "</td>";
echo"<td>" . $row['Length'] . "</td>";
echo"<td>" . $row['Speed'] . "</td>";
echo"<td>" . $row['ANG'] . "</td>";
echo"<td>" . $row['X_start'] . "</td>";
echo"<td>" . $row['Y_start'] . "</td>";
echo"</tr>";
}
echo"</table>";
//echo"<br>";
//Definetheobject$graph =& newPHPlot();
//Definesomedata$example_data = array(
array('a',3),
array('b',5),
array('c',7),
array('d',8),
array('e',2),
array('f',6),
array('g',7)
);
$graph->SetDataValues($example_data);
//Drawit$graph->DrawGraph(); // remember, sinceinthisexamplewehaveonegraph, PHPlot
// doesthePrintImagepartforyoumysql_close($con);
?>
I have attached the screen-print of the output that I got form these codes.
BTW; these codes are written in a php file (say, process.php) and this php
file is used to show data from MySQL tables and is called from another php
file like this:
<form name = "name" action='process.php' method='POST'>.
Now, I wanted to show the query result in a table and also draw a graph with
the data which I retrieved from MySQL table. I wanted to use $row and $row as
the graph drawing array. Can you please show me how can I take these two
values into PHPlot's array format? And can you also inform me why I am not
able to draw the sample plot with the sample data and codes in my php file?
Please look at the output below which is obtained by using sample codes and
data. I am pasting the output as text. The garbage values after the table is
generated by including the sample codes from PHPlot.
-- output--
Query result for User: 3004 and Date: 2011-10-03::
UserID Date System time Height Duration Length Speed ANG X_start Y_start
That won't work, because of the way browsers displays HTML. Your script is
returning an HTML page with a PNG image tacked on the end, and this cannot be
properly displayed.
The image has to be provided by a separate browser request (see exception
below), and generated by a second PHP script. Your main script generates the
HTML page, with your table, and includes an IMG tag for the image, with the
URL of the second script. The second script uses PHPlot to generate the image.
This also means you either have to query the database twice, once in each
script, or pass the results of the first query to the second script (possibly
using session variables).
There is another way now. Since PHPlot-5.5.0, you can use a single script
which produces HTML with an embedded plot image. If you want to try this, you
should take a look at EncodeImage() in
the reference manual, which has details and an example.
By the way:
$graph =& new PHPlot();
That syntax, using &, is no longer valid and was removed the manual and
examples a long time ago. If you got that from some current web site or
documentation, it should be fixed.
(Maybe it would be easier to simplify this and do without the table for now -
just get the data from the database into a plot?)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tried the following coding as per your suggestion. I didn't use
EncodeImage(), but I followed one example which comes with PHPlot. Below is my
code. But, I am having another problem. Although, I am able to draw both the
graph and the data table (table isn't drawn by html anymore), both my graph
and data table are drawn on the same place; they are overlapped.
Since, my table has 10 columns and I wasn't able to show it on the right side
of the graph as this code in the example does:
In this example code, the position is (640, 20) which draws the table just on
the right side of the data table. But, if I maintain this position, my table
doesn't fit on the screen and it shows only part of the table. So, I had to do
like this:
Here, I have set the position to (10, 10). So the table is now shown fully,
but since they start from the left of the screen and the graph aslo start from
the left side, they are overlapped.
Can you please suggest me, how can I move the graph to the right side of the
screen. I found that this code,
$plot->SetPlotAreaPixels(NULL, NULL, 630, NULL);
is for setting the margin of the graph, not for the position of the graph.
Hence, 1) by which function I can define the starting position of the graph?
2) If the graph position isn't possible to change, then how can I fit the
table on the right side of my graph?
Here is the full code that I am using---
<?php>require_once'phplot.php';
require_once'data_table.php';
$var_1 = $_POST['userid'];
$var_2 = $_POST['day'];
$var = trim($var_1);
if($var =='-Select-' )
{
echo" User day wasn't selected";
exit;
}
$con = mysql_connect("localhost","root","root"); // (host, user,pwd)
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql", $con);
if ($_POST['day']=='-Select-'){
echo" User day wasn't selected; this is the result for user $var for all the dates:";
$result = mysql_query("SELECT * FROM tblGaitUserData WHERE UserID = $var");
}
else{
$result = mysql_query("SELECT * FROM tblGaitUserData WHERE UserID = $var and Date = STR_TO_DATE('$var_2','%Y-%m-%d')");
}
$numrows=mysql_num_rows($result);
if ($numrows == 0)
{
echo"<h4>Results</h4>";
echo"<p>Sorry, your search: "" . $var . "" and "" . $var_2 . "" returned zero results</p>";
exit;
}
$graphdata = array();
$graphdata_table = array();
while($row = mysql_fetch_array($result))
{
$graphdata [] = array($row['System_time'],$row['Height']);
$graphdata_table [] = array($row['UserID'],$row['Date'],$row['System_time'],$row['Height'],$row['Duration'],$row['Length'],$row['Speed'],$row['ANG'],$row['X_start'],$row['Y_start']);
}
// The$settingsarrayconfiguresthedatatable:$settings = array(
'headers' => array('UserID', 'Date', 'System_time', 'Height', 'Duration', 'Length', 'Speed', 'ANG', 'X_start', 'Y_start'),
'position' => array(10, 10),
'data' => $graphdata_table,
'font' => 3,
);
//Definetheobject$plot = newPHPlot(800, 600);
$plot->SetTitle('Line Plot with Data Table on Right Side');
$plot->SetDataValues($graphdata);
//$plot->SetDataType('data-data');
$plot->SetPlotType('linepoints');
$plot->SetPlotAreaPixels(NULL, NULL, 630, NULL);
$plot->SetCallback('draw_graph', 'draw_data_table', $settings);
$plot->DrawGraph();
mysql_close($con);
?>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
(I was writing this when your next message was posted, but here it is anyway.)
(For anyone else reading, the PHPlot distribution contains
contrib/data_table.php which draws a simple data table on an image. There are
also some examples in the same contrib directory. I forgot all about this in
my reply, and I am glad tariq82 found it on his own. Sorry about that.)
You are almost there. Use SetPlotAreaPixels() or SetMarginsPixels() to set the
limits of the plot area within the image. Both of these functions do the same
thing - they just differ in how they use their arguments.
If you want to use the bottom half of the image for the plot, leaving the top
half for something else (such as a data table), try
$plot->SetMarginsPixels(NULL, NULL, 300, NULL). This sets the top margin of
the plot area to 300 pixels (1/2 image height), so the plot will go in the
bottom half. This is exactly the same as doing $plot->SetPlotAreaPixels(NULL,
300, NULL, NULL). Also trailing NULL arguments can be omitted. Watch the
argument order for these 2 functions - I can never remember and have to look
them up. SetPlotAreaPixels(x1, y1, x2, y2) vs SetMarginsPixels(leftmargin,
rightmargin, topmargin, bottommargin).
Be aware when setting margins or plot area size manually that PHPlot puts
titles, axis and labels outside the plot area, so you may need to leave room
for them too.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
So, I have a plot area of 1100X600, which is pretty big and I can easily
accommodate both my plot and related data table. Now, for the data table, I
have assigned the position to (10, 40), so, my data table will start from this
position and hence, I have plenty of space left to draw the plot. Here is the
postion settings for my table:
After than I defined the plot position by this code:
$plot->SetPlotAreaPixels(650, NULL, NULL, NULL);
So, my plot will start from the position 650. So, to wrap up, I defined a plot
area of 1100X600, then I drew the table at the position (10, 40) and then I
began drawing the plot at 650. The table took a size of 570. So, it started at
10 and ended at 570 and the left of the area was for the plot and the plot was
nicely fitted there. So, both the table and plot were perfectly fitted in this
1100X600 plot area.
Thanks a lot lbayuk for helping me to do my task.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have drawn the plot; but the number of tick on the X-axis isn't correct. The
data in the line graph is correct and the tick labels are also correct. But,
the number of tick is alway 9. I have drawn 7 data on the plot, so the number
of ticks on the X-axis should also be 7, but it always 9. I have tried with 14
data, they are plotting correctly, but the tick number doesn't follow.
Can you please tell me how to show the correct number of ticks on the X-axis.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The X axis ticks are evenly spaced using the X tick increment along the axis.
This is independent of the X values of your data points. PHPlot by default
calculates the start and increment for tick marks based on the data range (but
it doesn't do a very good job yet).
It sounds like you want to get a tick mark at each of your data points. This
makes sense if you are using data labels (rather than tick labels), and your
data type is 'text-data'. Then, the X values assigned to your data are 0.5,
1.5, 2.5, etc. So to get the tick marks at those points, try this:
Hello,
I am a newbie in php. I was trying to draw a table with some data fetched from
MySQL table and then, draw a graph based on two columns' data of that table.
But, I could not draw the graph after the table; the table is drawn fine, but
NOT the graph. Here is the code, I am calling this file as process.php.
However, I have added the PHPlot codes just below the table codes.
I have attached the screen-print of the output that I got form these codes.
BTW; these codes are written in a php file (say, process.php) and this php
file is used to show data from MySQL tables and is called from another php
file like this:
Now, I wanted to show the query result in a table and also draw a graph with
the data which I retrieved from MySQL table. I wanted to use $row and $row as
the graph drawing array. Can you please show me how can I take these two
values into PHPlot's array format? And can you also inform me why I am not
able to draw the sample plot with the sample data and codes in my php file?
Please look at the output below which is obtained by using sample codes and
data. I am pasting the output as text. The garbage values after the table is
generated by including the sample codes from PHPlot.
-- output--
Query result for User: 3004 and Date: 2011-10-03::
UserID Date System time Height Duration Length Speed ANG X_start Y_start
3004 2011-10-03 16:43:26:079 63.28 3.81 86.46 22.7 2.73 24.57 95.53
3004 2011-10-03 16:47:49:829 64.02 4.14 78.11 18.87 -0.79 -55.55 159.92
3004 2011-10-03 18:28:12:370 64.64 4.41 91.7 20.8 -0.39 -60.17 128.98
3004 2011-10-03 20:31:20:561 63.31 5.2 111.35 21.39 2.73 33.45 90.9
3004 2011-10-03 20:34:01:741 64.29 3.2 79.76 24.89 -1.08 -33.92 122.04
3004 2011-10-03 21:40:56:635 62.92 4.67 76.2 16.31 0.14 -14.67 91.57
3004 2011-10-03 22:16:14:744 64.63 5.07 106.12 20.92 2.11 1.17 52.78
‰PNG IHDRX[ö PLTEÿÿÿ¾¾¾‡Îë Í3¡ ÒIDATxœíÏnã6Æ©:¹u¬ß¡‡rÜCÐ8Àö»@
§@_ 4èÓôX
¯ÒزdýræŽdîšßY["‡?Ƥ5’¨ªªªªªªªz½^€UssiV͇Kðúv_~ûã§ ÊxÝwX7ÇW)ê±^ËÄ KVÅBT±U,D QÅBT±U,D QÅBT±U,D QÅBT±µX·e&’î÷M‰i·Û2³÷û÷7»/¯—Ošž^µX/û}Þºm^>ˆÕªbIº,ò0⎵ù×Ê;Öö?+îXÏoV®Å (ëð-ÜdVqX›ÁßLyb=µÿQ¦™ƒ±½ÃA”gæ(G¬®g <3G9buc´ÇèㇵíÞ”…Õÿ ) ëÜ/xŒ>nXOý»’°ºÞ!”…uvVQXt~ë1ú´X»CÚ-k;xï‡ö™)ÝááŽVš,omGŸ(¨Uõ9ì¾ünMv~ }¤üìi‹uÓìs¼5è"¶$¬§ñGÊjåÑALœå1(z
MûÏB°¦É2°¶Ó£Ö,sTÖ¼S/ëi¶¤¬iï\Ål,Æ5%
1©Ò°f½ÃAdB*‹Í+“ e¨L,~‘ e¨<,š÷ÇÅ6–Á¨•…µùÊXÅóy™,¬Øù#Öæ|+ +v6̈µ}£îí"Þ2Ž>^ÞÚFN;±žœbë‘øå6¬ 9})Öjè¨áÆ„5Œˆ¬hë&¬Ñ蚃 !Öè듃¦›’ƒo<¾&ªñèš•ØU„Y ³ßÞX‰Þ }&?E2°mãX4þXÖô·wÅWÁXÓÁÕ~9lªsBÅÙ ÊéÏÐ+Õ4Š5ón‹õ
Hé¦vˆ5?2o±>ìx*¿kÿÄR¸¿Ì–œvâËzñprÜÃE&Ñ]»GC>¹Ÿ0,Æ”¹ƒpÄb~z›±Ò]–¸4†+ÝpzíXÜqŠKØM¤·Äæ|¬XBÏŒ>lQ+–Ю‹é‚
‹5v–‹Ïù±¤@Å;k),õ )hÄ’šUcE²F,i'i±ØÞ!˜±HX¯
}"Î2b‰±¢ÅlXâ>Rbñ½C°bÉßÒ˜‰ôÁŠ%7—
g]+^È„¥ˆœx‹gÅz‡
ÄRôJšÑ'Ö;#–¢ME‘„³lX$É$·
i:%q?ßÅ{‡ÃÒ xb™çxïlXšp±Îçw8Y°’O’°6o‰ïárXbü¹{K5 ‹…ž“k XªŸx"V2â- XºÃÊZoÀ’Ô•J¯o±vÀu§Ê_xéf%+}îTvS=Pzµ kdš•G¦B1V8L¼ œJà£.!*ûQ7•€>wJªR’·„P€s §Ú#À¼LÜAh—WÆÒæb,J¯†±{½ÖÅR§¶Ó%3(–:CT±’¥ÔJiëP¬dcú’ÎXÀÉ J”œbç/“-;cåžèDBeK27ЊXÈyÂB±Ra(ÚÁ°3ö+b!Wƒd]'a‘LÓ+ëª :3ž*,z‚.Y ºÐ(… EReK´¦--ú°ÀËIãmˆ,ðò§x$úb—ÖÅ‹ËÛ‡
‘Ц×JXè•Êq,ÙízÁ_¼¼+zÕæ7‡Ebݫ礌ÝxU<eK=V°àîrn>豤´Ûæ"XáF˜J
ûö:#@ìÊT!ozžJ È pܤR¡XšxPb™îf] Ëñ±€ä—Ž¥ÄÒ˜ÒÖÒØÒa'ž
T¶tXÆû·‰¦y>ß©KhYPvŠýñV£{ö6gLG/‚ó¦Ã{ö÷3o¹Dü(B‹§gaåñYXÆX”EÓËË)â‡,€ÿ£XN¡ååZC,Ò׊b6’kü“ÃòŠxg,¯ˆwÆòŠøA4 [âà¥Þ’–[h °
Ár -_,¿Ð:›" e¸$G¬gÄ„ G¬ô4u˜º0…¾E,–0©&?¬E¼}¹#±å€Ó©ó’–c·ÕcA_nÍÕny
‹JËcu<„Ô¹v,ì7É Xm¬Š…õ9׎…ýTZ«õÓw„ey„•Z-Auº”.þ+µÚ® :}îtž÷Ò¦ÿ£WŸiþ$L%
ý ¦sÏS Ü4ð#¬ô:: ü×bí^àGX¢
Äjµ$xàyÅX¡X,ª\7š/³·-ü:U°p•œJàêRºúGX¢¢½Õ„ã#¬Ø©.ò:a-™;µè[‹æN ºƒhU±$U,D QÅBT±U,D
QÅBT±U,D QÅBT±U,D¥cÍ®¤¼áË1ŸöñOMˆR57Ëo•U¨·Ó°–£×½5›†µ ,
\Å*«ªªê¬F.ÒJù%rúåõ
`ØÍ5¯öe·Lýgë…iŽñ:·#n˜ö ö*®‰‘~ÍÀb¼õ)gñ³ãÕ;‘iïv¯Ã*è
b(f+«ª®Gÿ—¨ŒÊN-×úIEND®B`‚I would really appreciate any help here.
That won't work, because of the way browsers displays HTML. Your script is
returning an HTML page with a PNG image tacked on the end, and this cannot be
properly displayed.
The image has to be provided by a separate browser request (see exception
below), and generated by a second PHP script. Your main script generates the
HTML page, with your table, and includes an IMG tag for the image, with the
URL of the second script. The second script uses PHPlot to generate the image.
This also means you either have to query the database twice, once in each
script, or pass the results of the first query to the second script (possibly
using session variables).
There is another way now. Since PHPlot-5.5.0, you can use a single script
which produces HTML with an embedded plot image. If you want to try this, you
should take a look at
EncodeImage() in
the reference manual, which has details and an example.
By the way:
That syntax, using &, is no longer valid and was removed the manual and
examples a long time ago. If you got that from some current web site or
documentation, it should be fixed.
(Maybe it would be easier to simplify this and do without the table for now -
just get the data from the database into a plot?)
Thanks for your reply!
I tried the following coding as per your suggestion. I didn't use
EncodeImage(), but I followed one example which comes with PHPlot. Below is my
code. But, I am having another problem. Although, I am able to draw both the
graph and the data table (table isn't drawn by html anymore), both my graph
and data table are drawn on the same place; they are overlapped.
Since, my table has 10 columns and I wasn't able to show it on the right side
of the graph as this code in the example does:
// The $settings array configures the data table:
In this example code, the position is (640, 20) which draws the table just on
the right side of the data table. But, if I maintain this position, my table
doesn't fit on the screen and it shows only part of the table. So, I had to do
like this:
Here, I have set the position to (10, 10). So the table is now shown fully,
but since they start from the left of the screen and the graph aslo start from
the left side, they are overlapped.
Can you please suggest me, how can I move the graph to the right side of the
screen. I found that this code,
is for setting the margin of the graph, not for the position of the graph.
Hence, 1) by which function I can define the starting position of the graph?
2) If the graph position isn't possible to change, then how can I fit the
table on the right side of my graph?
Here is the full code that I am using---
Never mind, I have solved it. Just played around the plot size ans
setplotareapixels function.
Thanks a lot for all the suggestions.
(I was writing this when your next message was posted, but here it is anyway.)
(For anyone else reading, the PHPlot distribution contains
contrib/data_table.php which draws a simple data table on an image. There are
also some examples in the same contrib directory. I forgot all about this in
my reply, and I am glad tariq82 found it on his own. Sorry about that.)
You are almost there. Use SetPlotAreaPixels() or SetMarginsPixels() to set the
limits of the plot area within the image. Both of these functions do the same
thing - they just differ in how they use their arguments.
If you want to use the bottom half of the image for the plot, leaving the top
half for something else (such as a data table), try
$plot->SetMarginsPixels(NULL, NULL, 300, NULL). This sets the top margin of
the plot area to 300 pixels (1/2 image height), so the plot will go in the
bottom half. This is exactly the same as doing $plot->SetPlotAreaPixels(NULL,
300, NULL, NULL). Also trailing NULL arguments can be omitted. Watch the
argument order for these 2 functions - I can never remember and have to look
them up. SetPlotAreaPixels(x1, y1, x2, y2) vs SetMarginsPixels(leftmargin,
rightmargin, topmargin, bottommargin).
Be aware when setting margins or plot area size manually that PHPlot puts
titles, axis and labels outside the plot area, so you may need to leave room
for them too.
Thanks for your detailed reply.
Here is what I did:
So, I have a plot area of 1100X600, which is pretty big and I can easily
accommodate both my plot and related data table. Now, for the data table, I
have assigned the position to (10, 40), so, my data table will start from this
position and hence, I have plenty of space left to draw the plot. Here is the
postion settings for my table:
After than I defined the plot position by this code:
So, my plot will start from the position 650. So, to wrap up, I defined a plot
area of 1100X600, then I drew the table at the position (10, 40) and then I
began drawing the plot at 650. The table took a size of 570. So, it started at
10 and ended at 570 and the left of the area was for the plot and the plot was
nicely fitted there. So, both the table and plot were perfectly fitted in this
1100X600 plot area.
Thanks a lot lbayuk for helping me to do my task.
Okay, I have another issue.
I have drawn the plot; but the number of tick on the X-axis isn't correct. The
data in the line graph is correct and the tick labels are also correct. But,
the number of tick is alway 9. I have drawn 7 data on the plot, so the number
of ticks on the X-axis should also be 7, but it always 9. I have tried with 14
data, they are plotting correctly, but the tick number doesn't follow.
Can you please tell me how to show the correct number of ticks on the X-axis.
The X axis ticks are evenly spaced using the X tick increment along the axis.
This is independent of the X values of your data points. PHPlot by default
calculates the start and increment for tick marks based on the data range (but
it doesn't do a very good job yet).
It sounds like you want to get a tick mark at each of your data points. This
makes sense if you are using data labels (rather than tick labels), and your
data type is 'text-data'. Then, the X values assigned to your data are 0.5,
1.5, 2.5, etc. So to get the tick marks at those points, try this:
Thanks, that works!