I have to draw a pie-chart using pdfbox.
Let the data be:
Subject Mark in Percentage Mark in Degrees Cumulative DegreesSub-1 80 80 80
Sub-2 70 70 150
Sub-3 65 65 215
Sub-4 90 90 305
Sub-5 55 55 360
Let the radius and centre be 100 pixels and ( 250, 400).
Let us take initial line parallel to x axis.
Drawing initial line statement will be:
contentStream.drawLine(250, 400, 350, 400);
I stuck up with:
a) finding x, y co-ordinates of point on the circle that is some degrees away from the initial line to draw radius
b) drawing arc between the two points using Bezier curve.
Any help on resolving the issues would be highly appreciated!
If you only want to draw some charts into a PDF using PDFBox and don't want to do all the math etc by yourself you can just use my PDFBox Graphics2D adapter. This allows you to use any Graphics2D based java library to draw your chart (e.g. JFreeChart). It will create a XForm which you can then place freely in your PDF.
If you really want to do this yourself you can still use the Java2D API to help with the shapes.
See also my graphics adapter shape walking code.
Finding the x, y coordinates on the circle depending on an angle is school math, i.e. sin() and cos(), the tricky part is to draw an arc with Bézier curves.
Here's some code that draws the pie chart you asked for. Note that
createSmallArc()
can only work with angles up to 90°. If you want more, you'd either have to modify the code by drawing several arcs until you go back to (0,0), or just draw several slices.(
createSmallArc()
is by Hans Muller, license: Creative Commons Attribution 3.0. Changes made: implemented original AS code into java. Algorithm is by Aleksas Riškus)