您的位置:首页 > 其它

JFreeChart中文显示乱码,英文正常的solution

2016-04-19 15:19 363 查看
低版本JFreeChart可以直接正常显示中文,而新版本的JFreeChart不再为显示中文提供方便,需要程序员自己进行设置字体,然后才可以显示中文,否则乱码,通常显示为方框。

显然,常规的解决思路就是自己设置字体。下面记录了常见设置:

折线图显示中文需要添加下列代码:

Font titleFont=new Font("宋体", Font.ITALIC, 22);
Font font=new Font("宋体",Font.BOLD,14);
Font legendFont=new Font("隶书", Font.BOLD, 16);

jfreechart.getTitle().setFont(titleFont);
jfreechart.getLegend().setItemFont(legendFont);

XYPlot plot=jfreechart.getXYPlot();<span style="white-space:pre">	</span>//或者有时可能是<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">CategoryPlot plot=jfreechart.getCategoryPlot(); </span>
plot.getDomainAxis().setLabelFont(font);
plot.getDomainAxis().setTickLabelFont(font);
plot.getRangeAxis().setLabelFont(font);


柱状图显示中文需要添加下列代码:

jfreechart.getTitle().setFont(new Font("宋体", Font.ITALIC, 22));//设置标题字体
jfreechart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 16));  <span style="font-family: Arial, Helvetica, sans-serif;">//设置图例类别字体 </span>

jfreechart.setBackgroundPaint(Color.WHITE);
CategoryPlot categoryPlot=jfreechart.getCategoryPlot();//用于设置显示特性
categoryPlot.setBackgroundPaint(Color.WHITE);
categoryPlot.setDomainGridlinePaint(Color.BLACK);//分类轴网格线条颜色
categoryPlot.setDomainGridlinesVisible(true);
categoryPlot.setRangeGridlinePaint(Color.GREEN);//数据轴网格线条颜色

CategoryAxis domainAxis=categoryPlot.getDomainAxis(); //水平底部列表
domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14)); //水平底部标题
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //垂直标题
ValueAxis rangeAxis=categoryPlot.getRangeAxis();//获取柱状
rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15)); //设置柱状标题


饼状图显示中文需要添加下列代码:

Font titleFont=new Font("宋体", Font.ITALIC, 22);
Font font=new Font("宋体",Font.BOLD,14);
Font legendFont=new Font("隶书", Font.BOLD, 16);

jfreechart.getTitle().setFont(titleFont);
jfreechart.getLegend().setItemFont(legendFont);

PiePlot plot=(PiePlot)jfreechart.getPlot();
plot.setLabelFont(font);


JFreeChart中文显示乱码还有更高级更统一的解决办法,详见http://blog.sina.com.cn/s/blog_67fdef900101ebgf.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: