jfreechart 1.0.13中文乱码问题的解决

来源:互联网 发布:thumbdata4 软件 编辑:程序博客网 时间:2024/05/29 17:10
将坐标图和饼状图中的中文乱码问题解决了,只要做如下配置:
/**
     * 配置字体 解决导出图中的中文乱码问题
     *
     * @param chart
     * JFreeChart 对象
     */
    public static void configFont(JFreeChart chart) {
        // 配置字体
        Font font = new Font("宋体", Font.BOLD, 25);

        chart.getTitle().setFont(font);
        if (chart.getLegend() != null) {
            chart.getLegend().setItemFont(font);
        }
        if (chart.getPlot() instanceof CategoryPlot) {
            chart.getCategoryPlot().getDomainAxis().setLabelFont(font);
            chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font);
            chart.getCategoryPlot().getRangeAxis().setLabelFont(font);
            chart.getCategoryPlot().getRangeAxis().setTickLabelFont(font);
        } else if (chart.getPlot() instanceof XYPlot) {
            if (chart.getXYPlot().getDomainAxis() != null) {
                chart.getXYPlot().getDomainAxis().setLabelFont(font);
                chart.getXYPlot().getDomainAxis().setTickLabelFont(font);
            }
            if (chart.getXYPlot().getRangeAxis() != null) {
                chart.getXYPlot().getRangeAxis().setLabelFont(font);
                chart.getXYPlot().getRangeAxis().setTickLabelFont(font);
            }
        } else if (chart.getPlot() instanceof PiePlot) {
            ((PiePlot3D) chart.getPlot()).setLabelFont(font);
        }
        // 设置背景色
        chart.setBackgroundPaint(Color.WHITE);
    }
原创粉丝点击