JfreeChart中文乱码

本文介绍了一个用于封装JFreeChart的实用类MyChartFactory,该类提供了创建柱状图、时间序列图及饼状图的方法,并针对中文显示进行了字体设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**  

 * 封装JFreeChart的一些方法  

 *   

 * @author 黄磊  

 *   

 */  

public class MyChartFactory {   

  

    /*  

     * 报表字体  

     */  

    private static final Font CHART_FONT = new Font("黑体"1212);   

  

    /**  

     * 获得一个柱状图报表  

     *   

     * @param title  

     *            标题  

     * @param xSubject  

     *            X轴标题  

     * @param ySubject  

     *            Y轴标题  

     * @param dataset  

     *            数据源  

     * @return 报表  

     */  

    public static JFreeChart getBarChart(String title, String xSubject,   

            String ySubject, CategoryDataset dataset) {   

  

        JFreeChart chart = ChartFactory.createBarChart(title, xSubject,   

                ySubject, dataset, PlotOrientation.VERTICAL, truetruefalse);   

        chart.setBackgroundPaint(new Color(230,239,248));   

  

        CategoryPlot plot = (CategoryPlot) chart.getPlot();   

        plot.setBackgroundPaint(Color.lightGray);   

        //plot.setBackgroundPaint(new Color(230,239,248));   

        plot.setDomainGridlinePaint(Color.white);   

        plot.setDomainGridlinesVisible(true);   

        plot.setRangeGridlinePaint(Color.white);   

  

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();   

        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());   

           

        BarRenderer renderer = (BarRenderer) plot.getRenderer();   

        renderer.setDrawBarOutline(false);   

           

        CategoryAxis domainAxis = plot.getDomainAxis();   

        domainAxis.setCategoryLabelPositions(CategoryLabelPositions   

                .createUpRotationLabelPositions(Math.PI / 6.0));   

        plot.setNoDataMessage("没有数据显示");   

  

        // 设置字体,解决中文乱码问题   

        chart.getTitle().setFont(CHART_FONT);   

        chart.getLegend().setItemFont(CHART_FONT);   

        rangeAxis.setLabelFont(CHART_FONT);   

        domainAxis.setLabelFont(CHART_FONT);   

        return chart;   

    }   

  

    /**  

     * 获得一个时间轴报表图  

     *   

     * @param title  

     *            标题  

     * @param xSubject  

     *            X轴标题  

     * @param ySubject  

     *            Y轴标题  

     * @param dataset  

     *            数据源  

     * @return 报表  

     */  

    public static JFreeChart getTimeSeriesChart(String title, String xSubject,   

            String ySubject, XYDataset dataset) {   

  

        JFreeChart chart = ChartFactory.createTimeSeriesChart(title, xSubject,   

                ySubject, dataset, truetruefalse);   

  

        chart.setBackgroundPaint(new Color(230,239,248));   

  

        XYPlot plot = (XYPlot) chart.getPlot();   

        plot.setBackgroundPaint(Color.lightGray);   

        plot.setDomainGridlinePaint(Color.white);   

        plot.setRangeGridlinePaint(Color.white);           

        plot.setAxisOffset(new RectangleInsets(5.05.05.05.0));   

        plot.setDomainCrosshairVisible(true);   

        plot.setRangeCrosshairVisible(true);           

  

        XYItemRenderer r = plot.getRenderer();   

        if (r instanceof XYLineAndShapeRenderer) {   

            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;   

            renderer.setBaseShapesVisible(true);   

            renderer.setBaseShapesFilled(true);   

            renderer.setDrawSeriesLineAsPath(true);   

        }   

  

        DateAxis axis = (DateAxis) plot.getDomainAxis();   

        axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));   

        plot.setNoDataMessage("没有数据显示");   

  

        // 设置字体,解决中文乱码问题   

        chart.getTitle().setFont(CHART_FONT);   

        chart.getLegend().setItemFont(CHART_FONT);     

        plot.getRangeAxis().setLabelFont(CHART_FONT);   

        axis.setLabelFont(CHART_FONT);   

        return chart;   

    }   

  

    /**  

     * 获得一个饼状图报表  

     *   

     * @param title  

     *            标题  

     * @param dataset  

     *            数据源  

     * @return 报表  

     */  

    public static JFreeChart getPieChart(String title, PieDataset dataset) {   

        JFreeChart chart = ChartFactory.createPieChart(title, dataset, true,   

                truefalse);   

  

        PiePlot plot = (PiePlot) chart.getPlot();   

        plot.setSectionOutlinesVisible(false);   

        plot.setNoDataMessage("没有数据显示");   

  

        // 设置字体,解决中文乱码问题   

        chart.getTitle().setFont(CHART_FONT);   

        chart.getLegend().setItemFont(CHART_FONT);   

        plot.setLabelFont(CHART_FONT);   

        return chart;   

    }   

  

}  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值