我使用 Xchart 和 iText 在 PDF 中创建了圆环图。从 Acrobat 阅读器打开它时,我在甜甜圈区域之间出现了一些线条。下面是我使用的代码。请提出这些线路出现在 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;
}
}