当前位置:编程学习 > JAVA >>

JFreeChart生成图表中文乱码问题解决方案

在使用JFreeChart生成图表时,可能会出现中文乱码,下面给出两种解决方法。
1、设置主题样式(推荐方法)
主题样式是在建立图表之前进行设置,JFreeChart图表一般包括3个部分:Title(图表标题)、Plot(图表主体)、Legend(图表图例),主题是统一对这三个部分进行字体设置
[java]  
//创建主题样式  
StandardChartTheme mChartTheme = new StandardChartTheme("CN");  
//设置标题字体  
mChartTheme.setExtraLargeFont(new Font("黑体", Font.BOLD, 20));  
//设置轴向字体  
mChartTheme.setLargeFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
//设置图例字体  
mChartTheme.setRegularFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
//应用主题样式  
ChartFactory.setChartTheme(mChartTheme);  
 
实例:
[java] 
import java.awt.Font;  
import org.jfree.chart.ChartFactory;  
import org.jfree.chart.ChartFrame;  
import org.jfree.chart.JFreeChart;  
import org.jfree.chart.StandardChartTheme;  
import org.jfree.chart.plot.PlotOrientation;  
import org.jfree.data.category.CategoryDataset;  
import org.jfree.data.category.DefaultCategoryDataset;  
  
public class Bar {  
    public static void main(String[] args) {  
        CategoryDataset mDataset = GetDataset();          
          
        //创建主题样式  
        StandardChartTheme mChartTheme = new StandardChartTheme("CN");  
        //设置标题字体  
        mChartTheme.setExtraLargeFont(new Font("黑体", Font.BOLD, 20));  
        //设置轴向字体  
        mChartTheme.setLargeFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
        //设置图例字体  
        mChartTheme.setRegularFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
        //应用主题样式  
        ChartFactory.setChartTheme(mChartTheme);  
        ///////////////以上主题设置必须位于创建图表函数之前才能生效////////////  
        JFreeChart mBarChart = ChartFactory.createBarChart3D(  
                "学校人员分布图",  //图表标题  
                "类型",       //横轴(目录轴)标签  
                "数量",           //纵轴(数值轴)标签  
                mDataset,       //数据集  
                PlotOrientation.VERTICAL,   //图表方向  
                true,           //是否显示图例  
                true,           //是否生成提示工具  
                false);         //是否生成url连接  
        ChartFrame mChartFrame = new ChartFrame("学校人员分布图", mBarChart);  
        mChartFrame.pack();  
        mChartFrame.setVisible(true);  
    }  
    public static CategoryDataset GetDataset()  
    {  
        DefaultCategoryDataset mDataset = new DefaultCategoryDataset();       
        mDataset.addValue(2000, "清华大学", "本科生");  
        mDataset.addValue(1500, "清华大学", "研究生");  
        mDataset.addValue(1000, "清华大学", "博士生");  
        mDataset.addValue(900, "清华大学", "讲师");  
        mDataset.addValue(800, "清华大学", "副教授");  
        mDataset.addValue(300, "清华大学", "教授");  
        mDataset.addValue(600, "清华大学", "行政人员");  
        mDataset.addValue(400, "清华大学", "管理人员");                
        return mDataset;  
    }  
  
}  
2、对乱码的字体分别进行设置
一般Title和Legend的设置方法比较单一:
[java] 
//图表标题设置  
        TextTitle mTextTitle = mBarChart.getTitle();  
        mTextTitle.setFont(new Font("黑体", Font.BOLD, 20));  
          
        //图表图例设置  
        LegendTitle mLegend = mBarChart.getLegend();  
        if(mLegend != null)  
            mLegend.setItemFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
          
简洁的方式还可以这样:
[java]  
//图表标题设置  
        mBarChart.setTitle(new TextTitle("学校人员分布图",new Font("黑体", Font.BOLD, 20)));  
        //图表图例设置  
        mBarChart.getLegend().setItemFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
Plot对应不同的图表,它的设置方法不同。
柱状图:
先取得CategoryPlot的对象,再分别设置x轴和y轴的坐标字体以及标题字体。x轴使用CategoryAxis,y轴使用ValueAxis
[java]  
//设置柱状图轴  
        CategoryPlot mPlot = (CategoryPlot)mBarChart.getPlot();  
        //x轴  
        CategoryAxis mDomainAxis = mPlot.getDomainAxis();  
        //设置x轴标题的字体  
        mDomainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 15));  
        //设置x轴坐标字体  
        mDomainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 15));  
        //y轴  
        ValueAxis mValueAxis = mPlot.getRangeAxis();  
        //设置y轴标题字体  
        mValueAxis.setLabelFont(new Font("宋体", Font.PLAIN, 15));  
        //设置y轴坐标字体  
   
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,