# Thread: Writing text in a pie chart

1. ## 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. 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. 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.

4. 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.

#### Posting Permissions

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