SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    India
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Writing text in a pie chart

    Hi all,
    I am trying to do a pie chart which works well as far as sectors are concerned.


    Code:
    imagefilledarc($img,$x1, $y1,$diameter, $diameter,$from, $to,$color, IMG_ARC_PIE);
    The above code draws an arc centered at x1,y1 from the degree $from to $to. I use this in a loop to accomplish a pie. My problem is that, I have to place a text inside each sector and write details about the sector.

    How do I calculate the point from where I should start writing? I first thought that I'll find out the midpoint of each sector and start writing from there by solving simulataneous equations.

    1) distance between two points (x1,y1) and (x2,y2)
    2) m=tan(angle)
    3)y2=m(x2-x1) + y1

    where x1,y1 and x2,y2 are two end points and m is the slope. This doesn't work always. Which is the ideal way to draw a string inside a pie chart? left-right or up-down?

    In all possible tutorials I saw, everyone has written the text as a legend.

    Thanks in advance for any help.
    Happy Xmas.
    Celia

  2. #2
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Melbourne
    Posts
    362
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason for writing a legend is not just because it's easier, it's because it is so much clearer and readable. Unless there's a convincing argument, strongly consider doing it this way.

    If this doesn't work, you should be able to figure out the point of intersection between the radius of the pie segment and the arc by some simple geometry (it's the tangent line of a right angle triangle). You can then draw bump a few pixels either side of that point to get the text 'in' the pie segment... though if it's a really small segment, good luck.

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to take a look at JpGraph, if it does not do what you are looking for outright, perhaps you could look at the code to give yourself some ideas.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by celia
    Hi all,
    I am trying to do a pie chart which works well as far as sectors are concerned.


    Code:
    imagefilledarc($img,$x1, $y1,$diameter, $diameter,$from, $to,$color, IMG_ARC_PIE);
    The above code draws an arc centered at x1,y1 from the degree $from to $to. I use this in a loop to accomplish a pie. My problem is that, I have to place a text inside each sector and write details about the sector.

    How do I calculate the point from where I should start writing? I first thought that I'll find out the midpoint of each sector and start writing from there by solving simulataneous equations.

    1) distance between two points (x1,y1) and (x2,y2)
    2) m=tan(angle)
    3)y2=m(x2-x1) + y1

    where x1,y1 and x2,y2 are two end points and m is the slope. This doesn't work always. Which is the ideal way to draw a string inside a pie chart? left-right or up-down?

    In all possible tutorials I saw, everyone has written the text as a legend.

    Thanks in advance for any help.
    Happy Xmas.
    Celia
    I've been tinkering with pie charts recently too, but opted for a legend approach, partially because using 3d pie charts.

    I did think about labeling them, but not on the actual pie thou, think of an outer circle centered on the pie, draw a line from the middle a sector to that outer circle, dog leg it to horizontal, and stick the label on the end of that. So it looks kinda like a spider.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •