1

I have created donut chart in PDF using Xchart along with iText. While opening it from Acrobat reader, I am getting some lines between donut area. Below is the code that I have used. Please suggest what may be the reason for these lines coming between dontu.

Also, when I am opening the same PDF using some other tool, then it is coming fine.

import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;

import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.BitmapEncoder.BitmapFormat;
import org.knowm.xchart.PieChart;
import org.knowm.xchart.PieChartBuilder;
import org.knowm.xchart.PieSeries.PieSeriesRenderStyle;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.VectorGraphicsEncoder;
import org.knowm.xchart.VectorGraphicsEncoder.VectorGraphicsFormat;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.PieStyler.AnnotationType;
import org.knowm.xchart.style.Styler.LegendPosition;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class DonutChart implements ExampleChart<PieChart> {

    public static void main(String[] args) {

        ExampleChart<PieChart> exampleChart = new DonutChart();
        PieChart chart = exampleChart.getChart();
        new SwingWrapper<PieChart>(chart).displayChart();
    }

    /* (non-Javadoc)
     * @see org.knowm.xchart.demo.charts.ExampleChart#getChart()
     */
    @Override
    public PieChart getChart() {

        // Create Chart
        PieChart chart = new PieChartBuilder().width(600).height(400).title("Donut Chart").build();

        // Customize Chart
        chart.getStyler().setLegendVisible(true);
        chart.getStyler().setChartTitleBoxBorderColor(Color.black);
        chart.getStyler().setChartTitleBoxBackgroundColor(new Color(0, 222, 0));
        chart.getStyler().setChartTitleFont(new Font(Font.SANS_SERIF, Font.PLAIN, 18));
        chart.getStyler().setLegendBackgroundColor(new Color(240, 240,230));
        chart.getStyler().setLegendBorderColor(Color.CYAN);
        //chart.getStyler().setChartTitleVisible(false);
        chart.getStyler().setAnnotationType(AnnotationType.Label);
        chart.getStyler().setHasAnnotations(false);
        //chart.getStyler().setAnnotationDistance(.7);
        chart.getStyler().setPlotContentSize(.9);
        chart.getStyler().setDonutThickness(.45);
        chart.getStyler().setChartBackgroundColor(Color.WHITE);
        chart.getStyler().setInfoPanelBorderColor(Color.WHITE);
        //chart.getStyler().setLegendBorderColor(Color.WHITE);
        chart.getStyler().setPlotBorderColor(Color.WHITE);
        chart.getStyler().setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Donut);
        chart.getStyler().setBorderWidth(0);
        chart.getStyler().setLegendFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
        chart.getStyler().setLegendPosition(LegendPosition.OutsideE);

        Color[] sliceColors = new Color[] { new Color(31, 43, 135), new Color(9, 179, 162), new Color(0, 0, 0),
                new Color(133, 129, 129), new Color(184, 180, 180),new Color(207, 207, 207) };

        chart.getStyler().setSeriesColors(sliceColors);

        // Series
        chart.addSeries("Office", 50);
        chart.addSeries("Multifamily", 10);
        chart.addSeries("Mixed use", 34);
        chart.addSeries("Hospitality", 22);
        chart.addSeries("Retail", 29);
        chart.addSeries("Industrial", 40);

        try {

            BitmapEncoder.saveBitmapWithDPI(chart, "D:\\Akash\\Workspace_OMS8.6main\\CHARTTEST\\Donut_Chart_300_DPI", BitmapFormat.PNG, 300);
            VectorGraphicsEncoder.saveVectorGraphic(chart, "./Donut_Chart", VectorGraphicsFormat.PDF);

            Document document = new Document(PageSize.A4);
            PdfWriter.getInstance(document, new FileOutputStream("D:\\Akash\\Workspace_OMS8.6main\\CHARTTEST\\Donut_Chart_300_DPI.pdf"));
            document.open();
            Image img = Image.getInstance("D:\\Akash\\Workspace_OMS8.6main\\CHARTTEST\\Donut_Chart_300_DPI.png");
            img.setAbsolutePosition(10,400);
            img.scaleAbsolute(300f, 250f);
            document.add(img);
            document.close();
            System.out.println("Done");

        } catch (IOException e) {
        } catch (DocumentException e1) {
        }


        return chart;
    }

}
Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48

1 Answers1

0

The issue here is related to how Graphics2D objects are saved in the PDF document. Graphics2D Objects are saved in PdfGraphics2D objects as drawing instructions (vector image). This means when a PDF reader opens the file, it translates these instructions to an image.

The reason that each PDF Viewer displays this a little differently is because they all have their own implementation of how to draw these images and it seems that there are some inconsistencies between how these individual PDF viewers render the drawing instructions. Unfortunately the only way to remedy this would be to create an actual image source such as BufferedImage that contains a fully fledged image, not drawing instructions. A BufferedImage can be gotten from a Graphics2D object with help of the ImageIO API. Here's an SO post that talks about this: Convert Graphics2D object to BufferedImage.

Hope this helps!

PeppaPig
  • 48
  • 6