View unanswered posts | View active topics It is currently Sat Apr 27, 2024 9:04 pm



Reply to topic  [ 8 posts ] 
missing values from csv - can't VOID 
Author Message
pChart user
pChart user

Joined: Wed Oct 12, 2011 8:50 pm
Posts: 6
Post missing values from csv - can't VOID
Hi,

I'm brand new to pchart, and it's taken me a whole week to figure out how to make it work. I'm very pleased with myself for doing that, and thanks to all of you who don't know how much you help people who are just Googling for answers.

I have - after much hard work - created a script on my Linux PC that takes a page from Amazon, strips out a book's sales data, uploads it to my web server where pchart turns a csv file into a graph of sales data over time.

It took me a long time to work out how to get csv import to work. Personally I can't see a single good reason to hard code chart data into the source code, why wouldn't you just always use an external csv file? Much easier to edit values.

So here's my question.

When Amazon's sales rank for a book goes above 100, it no longer displays a value and my csv file ends up with an empty "cell".

I can't therefore see how to break the chart line, because pchart doesn't actually seem to understand a blank value, it needs the value to be "VOID", which means that you have to hard code an empty value with the word "VOID" in order to tell pchart that it's empty. It doesn't seem like a very nice solution.

Why can't pchart just treat an empty value as an empty value?

By having an empty value default to 0, it makes my charts look as if a book has hit the top of the charts when actually it's falling.

Is there any way that I can have pchart treat an empty value in a csv file as EITHER a void, without me having to manually write the word VOID into the csv, OR to treat the empty value as "100" instead of "0"?

I have seen a post on here that was supposed to do this, adding in a piece of code to line 137 of the data class, but it did not work for me.

Thanks in advance,

Peter


Wed Oct 12, 2011 9:02 pm
Profile
pChart Guru
pChart Guru

Joined: Sun Jun 19, 2011 6:36 pm
Posts: 52
Post Re: missing values from csv - can't VOID
Do you fetch the data from CSV by while loop? (I never worked with them)
If yes, just use
Code:
if ($x == NULL ) {$x = 100;}

If not, show me your code.

_________________
Hope I helped :)
Image
If I helped, say thanks, so that I can see that my work isn't useless.


Thu Oct 13, 2011 12:13 am
Profile
pChart user
pChart user

Joined: Wed Oct 12, 2011 8:50 pm
Posts: 6
Post Re: missing values from csv - can't VOID
Hello, thanks for your help.

I don't use a loop, within the pchart code I just do this:

$dataa->importFromCSV("thepitchingbible.csv",array("GotHeader"=>TRUE,"SkipColumns"=>array(1,2)));

Then I just change a few formatting details and create the linechart.

So let's say that one of the csv header columns is "Rank", then that would make the variable $Rank, I think. I use 3 columns in total, and I can understand the for/while example, but I don't know how to make it work for an array.

Maybe I need to process the csv file before sending it to pchart, I don't know the php code yet but something like this:

while lines in file.csv
if ($x == NULL ) {$x = 100;}
write back to file.csv
then pass file.csv to the pchart script

What do you think?

thanks again,

Peter


Thu Oct 13, 2011 3:05 am
Profile
pChart user
pChart user

Joined: Wed Oct 12, 2011 8:50 pm
Posts: 6
Post Re: missing values from csv - can't VOID
I just thought of something else, if the sales rank value that becomes null is the last one, there is nothing there for php to search for, so maybe I can't pre-process the csv file.

The 'normal' csv line looks like this:

12-10-2011,13:00,15945,18,92,

This is no problem, 5 columns, 5 headings and pchart is happy.

The problem I have is when the line in the csv file looks like this:

12-10-2011,14:00,17195,19,

The 5th column is missing, but the bash script that strips the rank data out of the html file can't read something that isn't there. Amazon doesn't put any kind of placeholder, the rank information is just completely missing, so there's nothing to search and replace with a comma that I could then read as a null or VOID.

So it looks like I somehow have to do the replacement AFTER pchart knows the file is a csv, because then it has read a null value into the array.

So maybe there is something I can do in the pchart class, that says "while I am reading the csv file into the array, if I find a null value, I replace it with 100".

??

Peter


Thu Oct 13, 2011 3:13 am
Profile
pChart user
pChart user

Joined: Wed Oct 12, 2011 8:50 pm
Posts: 6
Post Re: missing values from csv - can't VOID
I'm back again.

So you got me thinking about how the pchart class turns a csv into chart data. These are lines 679 to 682 from pData.class.php:

$Buffer = fgets($Handle, 4096);

$Buffer = str_replace(chr(10),"",$Buffer);

$Buffer = str_replace(chr(13),"",$Buffer);

$Values = preg_split("/".$Delimiter."/",$Buffer);

So it reads file.csv into $Buffer, takes out the CR and LF and then splits the file at the comma delimiter.
Then, at line 693, pchart reads $Values into addpoints:

if ($SerieNames == "" ) { foreach($Values as $Key => $Name) { if ( !in_array($Key,$SkipColumns) ) { $SerieNames[$Key] = $DefaultSerieName.$Key; } } }

foreach($Values as $Key => $Value) { if ( !in_array($Key,$SkipColumns) ) { $this->addPoints($Value,$SerieNames[$Key]); } }

So I am guessing that this is the moment that I could check $Value, and if it's empty, replace it with 100.

But I don't know how to write the code or where to put it!!!

Peter


Thu Oct 13, 2011 3:22 am
Profile
pChart user
pChart user

Joined: Wed Oct 12, 2011 8:50 pm
Posts: 6
Post Re: missing values from csv - can't VOID
I've been at this all day...

I have now solved the problem of the null entry in the csv by finding a better way to strip out the data I need, so now the script that builds the CSV can put the word VOID instead of an empty value.

But pchart isn't breaking the line at the VOID!! It's still treating VOID as zero.

(Sound of hair being pulled out)


Thu Oct 13, 2011 8:31 pm
Profile
pChart user
pChart user

Joined: Wed Oct 12, 2011 8:50 pm
Posts: 6
Post Re: missing values from csv - can't VOID
OK. I think I am finally there.

Because Amazon doesn't provide any kind of placeholder such as a <div>, the rank data information is just missing, the best I could come up with is to put an extra comma at the end of the file and then if there are two commas together ,, (indicating a missing value) then replace them with ,VOID,

However, it's not enough to have the word VOID in the csv, so I had to add this to the pData.class.php file

682 $Buffer = str_replace ("VOID",VOID,$Buffer);


I think that this would also cope with the situation where there was a missing value in the middle of the csv data.

It's not 100% neat and tidy, because the script is now putting VOID on the end of every line, but at least I can tell pchart to ignore any extra data columns.


Thu Oct 13, 2011 9:59 pm
Profile
New pChart user
New pChart user

Joined: Sun Aug 11, 2013 10:10 am
Posts: 1
Post Re: missing values from csv - can't VOID
If anyone else browses this old thread, I had a similar experience; had data with empty values which I needed treated as VOID points.

I added an option to importFromCSV so empty values "" are added as missing (VOID) points, its used like this:
$data->importFromCSV("file.csv",array("GotHeader"=>TRUE,"TreatEmptyAsMissing"=>TRUE);

In pData.class.php, inside function importFromCSV, here is what I changed:

add this right after function importFromCSV declaration:
Code:
$TreatEmptyAsMissing = isset($Options["TreatEmptyAsMissing"]) ? $Options["TreatEmptyAsMissing"] : FALSE;

near the end, change this line which adds the points:
Code:
foreach($Values as $Key => $Value) {  if ( !in_array($Key,$SkipColumns) ) { $this->addPoints($Value,$SerieNames[$Key]); } }

to
Code:
foreach($Values as $Key => $Value) {  if ( !in_array($Key,$SkipColumns) ) { if ($TreatEmptyAsMissing && $Value=="") $Value=VOID; $this->addPoints($Value,$SerieNames[$Key]); } }


Sun Aug 11, 2013 10:35 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

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