php dynamic pie chart conception problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jahren
Forum Newbie
Posts: 8
Joined: Tue Dec 02, 2008 4:22 pm

php dynamic pie chart conception problem

Post by Jahren »

Hi guys,

i'm building a dynamic pic chart generator in php / ajax
i'm using GD for drawing arcs and printing values.

Problem is that I believe i'm bound to use GET parameter since the only way to re-draw an image is to change an img src=''

My image tag looks like this :
(fear not the %5B%5D, it means []. i'm using an array parameter)

Code: Select all

 
<img src='./pie_draw.php?c%5B%5D=000000&v%5B%5D=25&c%5B%5D=FFFFFF&v%5B%5D=75' alt='bla' />
 
in short :
c[]=000000
v[]=25
c[]=FFFFFF
v[]=75

c = color hex code
v = % value

Now in pie_draw.php
I parse the parameter and draw 25% of the pie chart in black and 75% in white.

I'm using AJAX to allow using buttons to increase or decrease one element's value and draw a new pie chart accordingly without reloading the page.

Thing is I might want to add labels and size parameters later on..
So a GET parameter won't be enough as i'll get above the 255 allowed chars
Do you see an other way I could re-draw a picture, using alot of params without reloading the page?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php dynamic pie chart conception problem

Post by McInfo »

A. You could send the arguments with AJAX and collect them in $_SESSION. The pie_draw.php script would look them up in the session. You wouldn't need the query string. AJAX can use POST, which supports a larger query string. If you need to use GET, you could send the arguments incrementally if your application design accommodates it.

Code: Select all

<img src="./pie_draw.php" alt="bla" />
B. You could compress the query string by using comma-separated lists instead of the array syntax. Split the lists apart using explode() in pie_draw.php.

Code: Select all

<img src="./pie_draw.php?c=000000,FFFFFF&v=25,75" alt="bla" />
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 4:29 pm, edited 1 time in total.
Jahren
Forum Newbie
Posts: 8
Joined: Tue Dec 02, 2008 4:22 pm

Re: php dynamic pie chart conception problem

Post by Jahren »

now that's an interesting idea.
I tried using SESSION vars

I have a problem tho
I have pie_chart class, pie_draw.php and pie_test.php

Calling $pie->draw() directly from pie_test.php works fine
but when I store $pie in $_SESSION['pie'] and try to $_SESSION['pie']->draw(), it won't work on the other side, telling me there are errors in the image. I feel like its a copy problem from my object to the session var...
Calling my static method drawDefault() works fine
Jahren
Forum Newbie
Posts: 8
Joined: Tue Dec 02, 2008 4:22 pm

Re: php dynamic pie chart conception problem

Post by Jahren »

here's the code

pie.class.php

Code: Select all

 
class pie_chart{
    private $image;
    function pie_chart(){
        $this->image = imagecreatetruecolor(300, 300);
    }
 
    public function draw(){ //FAIL
       $image = $this->image; //l'image     
    
    header('Content-type: image/png');
    imagepng($image);
    imagedestroy($image); //Netoyage
    }
public static function drawDefault(){ //WORKS
    $image = imagecreate(300, 300);
    $background_color = imagecolorallocate($image, 155, 155, 155);
    imagefill($image, 0, 0, $background_color);
        
    header('Content-type: image/png');
    imagepng($image);
    imagedestroy($image);
}
}
 
I have a pie_draw page

pie_draw.php

Code: Select all

 
require_once('pie.class.php');
 
    session_start();
    if(isset($_SESSION['pie'])){
        $_SESSION['pie']->draw(); //Is called
    }
    else{
        pie_chart::drawDefault(); //Exists but I haven't put it here
    }
 
and the test page

pie_test.php

Code: Select all

 
require_once('pie.class.php');
 
session_start();
$pie = new pie_chart();
$_SESSION['pie'] = $pie;
header('Content-Type:text/html; charset=UTF-8');
 
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
    <title>Pie chart</title>
</head>
<body style='margin:0; padding:0; text-align:center;'>
    <img src='./pie_draw.php' alt='damn..' />
</body>
</html>
 
the errors :
Warning: imagefill(): supplied argument is not a valid Image resource in C:Program FilesApache Software FoundationApache2.2htdocsplanspie_generatorpie.class.php on line 78

Warning: imagepng(): supplied argument is not a valid Image resource in C:Program FilesApache Software FoundationApache2.2htdocsplanspie_generatorpie.class.php on line 82

Warning: imagedestroy(): supplied argument is not a valid Image resource in C:Program FilesApache Software FoundationApache2.2htdocsplanspie_generatorpie.class.php on line 83
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php dynamic pie chart conception problem

Post by McInfo »

I was suggesting storing the settings in $_SESSION so that $_SESSION would act as a replacement for $_GET. You would create a new PHP script that would accept a POST or GET request from AJAX (I don't see any AJAX in your script.) and transfer the values into $_SESSION. Later, the pie chart script would look for the settings in $_SESSION instead of $_GET.

Storing the object in the session is a different concept. Usually, to store an object in a session you would serialize() it. However, you cannot serialize resources such as images.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 4:30 pm, edited 1 time in total.
Jahren
Forum Newbie
Posts: 8
Joined: Tue Dec 02, 2008 4:22 pm

Re: php dynamic pie chart conception problem

Post by Jahren »

I'm not using ajax yet. Ajax will be used to dynamically change the chart's values and update accordingly.

As for the serialize, I believe I should be able to build the image only after being unserialized.
I'll store all the data required to build the image and do it then.

isn't there ANY way to serialize an image or simply have it avaible on the second page? :P
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php dynamic pie chart conception problem

Post by McInfo »

If you don't want to save the configuration settings and use them to recreate the image each time you call the pie chart script, you can save the image to a file and recall the file whenever you need it. The file name could be an md5 hash of the serialized configuration settings. You might need to purge the saved image files frequently.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 4:31 pm, edited 1 time in total.
Jahren
Forum Newbie
Posts: 8
Joined: Tue Dec 02, 2008 4:22 pm

Re: php dynamic pie chart conception problem

Post by Jahren »

As the image's values update, I need to redraw the whole thing anyways. Like in DirectX.

I'm trying an other approach where I stored basic values only. Html Color Hexcode
When i'm on pie_draw.php, I build an image, build the colors ( as it required an image to even prepare the colors)
and then I show the image and cleanup.

i'll try this and be back soon
Jahren
Forum Newbie
Posts: 8
Joined: Tue Dec 02, 2008 4:22 pm

Re: php dynamic pie chart conception problem

Post by Jahren »

and it works perfectly :D
Man it's awesome. i've had some problems with 3D but it works too.
I ain't at home so I can't show this to you yet. i'll give a link soon enough.

New question :
How the hell am I supposed to position labels in this?
There is some hardcore maths required I believe :P

How can I position a label in the middle of a portion of the pie chart.

To write text, I need to supply x,y coordinates.
To draw the arc I supply various info :
start Angle, End Angle, CenterX and CenterY Coordinates, width and height.

Give me a hint please xD

ps : it gets worst, it aint a perfect circle, its an ellipse
Attachments
pie_draw.png
pie_draw.png (1.91 KiB) Viewed 229 times
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php dynamic pie chart conception problem

Post by McInfo »

I'm a little rusty with the trigonometry/calculus, but after finding the coordinates for the starting point of a label, this post might be helpful for positioning the label at the given point.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 4:31 pm, edited 1 time in total.
Jahren
Forum Newbie
Posts: 8
Joined: Tue Dec 02, 2008 4:22 pm

Re: php dynamic pie chart conception problem

Post by Jahren »

Thanks for the post, i'll read through

here's the math problem
c=180
b/c angle = 36deg


I need to find the coordinates oh the middle of H (center of the triangle)
Attachments
ellipse.JPG
ellipse.JPG (7.2 KiB) Viewed 204 times
Post Reply