`
mtnt2008
  • 浏览: 363631 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

JFreeChar乱码问题

阅读更多

 

JFreeChart中文乱码的解决方法

    使用JFreeChart绘制图表的时候,如果使用默认的字体会导致图标中的汉字显示为乱码。解决方法如下:

JFreeChart是用户使用该库提供的各类图标的统一接口,JFreeChart主要由三个部分构成:title(标题),legend(图释),plot(图表主体)。三个部分设置字体的方法分别如下:

1.Title

view plaincopy to clipboardprint?
TextTitle textTitle = freeChart.getTitle();  
textTitle.setFont(new Font("宋体",Font.BOLD,20)); 
TextTitle textTitle = freeChart.getTitle();
textTitle.setFont(new Font("宋体",Font.BOLD,20));

2.Legent

view plaincopy to clipboardprint?
LegendTitle legend = freeChart.getLegend();  
if (legend!=null) {  
   legend.setItemFont(new Font("宋体", Font.BOLD, 20));} 
LegendTitle legend = freeChart.getLegend();
if (legend!=null) {
   legend.setItemFont(new Font("宋体", Font.BOLD, 20));}

3.Plot

 

    对于不同类型的图表对应Plot的不同的实现类,设置字体的方法也不完全相同。

    对于使用CategoryPlot的图表(如柱状图):

view plaincopy to clipboardprint?
CategoryPlot plot = (CategoryPlot)freeChart.getPlot();  
CategoryAxis domainAxis = plot.getDomainAxis();//(柱状图的x轴)  
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体  
domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴上的标题的字体    
ValueAxis valueAxis = plot.getRangeAxis();//(柱状图的y轴)  
valueAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体  
valueAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体 
CategoryPlot plot = (CategoryPlot)freeChart.getPlot();
CategoryAxis domainAxis = plot.getDomainAxis();//(柱状图的x轴)
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体
domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴上的标题的字体 
ValueAxis valueAxis = plot.getRangeAxis();//(柱状图的y轴)
valueAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体
valueAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体


对于使用PiePlot的图标(如饼状图):


view plaincopy to clipboardprint?
PiePlot plot = (PiePlot)freeChart.getPlot();  
plot.setLabelFont(new Font("宋体",Font.BOLD,15)); 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics