simple Percentage for Pie Charts

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Peter Hanson

simple Percentage for Pie Charts

Post by Peter Hanson » Fri Oct 12, 2001 7:32 pm

Greetings,

I've had a lot of fun the last few days, learning the tool and messing around in the code.

I added a bit of code in PiePlot.java to add percentages to the series labels for each wedge.

At line 165 I inserted the following, which builds an array of
percentage values as ints (I wasn't concerned about the loss of precision...):

int[] percentVal = new int[seriesCount];
for ( int seriesIndex = 0; seriesIndex < seriesCount;
seriesIndex++ )
{
Number dataValue = data.getValue(seriesIndex, category);
double value = dataValue.doubleValue();
if (value <= 0)
continue;
// totalValue is established earlier in the code
double x = (value / totalValue) * 100;
int y = (int)x;
percentVal[seriesIndex] = y;
}

And then later on down at (now) line 246, I add the line
which draws the new percentage label:
g2.drawString(percentVal[seriesIndex]+ "%", (float)
labelLocationX , (float)labelLocationY + 10);

The +10 on the labelLocationY offsets the percentage label
from the existing series name label.

Cheers.
Peter

David Gilbert

RE: simple Percentage for Pie Charts

Post by David Gilbert » Fri Oct 12, 2001 8:01 pm

Peter,

Thanks for posting this - I'll try it out and add it to the next version of JFreeChart.

Regards,

DG.

Locked