Forum Moderators: coopster

Message Too Old, No Replies

GD line graph

using data from text file

         

Silent Miracle

7:10 am on May 12, 2011 (gmt 0)

10+ Year Member



hello .. i need a little help regarding GD and php .I want to read data from a text document and draw a line graph from this data.. My text document contains values like 1,2,3,4,5,6,7,8,9
I knw how to read a text document bt dnt knw to save this value into an array. please help!

Matthew1980

10:14 am on May 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Silent miracle,

>>My text document contains values like 1,2,3,4,5,6,7,8,9

Could you give a sample of format that this data is saved, then we can help show you how to load this data into an array.

Cheers,
MRb

Silent Miracle

8:25 am on May 13, 2011 (gmt 0)

10+ Year Member



Actually my file is in this format :

1->2,2->4,3->15,4->0,5->10,6->2

here 1,2,3,4,5,6 will be the points on x-axis
and 2,4,15,0,10 and 2 will be the points on y-axis.. i want to read these points separately in two arrays and then display the graph accordingly :( I have no idea how to do this..

rocknbil

5:35 pm on May 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Silent, don't re-invent the wheel, there are thousands of these you can learn from. I just sent you a PM of one of them.

Silent Miracle

2:51 pm on May 14, 2011 (gmt 0)

10+ Year Member



actually i want to read from a text document and then store it into arrray .. that i couldnt find on internet.. i mean i do searched on google bt couldnt find any helpful topic :(

Nutter

2:05 am on May 16, 2011 (gmt 0)

10+ Year Member



What about the Google Chart API?

astupidname

10:11 am on May 16, 2011 (gmt 0)

10+ Year Member



There's much info here: php filesystem functions [php.net] about opening and reading or creating files which you may want to wade through some time. Most useful in this situation being file() or file_get_contents(). And of course there are many ways you could go about filling arrays with the data. The below will get you started on putting the data into array(s):

$lines = file('coordinates.txt');

foreach ($lines as $idx => $line) {
$data = preg_match_all('/ *(\d+) *-> *(\d+) *\,?/', $line, $m);
$xArr = $m[1];
$yArr = $m[2];
//$xArr and $yArr are now filled with x & y coordinates
//do what you will with them...
//....
}


sample 'coordinates.txt' file with 3 datasets each on it's own line:
1 -> 2,2->4, 3->15,4->0,5->10,6->2
1->4,2->7,3->4,4->10,5->12,6->6,7->10,8->3
1->3,2->1,3->5,4->10,5->3,6->9


make sure that's 3 seperate lines there, and no blank lines.

And as rocknbil suggests, I would recommend using a pre-built graphing library for this. Just google "php graphing" and you will find many.