获取使用iText和Xchart创建的甜甜圈图之间的线

问题描述 投票:0回答:1

我已经使用Xchart和iText在PDF中创建了甜甜圈图。从Acrobat Reader打开它时,我在甜甜圈区域之间得到了一些界线。下面是我使用的代码。请提出这些消息在dontu之间出现的原因可能是什么。

此外,当我使用其他工具打开相同的PDF时,也很好。

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;
    }

}
java itext xcharts
1个回答
0
投票

这里的问题与Graphics2D对象如何保存在PDF文档中有关。 Graphics2D对象作为绘图指令(矢量图像)保存在PdfGraphics2D对象中。这意味着当PDF阅读器打开文件时,它将这些指令转换为图像。

每个PDF查看器显示此内容的原因有所不同,是因为它们都有自己的实现方式来绘制这些图像,并且这些单独的PDF查看器如何呈现绘制指令之间似乎存在一些不一致之处。不幸的是,唯一的补救方法是创建一个实际的图像源,例如BufferedImage,其中包含完整的图像,而不是绘制指令。可以在ImageIO API的帮助下从Graphics2D对象获得BufferedImage。这是一个谈论此的SO帖子:Convert Graphics2D object to BufferedImage

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.