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

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

在使用JFreeChart生成图表时,可能会出现中文乱码,下面给出两种解决方法。

1、设置主题样式(推荐方法)

主题样式是在建立图表之前进行设置,JFreeChart图表一般包括3个部分:Title(图表标题)、Plot(图表主体)、Legend(图表图例),主题是统一对这三个部分进行字体设置

//创建主题样式
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);

实例:

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的设置方法比较单一:

//图表标题设置
		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));
		
简洁的方式还可以这样:

//图表标题设置
		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

//设置柱状图轴
		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轴坐标字体
		mValueAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 15));
饼图:

饼图没有x轴和y轴,所以不用设置,需要设置的是饼图上指示比例的标签的字体

                PiePlot mChartPlot = (PiePlot)mChart.getPlot();
		mChartPlot.setLabelFont(new Font("宋体", Font.CENTER_BASELINE, 15));
时序图:

时序图有x轴和y轴,所以Plot的设置与柱状图基本相同,只是时序图声明的Plot类型为XYPlot,柱状图为CateGoryPlot

//设置柱状图轴
		XYPlot mPlot = (XYPlot)mTimeChart.getPlot();
		//x轴
		ValueAxis 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轴坐标字体
		mValueAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 15));