JFreeChart中文乱码解决方案

来源:互联网 发布:js gzip 解压缩字符串 编辑:程序博客网 时间:2024/04/26 13:55

由于JFreeChart组件的版本、操作平台、JDK的设置等因素,在使用JFreeChart组件时可能会出现中文乱码的现象。遇到此问题时,可通过设置文字的字体来解决问题。在此提供以下两种解决此问题的方法。

 

一、设置主题的样式(强烈推荐)

在制图前,创建主题样式并制定样式中的字体,通过ChartFactory的setChartTheme()方法设置主题样式。

复制代码
//创建主题样式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);
复制代码

 

例如:

复制代码
DefaultPieDataset dataset = new DefaultPieDataset();dataset.setValue("香蕉", 56.4);dataset.setValue("苹果", 63.5);dataset.setValue("橘子", 58.4);dataset.setValue("西瓜", 76.3);// 创建主题样式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);JFreeChart chart = ChartFactory.createPieChart("测试PieChart",dataset, true, true, false);ChartUtilities.saveChartAsJPEG(new File(filePath), chart, 400, 300);
复制代码

 

二、制定乱码文字的字体

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

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

1.Title

TextTitle textTitle = freeChart.getTitle();textTitle.setFont(new Font("宋体",Font.BOLD,20));

 

2.Legent

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

3.Plot

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

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

复制代码
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的图标(如饼状图):

复制代码
//三个部分设置字体的方法分别如下:TextTitle textTitle = chart.getTitle();textTitle.setFont(new Font("宋体", Font.BOLD, 20));LegendTitle legend = chart.getLegend();if (legend != null) {legend.setItemFont(new Font("宋体", Font.BOLD, 20));}PiePlot pie = (PiePlot) chart.getPlot();pie.setLabelFont(new Font("宋体", Font.BOLD, 12));pie.setNoDataMessage("No data available");pie.setCircular(true);pie.setLabelGap(0.01D);// 间距
复制代码
转载自:  http://www.cnblogs.com/jtmjx/archive/2013/04/29/3050846.html
0 0
原创粉丝点击