写J免费图表与中文标签到pdf



我正在尝试写一个带有中文标签的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

相关内容

  • 没有找到相关文章

最新更新