gpt4 book ai didi

java - 写J自由图表与中文标签到pdf

转载 作者:行者123 更新时间:2023-11-30 07:23:27 26 4
gpt4 key购买 nike

我正在尝试编写带有中文标签的JFree图表到PDF,我可以在没有标签的pdf中看到图表。但我需要带有标签的图表。

下面是代码片段。有人可以帮忙解决这个问题吗?

public static void main(String[] args) throws Exception {


writeChartToPDF(generatePieChart(), 500, 400, "2.pdf");


}

public JFreeChart generatePieChart() throws SQLException {
java.awt.Font font = new java.awt.Font("c:\\fonts\\SimHei Regular.TTF", java.awt.Font.HANGING_BASELINE,8);
JFreeChart localJFreeChart = ChartFactory.createRingChart("", dataSet, true, false, false);
RingPlot localRingPlot = (RingPlot)localJFreeChart.getPlot();
localRingPlot.setLabelFont(font);
localRingPlot.setNoDataMessage("No data available");
localRingPlot.setNoDataMessageFont(font);
localRingPlot.setNoDataMessagePaint(Color.darkGray);
localRingPlot.setSectionDepth(0.75D);
localRingPlot.setCircular(true);
localRingPlot.setLabelGap(0.02D);
localRingPlot.setSeparatorsVisible(true);
localRingPlot.setSeparatorPaint(Color.white);
localRingPlot.setShadowPaint(Color.white);
localRingPlot.setOutlineVisible(false);
localRingPlot.setBackgroundPaint(Color.white);
localRingPlot.setLabelLinkStyle(PieLabelLinkStyle.STANDARD);

localRingPlot.setLabelBackgroundPaint(Color.WHITE);
localRingPlot.setLabelOutlinePaint(Color.WHITE);
localRingPlot.setLabelShadowPaint(null);

colorRingPlot(localRingPlot, dataSet);
localJFreeChart.removeLegend();

localRingPlot.setBaseSectionOutlinePaint(Color.white);
localJFreeChart.setBackgroundPaint(Color.white);
localJFreeChart.setBorderVisible(false);
return localJFreeChart;
}

private static void colorRingPlot(RingPlot plot, DefaultPieDataset dataset) {

Double dataVal ;

for (int i = 0; i < dataset.getItemCount(); i++) {
dataVal= (Double) dataset.getValue(i);
if(dataVal >= 20){
plot.setSectionPaint(dataset.getKey(i), colorRange[0]);
}else if(dataVal >= 10 && dataVal < 20){
plot.setSectionPaint(dataset.getKey(i), colorRange[1]);
}else if((dataVal > 0) && (dataVal < 10)){
plot.setSectionPaint(dataset.getKey(i), colorRange[2]);
}
}
}


private static final Color[] colorRange = { new Color(111,191,103), new Color(169,217,164), new Color(210,235,205) };



private PieDataset createDataset() {
DefaultPieDataset result = new DefaultPieDataset();
result.setValue("短期资 (68.20%)", 0);
result.setValue("股 (1.87%)", 2);
result.setValue("同基金s (3.95%)", 3);
result.setValue("另投资 (8.19%))", 5);
result.setValue("债 (7.96%)", 1);
result.setValue("其投资 (2.81%))", 6);
result.setValue("组合资 (7.02%)", 4);
return result;
}




public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
PdfWriter writer = null;
BaseFont bfA = null ;

Document document = new Document();

try {
bfA = BaseFont.createFont("c:\\S24_Statements\\HK\\rundir\\fonts\\SimHei Regular.TTF", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
FontFactory.getFont("GulimChe", BaseFont.IDENTITY_H, true, Font.BOLD,
14);

DefaultFontMapper dfm=new DefaultFontMapper();
dfm.pdfToAwt(bfA, 8);
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height,
dfm);
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);

chart.draw(graphics2d, rectangle2d);

graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);

} catch (Exception e) {
e.printStackTrace();
}
document.close();
}

预期输出:带中文标签的图表

最佳答案

FontMapper arialuni = new FontMapper() {
public BaseFont awtToPdf(Font font) {
try {
return BaseFont.createFont(
"c:/windows/fonts/arialuni.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public Font pdfToAwt(BaseFont font, int size) {
return null;
}

};

此代码片段帮助了我,

引用:http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-14#524-text2topdf1.java

关于java - 写J自由图表与中文标签到pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37154482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com