Forum Moderators: coopster

Message Too Old, No Replies

array issue with pchart

pchart array shows 0 always to start X values

         

hozyali

8:27 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Hello,

I am using pChart open source libraries to create graphs on my site.

This would be a common problem but I am unable to fix this.

I have a mysql table in which daily sales being recorded. I am getting SUM of amount in each day of a given month.

The FOR loop works from 1 to 31 days
Then I have an array_search function which replaces the day with amount of that day’s sale.

Then I am add points and serie information for pChart to generate the graph. But the problem is, pChart always starts its array from 0 which changes the sale values, so if I have a sale of $500 on 5th March, the graph sets it on 4th March.

Any help related to this will highly be appreciated.

I can send the exact code I am using if needed.

thanks

eelixduppy

8:59 pm on Apr 28, 2010 (gmt 0)



So what's preventing you from starting your days at 0 instead of 1? You can change your indexing to match pChart's indexing.

hozyali

9:01 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



thanks for your reply, but thats what I don't know how to do.

can you help further with that?

eelixduppy

9:05 pm on Apr 28, 2010 (gmt 0)



Can you post the relevant code?

As a simple solution, you could always take your array of data and make a new array with the correct indexes:

// given $array $new_array = array_values($array);

hozyali

9:26 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



hi,

thanks for helping, please review the below code which takes data from the database and sends to pChart to generate graph.


$DataSet = new pData;
$currday = 0;
$currmonth = '03'; //March
$days = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);

for ($currday=1; $currday<=31; $currday+=1) {
$MySql = "SELECT SUM(amount) as DaysTotal,date_time,date_format(`date_time`,'%D') as DateOnly FROM TBLTRANSACTION where month(date_time)= '$currmonth' and DAYOFMONTH(date_time)='$currday' AND order_payment_status='Paid' ORDER BY date_time ASC ";
$MySales = mysql_query($MySql) or die(mysql_error());

$TotalDaySale = mysql_fetch_assoc($MySales);
echo $TotalDaySale['DateOnly'].' = '.$TotalDaySale['DaysTotal'].'<br>';

$green_key = array_search($currday, $days);
$days[$green_key] = $TotalDaySale['DaysTotal'];

}
print_r($days);



$DataSet->AddPoint($days,"Serie1");

$DataSet->AddAllSeries();
$DataSet->SetAbsciseLabelSerie();
$DataSet->SetSerieName("March","Serie1");



The above code is not fully completed in regards to create the graph, the remaining code is just fixed code provided by pChart to create the graph.

I have also uploaded the screenshot so you can see what actually is happening. please follow this link
[docs.google.com...]

thanks again for your help.

eelixduppy

9:30 pm on Apr 28, 2010 (gmt 0)



Not sure that array_search in there is necessary since you should be able to grab what day it is from the database, however, changing the following line should work, no?


$days[$green_key-1] = $TotalDaySale['DaysTotal'];

hozyali

9:39 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



thanks, I tried that, but when I set it to -1, it sets one more day back in the graph. like the 6th march value goes on 4th.

All I need is to set the array to start indexing from 1 instead of 0. so it will automatically fix things in graph.

thanks again for your help.

eelixduppy

9:43 pm on Apr 28, 2010 (gmt 0)




$days = array(1 => 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);

hozyali

9:53 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



thanks, it works perfect for the array when its in my own code of php.

when I supply it in pchart, it starts with zero again. thats the main problem. I tried contacting someone on pchart support too, but no luck :(

I am sure you are an expert in php, would you mind taking a look at pchart's library of bar graph and advise how should I actually supply data to its graph?

you can get its files and documentation at [pchart.sourceforge.net...]

thanks again

eelixduppy

1:03 pm on Apr 29, 2010 (gmt 0)



You might need to play around with the following:

void setFixedScale($VMin,$VMax,$Divisions=5,$VXMin=0,$VXMin=0,$XDivisions=5)


So something like:

$pChartInstance->setFixedScale(1,31);

hozyali

4:18 pm on Apr 29, 2010 (gmt 0)

10+ Year Member



Thanks very much. I have been bugging with this and seem not to be able to fix this for the site.

I just need an advise from you. I am planning to use XML/SWF charts which I have used earlier and its very easy. only thing to ask is Are we able to remove the context menu which shows their name if I have purchased the license.

I tried to contact them directly but they never got back.

thanks

eelixduppy

4:01 pm on Apr 30, 2010 (gmt 0)



That is something you should ask them as I have never used this service before. Perhaps someone else here can shed some light on this?

I'm assuming the sexFixedScale didn't work, though? I'm sure if you stick with it a little longer you'll be able to get it working.

hozyali

5:39 pm on Apr 30, 2010 (gmt 0)

10+ Year Member



Yes, thanks. I am still trying to get it working.

I was able to get it start with 1 anyway. but the other problem I am having is, it also uses the X values to make the pointers.

For example, I have 1 to 31 days which should only appear as values of X axis, but it also show bars of those values, so the 25th day shows a bar till 25, and thats wrong.

thanks

Matthew1980

5:54 pm on Apr 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Eelixduppy,


I'm assuming the sexFixedScale didn't work,


I'll assume as you mean 'setFixedScale' LOl !

Yes, stick with it though as the answer might be just around the corner.

Cheers,
MRb

hozyali

8:38 pm on Apr 30, 2010 (gmt 0)

10+ Year Member



Hello Eelixduppy,

thanks a lot for your help. I really appreciate. I was able to sort out my issue.

Actually I was making a mistake. I was supplying 1 to 31 as a serie and that was being used in the bars. so I set them all to 0 when there is no sale. and it worked perfect then.

Thanks again for your time and help.

Regards