打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JFreeChart中文乱码和文字模糊问题的通用解决方案

在使用JFreeChart (http://www.jfree.org/jfreechart/ )做中文的图表时,中文乱码是一个最常要处理的问题,看网上许多文章都是在JFreeChart对象上下功夫,每次都得重新设置字体,比较麻烦。其实JFreeChart为我们提供了一个通用的解决方案——ChartTheme 

       ChartTheme有一个默认的实现类StandardChartTheme ,该类提供了图表主题的默认实现,通过ChartFactory 默认将这一实现应用到所有ChartFactory所生成的JFreeChart 对象中,你可以利用ChartFactory的setChartTheme(ChartThemetheme) 方法改变这一默认实现,让所有由ChartFactory生成的图表都应用你所指定的主题。

StandardChartTheme提供如下方法来定制字体:

  • public void setExtraLargeFont(Fontfont)
  • public void setLargeFont(Fontfont)
  • public void setRegularFont(Fontfont)
  • public void setSmallFont(Fontfont)

  只要重写这几个方法,就可以制定出一个适合中文图表的主题了:

StandardChartTheme theme = new StandardChartTheme("unicode"){   
     public void apply(JFreeChart chart){   
         chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,   
                 RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);   
         super.apply(chart);   
       
  };   
   theme.setExtraLargeFont(newFont("宋体", Font.PLAIN,14));   
   theme.setLargeFont(newFont("宋体", Font.PLAIN,14));   
   theme.setRegularFont(newFont("宋体", Font.PLAIN,12));   
   theme.setSmallFont(newFont("宋体", Font.PLAIN,10));   
  ChartFactory.setChartTheme(theme);

重写apply(...)方法是为了借机消除文字锯齿.VALUE_TEXT_ANTIALIAS_OFF

ChartFactory.setChartTheme(theme); 用于将该主题作为工厂的默认主题。

这样一来,以后使用ChartFactory创建图表时,都可以自动应用主题中的配置了,做到解决中文乱码和锯齿一劳永逸。

demo:

import java.awt.Font;
import java.awt.RenderingHints;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.data.general.DefaultPieDataset;

public class Test1 {
public static void main(String[] args) {
   DefaultPieDataset dfp = newDefaultPieDataset();
  
   dfp.setValue("开发人员",110);
   dfp.setValue("测试人员",8);
   dfp.setValue("人事",4);
   dfp.setValue("部门经理",8);
   dfp.setValue("销售人员",15);
   // 通用的中文问题解决方案
   StandardChartTheme theme = newStandardChartTheme("unicode"){   
     public void apply(JFreeChart chart){   
         chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,   
                 RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);   
         super.apply(chart);   
       
  };   
   theme.setExtraLargeFont(newFont("宋体", Font.PLAIN,14));   
   theme.setLargeFont(newFont("宋体", Font.PLAIN,14));   
   theme.setRegularFont(newFont("宋体", Font.PLAIN,12));   
   theme.setSmallFont(newFont("宋体", Font.PLAIN,10));   
  ChartFactory.setChartTheme(theme); 
  
   JFreeChart jfreeChart =ChartFactory.createPieChart("全景人员信息图", dfp,
    true, true, true);
  
  
   ChartFrame chartFrame = newChartFrame("全景人员信息图", jfreeChart, true);
   chartFrame.pack();
  chartFrame.setVisible(true);
}
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JFreeChart 中文乱码解决方法方案
第一个JFreeChart实例
使用JFreeChart创建图象|JAVAAPPLET与SWING|
jfreechart画复合图(混合图)已做乱码处理
struts2+hibernate整合jfreechart
jfreechart
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服