`
bcyy
  • 浏览: 1823990 次
文章分类
社区版块
存档分类
最新评论

JFreeChart乱码解决方法

 
阅读更多

jfreechart主要是用来动态产生各种数据图形的,可最初使用的时候大都会碰到图片中的中文乱码或是一个小方块的情况。
仔细研究主要有以下2种原因:

1:服务器缺少中文字体,这多发生在Hp等unix操作系统上,解决的方法就是下载可用字体库到系统中,
有人也提出在Windows上产生图片在传回到Unix主机上的方法。
2:软件版本问题,jfreechart-1.0.10有人说没有问题,但jfreechart-1.0.11到13都有问题,我用的最新的jfreechart-1.0.13不做设置是有问题的。
究其原因,是它代码的内部设置的字体有问题.
先来跟踪一下它的代码:

JFreeChart chart = ChartFactory.createBarChart(
"数据统计图",
"设备号",
"积累值",
dataset,
PlotOrientation.VERTICAL,
true, true, false
);
它的原型
public static JFreeChart createBarChart(String title,
String categoryAxisLabel,
String valueAxisLabel,
CategoryDataset dataset,
PlotOrientation orientation,
boolean legend,
boolean tooltips,
boolean urls) {
上面的原型又调用了
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
看看缺省字体的定义:
public static final Font DEFAULT_TITLE_FONT
= new Font("SansSerif", Font.BOLD, 18);
看看当前主题currentTheme是什么
private static ChartTheme currentTheme = new StandardChartTheme("JFree");
看它的原型定义
public StandardChartTheme(String name) {
if (name == null) {
throw new IllegalArgumentException("Null 'name' argument.");
}
this.name = name;
this.extraLargeFont = new Font("Tahoma", Font.BOLD, 20);
this.largeFont = new Font("Tahoma", Font.BOLD, 14);
this.regularFont = new Font("Tahoma", Font.PLAIN, 12);
this.smallFont = new Font("Tahoma", Font.PLAIN, 10);
……
看到了吧,默认的标题字体是SansSerif,在很多中文系统中是没有这种字体的,这可能是用老外开发开源产品的弊端吧。
首先说标题的乱码吧:
public JFreeChart(String title, Font titleFont, Plot plot,
boolean createLegend) {
……
对标题设置的代码:
if (title != null) {
if (titleFont == null) {
titleFont = DEFAULT_TITLE_FONT;
}
this.title = new TextTitle(title, titleFont);
this.title.addChangeListener(this);
}
它使用了默认字体,因此要解决这个问题只要,对标题重新设置字体就可以了。
……
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
图例和其它乱码一样处理,更换字体。
CategoryPlot plot = chart.getCategoryPlot();//获得图表区域对象
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(true);
plot.setDomainAxis(domainAxis);
ValueAxis rAxis = plot.getRangeAxis();
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置Y轴坐标上的文字-----------*/
rAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置Y轴的标题文字------------*/
rAxis.setLabelFont(new Font("黑体",Font.PLAIN,15));

/*------设置最底下方框内的字体------------*/

chart.getLegend().setItemFont(new Font("黑体", Font.PLAIN, 15));
这里需要注意的是,哪里出现了乱码就修改哪里的字体,将字体转换为系统有的就可以了。
另外有人提出将jfreechart源文件里面的涉及到SansSerif字体的地方都替换成中文字体在重新编译,来个一劳永逸,我没有试,不知可不可以,我主要采用了重新设置字体的方法。

上面对于饼状图3D无效果:

 //创建主题样式         
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");         
        //设置标题字体        
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));         
        //设置图例的字体         
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));         
        //设置轴向的字体        
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));         
        //应用主题样式        
        ChartFactory.setChartTheme(standardChartTheme);         
在创建Chart之前设置

转自:http://blog.csdn.net/kinble/article/details/4155085

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics