Jfreechart中文乱码解决方法
jfreechart对中文的支持部是很好,我开始做的时候也有乱码,下面的方案是在java上运行可以的。。。
<!--[if !supportLists]-->1. <!--[endif]-->柱状图(CategoryPlot):
CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
CategoryAxis domainAxis=plot.getDomainAxis();
//水平底部列表
domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));
//水平底部标题
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
//垂直标题
ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
<!--[if !supportLists]-->2. <!--[endif]-->饼图(PiePlot):
JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图", dataset, true, false, false);
chart.getTitle().setFont(new Font("黑体",Font.BOLD,20));//设置标题字体
PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
piePlot.setLabelFont(new Font("黑体",Font.BOLD,10));
chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10));
<!--[if !supportLists]-->3. <!--[endif]-->时序图(TimeSeries)
XYPlot plot = (XYPlot) chart.getPlot();
//纵轴字体
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15));
//横轴框里的标题字体
chart.getLegend().setItemFont(new Font("宋体", Font.ITALIC, 15));
//横轴列表字体
plot.getDomainAxis().setTickLabelFont(new Font("新宋体", 1, 15));
//横轴小标题字体
plot.getDomainAxis().setLabelFont(new Font("新宋体", 1, 12));
<!--[if !supportLists]-->4. <!--[endif]-->折线图
CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 15));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
CategoryAxis domainAxis = plot.getDomainAxis();
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 11));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
/*------设置Y轴坐标上的文字-----------*/
numberaxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12));
/*------设置Y轴的标题文字------------*/
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12))
下面一个实例:
package com.zzs.jfreechart.demo;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
public class JfreeChartOne extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public JfreeChartOne(String s)
{
super(s);
setContentPane(createJPanel());
}
public static void main(String[] args) {
JfreeChartOne one = new JfreeChartOne("CityInfoPort公司组织架构图");
one.pack();
one.setVisible(true);
}
// 利用静态方法设定数据源(饼状图)
public static PieDataset createPieDataset() {
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
defaultpiedataset.setValue("管理人员", 10.02D);
defaultpiedataset.setValue("市场人员", 20.23D);
defaultpiedataset.setValue("开发人员", 60.02D);
defaultpiedataset.setValue("OEM人员", 10.02D);
defaultpiedataset.setValue("其他人员", 5.11D);
return defaultpiedataset;
}
// 通过ChartFactory创建JFreeChart的实例
public static JFreeChart createJFreeChart(PieDataset p)
{
JFreeChart a = ChartFactory.createPieChart("CityInfoPort公司组织架构图", p,
true, true, true);
// JFreeChart主要由三个部分构成:title(标题),legend(图释),plot(图表主体)。
//三个部分设置字体的方法分别如下:
TextTitle textTitle = a.getTitle();
textTitle.setFont(new Font("宋体", Font.BOLD, 20));
LegendTitle legend = a.getLegend();
if (legend != null) {
legend.setItemFont(new Font("宋体", Font.BOLD, 20));
}
PiePlot pie = (PiePlot) a.getPlot();
pie.setLabelFont(new Font("宋体", Font.BOLD, 12));
pie.setNoDataMessage("No data available");
pie.setCircular(true);
pie.setLabelGap(0.01D);// 间距
return a;
}
public static JPanel createJPanel() {
JFreeChart jfreechart = createJFreeChart(createPieDataset());
return new ChartPanel(jfreechart);
}
}