View unanswered posts | View active topics It is currently Sun Apr 28, 2024 7:27 pm



Reply to topic  [ 2 posts ] 
Problem with MySQL and CSV 
Author Message
New pChart user
New pChart user

Joined: Mon May 07, 2012 10:39 am
Posts: 1
 Problem with MySQL and CSV
Hi,

I'm having problems with my script so I need a little help...

I have two data sources, first is a MySQL DB with this colums:

HOUR DATE DATA
00:00 2012-05-07 5000
00:15 2012-05-07 6000
00:30 2012-05-07 3000
00:45 2012-05-07 7000
01:00 2012-05-07 3000


and a CSV file with this structure

HOUR MIN MAX
00:00 1000 10000
00:15 2000 7000
00:30 500 7000
00:45 100 5000
01:00 1000 4500

What is the script for?

The script gets the MySQL values for DATA and HOUR and print a line with the values for DATA, and an ABSCISA of HOUR, easy :)

Then, it imports a CSV file with HOUR, MIN and MAX values, like a threshold for the data each HOUR value, so this is perfectly working.

Both of MySQL and CSV data, has exactly 96 rows every day (96/4 = 24 hours a day)

I have an obiously problem, the CSV has static values that never changes, so it ALWAYS have 96 rows, but, sometimes, I cannot insert a value into MySQL so maybe that day will have 67 rows, when this happens, I have an abscisa with hours, and a line with data values for MySQL, but, a threshold line which does not correspond with the hour value, this is an example.


MYSQL
HOUR DATE DATA
00:00 2012-05-07 5000
00:15 2012-05-07 6000
01:00 2012-05-07 3000


CSV
HOUR MIN MAX
00:00 1000 10000
00:15 2000 7000
00:30 500 7000
00:45 100 5000
01:00 1000 4500

With this data, I will have a chart with messed values, because I have nothing for 00:30 and 00:45, but it will draw a threshold line with thresold values for incorrect times... I can't set VOID values to MySQL because it is an automated process...

I don't know if I explained very good, but... Basicaly what I need to do is this:

Compare both HOUR values, from MySQL and CSV, if this values are not the same, don't draw that CSV row and skip to next row. Any ideas? :(

This is my code

Code:
$hour=""; $date=""; $data="";
while($row = $db->fetch_array($myQuery)){
   $date[]   =   $row["date"];
   $hour[]   =   $row["hour"];
   $data[]   =   $row[$_data];
}
$db->close();
$MyData = new pData();
$MyData->addPoints($data,$_data);
$MyData->setSerieDescription("TRX ".$_data,$_data);
$MyData->setSerieOnAxis($_data,0);
$MyData->setPalette($_data, array("R"=>0,"G"=>0,"B"=>0));
$MyData->setSerieWeight($_data,1);

$MyData->importFromCSV("CSV/".$day."/".$_name."/".$_second."_".$_data.".csv",array("Delimiter"=>";","GotHeader"=>TRUE));

$MyData->setPalette("MIN", array("R"=>255,"G"=>0,"B"=>0));
$MyData->setSerieOnAxis("MIN",0);
$MyData->setSerieWeight("MIN",1);

$MyData->setPalette("MAX", array("R"=>255,"G"=>255,"B"=>0));
$MyData->setSerieOnAxis("MAX",0);
$MyData->setSerieWeight("MAX",1);

//print_r($MyData->getData());

$MyData->addPoints($hour,"HOUR");
$MyData->setAbscissa("HOUR");


Mon May 07, 2012 10:56 am
Profile
Experienced pChart user
Experienced pChart user

Joined: Tue Aug 02, 2011 11:49 pm
Posts: 34
Post Re: Problem with MySQL and CSV
also you asked weeks ago and I don't know whether your problem is solved or not:

you can't use the csv import function if your csv files are not identically in structure

you have to open and imort the files "manually" into arrays with php (3 arrays: point-of-time, min-value, max-value)

create an extra array to set the abscissa: $hrs with the 15-min-values for the abscissa, there will be actually 97 items because you need a zero at the beginning and a 24 at the end (later just duplicate the corresponding data value of 0:00 for 24:00)

then you have to manipulate your imported data to fit into these structure

set the VOID constant for all missing data to make sure you have always 97 min and 97 max values

if your csv shows the gaps like this
HOUR MIN MAX
00:00 500 800
00:15
00:30 750 900

it should be quite easily done to get valid series data like
$HOUR = array ('00:00', '00:15', '00:30', ...);
$MAX = array (800, VOID, 900, ...);
$MIN = array (500, VOID, 750, ...);

if not, you must check each data-triple against the abscissa array $hrs for matches at the single points of time, because your array length is different from 96

don't forget to copy the 00:00 data to be used also with 24:00


Fri Jun 01, 2012 10:53 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 39 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