I've been trying to read the data in this code from an external .csv file but
I need help with what the external data file should look like plus how do I
read it in so it works with this code.
Thanks Gary
SetPlotAreaWorld(null, 29.00, null, 30.00);
$plot->SetDataValues($example_data);
//Turn off X axis ticks and labels because they get in the way:
$plot->SetXTickLabelPos('none');
$plot->SetXTickPos('none');
//Draw it
$plot->DrawGraph();
?>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You can put your data into a file that looks like this. I called it
"data.csv":
12am,29.551am,29.582am,29.66...
Then you read it from your script, by replacing the assignment to
$example_data with something like this:
//Read data from file:$f=fopen('data.csv','r');if(!$f)exit(1);// Need error handling here$example_data=array();while(($row=fgetcsv($f))!==FALSE)$example_data[]=$row;fclose($f);
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Be careful of race conditions. If you are updating the file at the exact time
the script is also reading it, you might get an error opening the file, or end
up reading corrupt data.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is putting a newline between each record, but not after the last record.
You need another newline at the end of the file (before you append your data).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, assuming you left the other line in that appends a newline also. But you
should be able to do a lot better. Why not append your new data to the
$contents array at the top of your code, then skip the whole re-open in append
mode and write?
You would be writing it out with the new data at the top, like this:
I've been trying to read the data in this code from an external .csv file but
I need help with what the external data file should look like plus how do I
read it in so it works with this code.
Thanks Gary
SetPlotAreaWorld(null, 29.00, null, 30.00); $plot->SetDataValues($example_data); //Turn off X axis ticks and labels because they get in the way: $plot->SetXTickLabelPos('none'); $plot->SetXTickPos('none'); //Draw it $plot->DrawGraph(); ?>You can put your data into a file that looks like this. I called it
"data.csv":
Then you read it from your script, by replacing the assignment to
$example_data with something like this:
Wow thanks so much - it works perfect.
Now on to updating the data file once an hour.
Be careful of race conditions. If you are updating the file at the exact time
the script is also reading it, you might get an error opening the file, or end
up reading corrupt data.
I haven't got to the race conditions yet but I will when I get everything else
working.
I'm now stuck trying to add a newline when I append hourly data to data.csv.
When I add data to data.csv it adds it like so to the bottom of the file
without a newline.
4pm,29.66
5pm,29.78
6pm,29.85
7pm,29.60
8pm,29.45
9pm,29.55
10pm,29.58
11pm,29.663pm,29.21\n5pm,29.215pm,29.215pm,29.215pm,29.215pm,29.21
Any help here would be appreciated - Thanks Gary
pressure_in; // combine current hour and pressure $new_time_pressure = $time.$pressure; // append a newline $new_time_pressure .= "\n"; // append to data file $f = fopen($file,'a'); fwrite($f,$new_time_pressure); fclose($f); ?>file_put_contents($file, implode("\r\n", $contents));
This is putting a newline between each record, but not after the last record.
You need another newline at the end of the file (before you append your data).
Would this do it?
$new_time_pressure = "\n" . $new_time_pressure;
Yes, assuming you left the other line in that appends a newline also. But you
should be able to do a lot better. Why not append your new data to the
$contents array at the top of your code, then skip the whole re-open in append
mode and write?
You would be writing it out with the new data at the top, like this:
OK I'll try that. I just tried this (like you said) and it is looking better
now.
// append a newline
$new_time_pressure = "\n" . $new_time_pressure ."\n";
OK I used your suggestion and it looks good.
This; file_put_contents($file, implode("\r\n", $contents) . "\r\n");
I made it a cron job set to run every 12 mins after the hour so we'll see.
I want to work on the graph but here is where I'm headed:
http://www.mnlakecams.com/simpleplot.php
Thanks much Gary