How to draw a pie chart using pdfbox?

前端 未结 2 1875
伪装坚强ぢ
伪装坚强ぢ 2021-01-23 05:57

I have to draw a pie-chart using pdfbox.

Let the data be:

Subject Mark in Percentage Mark in Degrees Cumulative Degrees
Sub-1 80 80 80
Sub-2 70 70 150
2条回答
  •  一整个雨季
    2021-01-23 06:54

    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.

    Arc2D.Float arc = new Arc2D.Float(x,y,w,h,start,extend, Arch2D.OPEN);
    AffineTransform tf = new AffineTransform();
    // You may need to setup tf to correctly position the drawing.
    float[] coords = new float[6];
    PathIterator pi = arc.getPathIterator(tf);
    while (!pi.isDone()) {
        int segment = pi.currentSegment(coords);
        switch (segment) {
        case PathIterator.SEG_MOVETO:
            if (isFinite(coords, 2))
                contentStream.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            if (isFinite(coords, 2))
                contentStream.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_QUADTO:
            if (isFinite(coords, 4))
                contentStream.curveTo1(coords[0], coords[1], coords[2], coors[3]);
                break;
        case PathIterator.SEG_CUBICTO:
            if (isFinite(coords, 6))
                contentStream.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;
        case PathIterator.SEG_CLOSE:
            contentStream.closePath();
            break;
        }
        pi.next();
    }
    contentStream.fill();
    

    See also my graphics adapter shape walking code.

提交回复
热议问题