JFreeChart中文乱码的解决方法
使用JFreeChart绘制图表的时候,如果使用默认的字体会导致图标中的汉字显示为乱码。解决方法如下:
JFreeChart是用户使用该库提供的各类图标的统一接口,JFreeChart主要由三个部分构成:title(标题),legend(图释),plot(图表主体)。三个部分设置字体的方法分别如下: 1.Title view plaincopy to clipboardprint? 1. TextTitle textTitle = freeChart.getTitle(); 2. textTitle.setFont(new Font(\"宋体\",Font.BOLD,20));
2.Legent view plaincopy to clipboardprint? 1. LegendTitle legend = freeChart.getLegend(); 2. if (legend!=null) {
3. legend.setItemFont(new Font(\"宋体\", Font.BOLD, 20));}
3.Plot
对于不同类型的图表对应Plot的不同的实现类,设置字体的方法也不完全相同。 对于使用CategoryPlot的图表(如柱状图): view plaincopy to clipboardprint? 1. CategoryPlot plot = (CategoryPlot)freeChart.getPlot();
2. CategoryAxis domainAxis = plot.getDomainAxis();//(柱状图的x轴)
3. domainAxis.setTickLabelFont(new Font(\"宋体\",Font.BOLD,20));//设置x轴坐标上的字
体
4. domainAxis.setLabelFont(new Font(\"宋体\",Font.BOLD,20));//设置x轴上的标题的字
体
5. ValueAxis valueAxis = plot.getRangeAxis();//(柱状图的y轴)
6. valueAxis.setTickLabelFont(new Font(\"宋体\",Font.BOLD,20));//设置y轴坐标上的字
体
7. valueAxis.setLabelFont(new Font(\"宋体\",Font.BOLD,20));//设置y轴坐标上的标题的
字体
对于使用PiePlot的图标(如饼状图): view plaincopy to clipboardprint? 1. PiePlot plot = (PiePlot)freeChart.getPlot(); 2. plot.setLabelFont(new Font(\"宋体\",Font.BOLD,15));
柱状图(CategoryPlot):
CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象 CategoryAxis domainAxis=plot.getDomainAxis(); //水平底部列表
domainAxis.setLabelFont(new Font(\"黑体\ //水平底部标题
domainAxis.setTickLabelFont(new Font(\"宋体\ //垂直标题
ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状 rangeAxis.setLabelFont(new Font(\"黑体\饼图(PiePlot):
JFreeChart chart = ChartFactory.createPieChart3D(\"IT行业职业分布图\dataset, true, false, false);
chart.getTitle().setFont(new Font(\"黑体\设置标题字体 PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象 piePlot.setLabelFont(new Font(\"黑体\
chart.getLegend().setItemFont(new Font(\"黑体\时序图(TimeSeries) XYPlot :
XYPlot plot = (XYPlot) chart.getPlot(); //纵轴字体
plot.getRangeAxis().setLabelFont(new Font(\"宋体\ //横轴框里的标题字体
chart.getLegend().setItemFont(new Font(\"宋体\ //横轴列表字体
plot.getDomainAxis().setTickLabelFont(new Font(\"新宋体\ //横轴小标题字体
plot.getDomainAxis().setLabelFont(new Font(\"新宋体\