由于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);
//创建主题样式 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);
例如:
- package com.zzs.jfreechart.demo;
-
- import java.awt.Font;
-
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.ChartFrame;
- import org.jfree.chart.JFreeChart;
- import org.jfree.chart.StandardChartTheme;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.chart.title.LegendTitle;
- import org.jfree.chart.title.TextTitle;
- import org.jfree.data.category.DefaultCategoryDataset;
-
- public class JfreeChartTest {
-
- public static void main(String[] args) {
-
-
-
- DefaultCategoryDataset dataset = new DefaultCategoryDataset();
-
- dataset.addValue(100, "北京", "苹果");
-
- dataset.addValue(100, "上海", "苹果");
-
- dataset.addValue(100, "广州", "苹果");
-
- dataset.addValue(200, "北京", "梨子");
-
- dataset.addValue(200, "上海", "梨子");
-
- dataset.addValue(200, "广州", "梨子");
-
- dataset.addValue(300, "北京", "葡萄");
-
- dataset.addValue(300, "上海", "葡萄");
-
- dataset.addValue(300, "广州", "葡萄");
-
- dataset.addValue(400, "北京", "香蕉");
-
- dataset.addValue(400, "上海", "香蕉");
-
- dataset.addValue(400, "广州", "香蕉");
-
- dataset.addValue(500, "北京", "荔枝");
-
- dataset.addValue(500, "上海", "荔枝");
-
- dataset.addValue(500, "广州", "荔枝");
-
- 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.createBarChart3D("水果产量图", "水果", "水果", dataset, PlotOrientation.VERTICAL, true, true, true);
-
-
-
-
-
-
- ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);
-
- frame.pack();
-
- frame.setVisible(true);
-
- }
-
-
- }
二、制定乱码文字的字体
使用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));
4.jfreechart中文乱码问题解决方案(转)
柱状图(CategoryPlot):
CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
CategoryAxis domainAxis=plot.getDomainAxis();
//水平底部列表
domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));
//水平底部标题
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
//垂直标题
ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
饼图(PiePlot):
JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图", dataset, true, false, false);
chart.getTitle().setFont(new Font("黑体",Font.BOLD,20));//设置标题字体
PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
piePlot.setLabelFont(new Font("黑体",Font.BOLD,10));
chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10));
时序图(TimeSeries)
XYPlot plot = (XYPlot) chart.getPlot();
//纵轴字体
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15));
//横轴框里的标题字体
chart.getLegend().setItemFont(new Font("宋体", Font.ITALIC, 15));
//横轴列表字体
plot.getDomainAxis().setTickLabelFont(new Font("新宋体", 1, 15));
//横轴小标题字体
plot.getDomainAxis().setLabelFont(new Font("新宋体", 1, 12));
折线图
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 15));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
CategoryAxis domainAxis = plot.getDomainAxis();
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 11));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
/*------设置Y轴坐标上的文字-----------*/
numberaxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12));
/*------设置Y轴的标题文字------------*/
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12))
下面一个实例:
- package com.zzs.jfreechart.demo;
-
- import java.awt.Font;
import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; import org.jfree.ui.ApplicationFrame; public class JfreeChartOne extends ApplicationFrame { private static final long serialVersionUID = 1L; public JfreeChartOne(String s) { super(s); setContentPane(createJPanel()); } public static void main(String[] args) { JfreeChartOne one = new JfreeChartOne("CityInfoPort公司组织架构图"); one.pack(); one.setVisible(true); } public static PieDataset createPieDataset() { DefaultPieDataset defaultpiedataset = new DefaultPieDataset(); defaultpiedataset.setValue("管理人员", 10.02D); defaultpiedataset.setValue("市场人员", 20.23D); defaultpiedataset.setValue("开发人员", 60.02D); defaultpiedataset.setValue("OEM人员", 10.02D); defaultpiedataset.setValue("其他人员", 5.11D); return defaultpiedataset; } public static JFreeChart createJFreeChart(PieDataset p) { JFreeChart a = ChartFactory.createPieChart("CityInfoPort公司组织架构图", p, true, true, true); TextTitle textTitle = a.getTitle(); textTitle.setFont(new Font("宋体", Font.BOLD, 20)); LegendTitle legend = a.getLegend(); if (legend != null) { legend.setItemFont(new Font("宋体", Font.BOLD, 20)); } PiePlot pie = (PiePlot) a.getPlot(); pie.setLabelFont(new Font("宋体", Font.BOLD, 12)); pie.setNoDataMessage("No data available"); pie.setCircular(true); pie.setLabelGap(0.01D); return a; } public static JPanel createJPanel() { JFreeChart jfreechart = createJFreeChart(createPieDataset()); return new ChartPanel(jfreechart); } } package com.zzs.jfreechart.demo;import java.awt.Font;import javax.swing.JPanel;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartPanel;import org.jfree.chart.JFreeChart;import org.jfree.chart.plot.PiePlot;import org.jfree.chart.title.LegendTitle;import org.jfree.chart.title.TextTitle;import org.jfree.data.general.DefaultPieDataset;import org.jfree.data.general.PieDataset;import org.jfree.ui.ApplicationFrame;public class JfreeChartOne extends ApplicationFrame { private static final long serialVersionUID = 1L; public JfreeChartOne(String s) { super(s); setContentPane(createJPanel()); } public static void main(String[] args) { JfreeChartOne one = new JfreeChartOne("CityInfoPort公司组织架构图"); one.pack(); one.setVisible(true); } // 利用静态方法设定数据源(饼状图) public static PieDataset createPieDataset() { DefaultPieDataset defaultpiedataset = new DefaultPieDataset(); defaultpiedataset.setValue("管理人员", 10.02D); defaultpiedataset.setValue("市场人员", 20.23D); defaultpiedataset.setValue("开发人员", 60.02D); defaultpiedataset.setValue("OEM人员", 10.02D); defaultpiedataset.setValue("其他人员", 5.11D); return defaultpiedataset; } // 通过ChartFactory创建JFreeChart的实例 public static JFreeChart createJFreeChart(PieDataset p) { JFreeChart a = ChartFactory.createPieChart("CityInfoPort公司组织架构图", p, true, true, true); // JFreeChart主要由三个部分构成:title(标题),legend(图释),plot(图表主体)。 //三个部分设置字体的方法分别如下: TextTitle textTitle = a.getTitle(); textTitle.setFont(new Font("宋体", Font.BOLD, 20)); LegendTitle legend = a.getLegend(); if (legend != null) { legend.setItemFont(new Font("宋体", Font.BOLD, 20)); } PiePlot pie = (PiePlot) a.getPlot(); pie.setLabelFont(new Font("宋体", Font.BOLD, 12)); pie.setNoDataMessage("No data available"); pie.setCircular(true); pie.setLabelGap(0.01D);// 间距 return a; } public static JPanel createJPanel() { JFreeChart jfreechart = createJFreeChart(createPieDataset()); return new ChartPanel(jfreechart); }}
下面这个修改坐标轴:
- 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 {
-
-
-
-
- public static void main(String[] args) {
-
-
-
- TimeSeries timeseries = new TimeSeries("L&G European Index Trust",Month.class);
-
- timeseries.add(new Month(2, 2001), 181.8D);
-
- 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);
-
- 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();
- ValueAxis domainAxis=xyplot.getDomainAxis();
- domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));
- domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));
-
- ValueAxis rangeAxis=xyplot.getRangeAxis();
- rangeAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));
- rangeAxis.setLabelFont(new Font("宋体",Font.BOLD,20));
-
- 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);
-
-
-
- }
-
- }