JFreeChart中文乱码的解决方法(补充方法二)
使用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的图标(如饼状图):
PiePlot plot = (PiePlot)freeChart.getPlot();
plot.setLabelFont(new Font("宋体",Font.BOLD,15));
对于使用PiePlot的图标(如饼状图):
view plaincopy to clipboardprint?
PiePlot plot = (PiePlot)freeChart.getPlot();
plot.setLabelFont(new Font("宋体",Font.BOLD,15));
下面一个实例:
package com.zzs.jfreechart.demo;import java.awt.Color;import java.awt.Font;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartFrame;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.plot.XYPlot;import org.jfree.chart.title.LegendTitle;import org.jfree.chart.title.TextTitle;import org.jfree.data.time.Month;import org.jfree.data.time.TimeSeries;import org.jfree.data.time.TimeSeriesCollection;import org.jfree.ui.RectangleInsets;public class ShiJianXuLieTu01 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub//时间序列图 TimeSeries timeseries = new TimeSeries("L&G European Index Trust",Month.class); timeseries.add(new Month(2, 2001), 181.8D);//这里用的是Month.class,同样还有Day.class Year.class 等等 timeseries.add(new Month(3, 2001), 167.3D); timeseries.add(new Month(4, 2001), 153.8D); timeseries.add(new Month(5, 2001), 167.6D); timeseries.add(new Month(6, 2001), 158.8D); timeseries.add(new Month(7, 2001), 148.3D); timeseries.add(new Month(8, 2001), 153.9D); timeseries.add(new Month(9, 2001), 142.7D); timeseries.add(new Month(10, 2001), 123.2D); timeseries.add(new Month(11, 2001), 131.8D); timeseries.add(new Month(12, 2001), 139.6D); timeseries.add(new Month(1, 2002), 142.9D); timeseries.add(new Month(2, 2002), 138.7D); timeseries.add(new Month(3, 2002), 137.3D); timeseries.add(new Month(4, 2002), 143.9D); timeseries.add(new Month(5, 2002), 139.8D); timeseries.add(new Month(6, 2002), 137D); timeseries.add(new Month(7, 2002), 132.8D); TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust曾召帅",Month.class); timeseries1.add(new Month(2, 2001), 129.6D); timeseries1.add(new Month(3, 2001), 123.2D); timeseries1.add(new Month(4, 2001), 117.2D); timeseries1.add(new Month(5, 2001), 124.1D); timeseries1.add(new Month(6, 2001), 122.6D); timeseries1.add(new Month(7, 2001), 119.2D); timeseries1.add(new Month(8, 2001), 116.5D); timeseries1.add(new Month(9, 2001), 112.7D); timeseries1.add(new Month(10, 2001), 101.5D); timeseries1.add(new Month(11, 2001), 106.1D); timeseries1.add(new Month(12, 2001), 110.3D); timeseries1.add(new Month(1, 2002), 111.7D); timeseries1.add(new Month(2, 2002), 111D); timeseries1.add(new Month(3, 2002), 109.6D); timeseries1.add(new Month(4, 2002), 113.2D); timeseries1.add(new Month(5, 2002), 111.6D); timeseries1.add(new Month(6, 2002), 108.8D); timeseries1.add(new Month(7, 2002), 101.6D); TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(); timeseriescollection.addSeries(timeseries); timeseriescollection.addSeries(timeseries1); timeseriescollection.setDomainIsPointsInTime(true); //domain轴上的刻度点代表的是时间点而不是时间段 JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("合法 & General Unit Trust Prices", "日期", "暗示的话发神经提防", timeseriescollection, true, true, false); jfreechart.setBackgroundPaint(Color.white); TextTitle textTitle = jfreechart.getTitle(); textTitle.setFont(new Font("宋体", Font.BOLD, 20)); LegendTitle legend = jfreechart.getLegend(); if (legend != null) { legend.setItemFont(new Font("宋体", Font.BOLD, 20)); } XYPlot xyplot = (XYPlot)jfreechart.getPlot(); //获得 plot : XYPlot!! ValueAxis domainAxis=xyplot.getDomainAxis(); domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体 domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的标题的字体 ValueAxis rangeAxis=xyplot.getRangeAxis(); rangeAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体 rangeAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体 xyplot.setBackgroundPaint(Color.lightGray); xyplot.setDomainGridlinePaint(Color.white); xyplot.setRangeGridlinePaint(Color.white); xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); xyplot.setDomainCrosshairVisible(true); xyplot.setRangeCrosshairVisible(true); ChartFrame frame=new ChartFrame ("折线图 ",jfreechart,true); frame.pack(); frame.setVisible(true);}}