JFreeChart中文乱码解决方案

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

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

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

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

  1. //创建主题样式  
  2.    StandardChartTheme standardChartTheme=new StandardChartTheme("CN");  
  3.    //设置标题字体  
  4.    standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));  
  5.    //设置图例的字体  
  6.    standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));  
  7.    //设置轴向的字体  
  8.    standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));  
  9.    //应用主题样式  
  10.    ChartFactory.setChartTheme(standardChartTheme); 

例如:

  1. package com.zzs.jfreechart.demo;  
  2.  
  3. import java.awt.Font;  
  4. import org.jfree.chart.ChartFactory;  
  5. import org.jfree.chart.ChartFrame;  
  6. import org.jfree.chart.JFreeChart;  
  7. import org.jfree.chart.StandardChartTheme;  
  8. import org.jfree.chart.plot.PlotOrientation;  
  9. import org.jfree.chart.title.LegendTitle;  
  10. import org.jfree.chart.title.TextTitle;  
  11. import org.jfree.data.category.DefaultCategoryDataset;  
  12.  
  13. public class JfreeChartTest {  
  14.        
  15.     public static void main(String[] args) {  
  16.    
  17. //     创建类别图(Category)数据对象  
  18.    
  19.        DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
  20.    
  21.        dataset.addValue(100"北京""苹果");  
  22.    
  23.        dataset.addValue(100"上海""苹果");  
  24.    
  25.        dataset.addValue(100"广州""苹果");  
  26.    
  27.        dataset.addValue(200"北京""梨子");  
  28.    
  29.        dataset.addValue(200"上海""梨子");  
  30.    
  31.        dataset.addValue(200"广州""梨子");  
  32.    
  33.        dataset.addValue(300"北京""葡萄");  
  34.    
  35.        dataset.addValue(300"上海""葡萄");  
  36.    
  37.        dataset.addValue(300"广州""葡萄");  
  38.    
  39.        dataset.addValue(400"北京""香蕉");  
  40.    
  41.        dataset.addValue(400"上海""香蕉");  
  42.    
  43.        dataset.addValue(400"广州""香蕉");  
  44.    
  45.        dataset.addValue(500"北京""荔枝");  
  46.    
  47.        dataset.addValue(500"上海""荔枝");  
  48.    
  49.        dataset.addValue(500"广州""荔枝");  
  50.        //创建主题样式  
  51.        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");  
  52.        //设置标题字体  
  53.        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));  
  54.        //设置图例的字体  
  55.        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));  
  56.        //设置轴向的字体  
  57.        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));  
  58.        //应用主题样式  
  59.        ChartFactory.setChartTheme(standardChartTheme);  
  60.         JFreeChart chart=ChartFactory.createBarChart3D("水果产量图""水果""水果", dataset, PlotOrientation.VERTICAL, truetruetrue);  
  61. //        TextTitle textTitle = chart.getTitle();  
  62. //      textTitle.setFont(new Font("宋体", Font.BOLD, 20));  
  63. //      LegendTitle legend = chart.getLegend();  
  64. //      if (legend != null) {  
  65. //          legend.setItemFont(new Font("宋体", Font.BOLD, 20));  
  66. //      }  
  67.        ChartFrame  frame=new ChartFrame ("水果产量图 ",chart,true);  
  68.        frame.pack();  
  69.        frame.setVisible(true);  
  70.     }  

二、制定乱码文字的字体

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

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

1.Title

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

2.Legent

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

3.Plot

 

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

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

  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轴坐标上的标题的字体   
  8. CategoryPlot plot = (CategoryPlot)freeChart.getPlot();   
  9. CategoryAxis domainAxis = plot.getDomainAxis();//(柱状图的x轴)   
  10. domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体   
  11. domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴上的标题的字体   
  12. ValueAxis valueAxis = plot.getRangeAxis();//(柱状图的y轴)   
  13. valueAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体   
  14. valueAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体 

对于使用PiePlot的图标(如饼状图):

  1. PiePlot plot = (PiePlot)freeChart.getPlot();   
  2. plot.setLabelFont(new Font("宋体",Font.BOLD,15));  

对于使用PiePlot的图标(如饼状图):

  1. PiePlot plot = (PiePlot)freeChart.getPlot();   
  2. plot.setLabelFont(new Font("宋体",Font.BOLD,15));  

下面一个实例:
 

  1. package com.zzs.jfreechart.demo;  
  2.  
  3. import java.awt.Font;  
  4. import javax.swing.JPanel;  
  5. import org.jfree.chart.ChartFactory;  
  6. import org.jfree.chart.ChartPanel;  
  7. import org.jfree.chart.JFreeChart;  
  8. import org.jfree.chart.plot.PiePlot;  
  9. import org.jfree.chart.title.LegendTitle;  
  10. import org.jfree.chart.title.TextTitle;  
  11. import org.jfree.data.general.DefaultPieDataset;  
  12. import org.jfree.data.general.PieDataset;  
  13. import org.jfree.ui.ApplicationFrame;  
  14. public class JfreeChartOne extends ApplicationFrame {  
  15.     private static final long serialVersionUID = 1L;  
  16.     public JfreeChartOne(String s)  
  17.     {  
  18.         super(s);  
  19.         setContentPane(createJPanel());  
  20.     }  
  21.     public static void main(String[] args) {  
  22.         JfreeChartOne one = new JfreeChartOne("CityInfoPort公司组织架构图");  
  23.         one.pack();  
  24.         one.setVisible(true);  
  25.     }  
  26.     // 利用静态方法设定数据源(饼状图)  
  27.     public static PieDataset createPieDataset() {  
  28.         DefaultPieDataset defaultpiedataset = new DefaultPieDataset();  
  29.         defaultpiedataset.setValue("管理人员"10.02D);  
  30.         defaultpiedataset.setValue("市场人员"20.23D);  
  31.         defaultpiedataset.setValue("开发人员"60.02D);  
  32.         defaultpiedataset.setValue("OEM人员"10.02D);  
  33.         defaultpiedataset.setValue("其他人员"5.11D);  
  34.         return defaultpiedataset;  
  35.     }  
  36.     // 通过ChartFactory创建JFreeChart的实例  
  37.     public static JFreeChart createJFreeChart(PieDataset p)  
  38.     {  
  39.         JFreeChart a = ChartFactory.createPieChart("CityInfoPort公司组织架构图", p,  
  40.                 truetruetrue);  
  41.         // JFreeChart主要由三个部分构成:title(标题),legend(图释),plot(图表主体)。  
  42.         //三个部分设置字体的方法分别如下:  
  43.         TextTitle textTitle = a.getTitle();  
  44.         textTitle.setFont(new Font("宋体", Font.BOLD, 20));  
  45.         LegendTitle legend = a.getLegend();  
  46.         if (legend != null) {  
  47.             legend.setItemFont(new Font("宋体", Font.BOLD, 20));  
  48.         }  
  49.         PiePlot pie = (PiePlot) a.getPlot();  
  50.         pie.setLabelFont(new Font("宋体", Font.BOLD, 12));  
  51.         pie.setNoDataMessage("No data available");  
  52.         pie.setCircular(true);  
  53.         pie.setLabelGap(0.01D);// 间距  
  54.         return a;  
  55.     }  
  56.     public static JPanel createJPanel() {  
  57.         JFreeChart jfreechart = createJFreeChart(createPieDataset());  
  58.         return new ChartPanel(jfreechart);  
  59.     }  

下面这个修改坐标轴:

  1. package com.zzs.jfreechart.demo;  
  2.  
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import org.jfree.chart.ChartFactory;  
  6. import org.jfree.chart.ChartFrame;  
  7. import org.jfree.chart.JFreeChart;  
  8. import org.jfree.chart.axis.CategoryAxis;  
  9. import org.jfree.chart.axis.ValueAxis;  
  10. import org.jfree.chart.plot.XYPlot;  
  11. import org.jfree.chart.title.LegendTitle;  
  12. import org.jfree.chart.title.TextTitle;  
  13. import org.jfree.data.time.Month;  
  14. import org.jfree.data.time.TimeSeries;  
  15. import org.jfree.data.time.TimeSeriesCollection;  
  16. import org.jfree.ui.RectangleInsets;  
  17. public class ShiJianXuLieTu01 {  
  18.  
  19.     /**  
  20.      * @param args  
  21.      */ 
  22.     public static void main(String[] args) {  
  23.         // TODO Auto-generated method stub  
  24.         //时间序列图  
  25.            TimeSeries timeseries = new TimeSeries("L&G European Index Trust",Month.class);  
  26.            timeseries.add(new Month(22001), 181.8D);//这里用的是Month.class,同样还有Day.class Year.class 等等  
  27.            timeseries.add(new Month(32001), 167.3D);  
  28.            timeseries.add(new Month(42001), 153.8D);  
  29.            timeseries.add(new Month(52001), 167.6D);  
  30.            timeseries.add(new Month(62001), 158.8D);  
  31.            timeseries.add(new Month(72001), 148.3D);  
  32.            timeseries.add(new Month(82001), 153.9D);  
  33.            timeseries.add(new Month(92001), 142.7D);  
  34.            timeseries.add(new Month(102001), 123.2D);  
  35.            timeseries.add(new Month(112001), 131.8D);  
  36.            timeseries.add(new Month(122001), 139.6D);  
  37.            timeseries.add(new Month(12002), 142.9D);  
  38.            timeseries.add(new Month(22002), 138.7D);  
  39.            timeseries.add(new Month(32002), 137.3D);  
  40.            timeseries.add(new Month(42002), 143.9D);  
  41.            timeseries.add(new Month(52002), 139.8D);  
  42.            timeseries.add(new Month(62002), 137D);  
  43.            timeseries.add(new Month(72002), 132.8D);  
  44.            TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust曾召帅",Month.class);  
  45.        
  46.            timeseries1.add(new Month(22001), 129.6D);  
  47.            timeseries1.add(new Month(32001), 123.2D);  
  48.            timeseries1.add(new Month(42001), 117.2D);  
  49.            timeseries1.add(new Month(52001), 124.1D);  
  50.            timeseries1.add(new Month(62001), 122.6D);   
  51.            timeseries1.add(new Month(72001), 119.2D);  
  52.            timeseries1.add(new Month(82001), 116.5D);  
  53.            timeseries1.add(new Month(92001), 112.7D);  
  54.            timeseries1.add(new Month(102001), 101.5D);  
  55.            timeseries1.add(new Month(112001), 106.1D);  
  56.            timeseries1.add(new Month(122001), 110.3D);  
  57.            timeseries1.add(new Month(12002), 111.7D);  
  58.            timeseries1.add(new Month(22002), 111D);  
  59.            timeseries1.add(new Month(32002), 109.6D);  
  60.            timeseries1.add(new Month(42002), 113.2D);  
  61.            timeseries1.add(new Month(52002), 111.6D);  
  62.            timeseries1.add(new Month(62002), 108.8D);  
  63.            timeseries1.add(new Month(72002), 101.6D);  
  64.            TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();  
  65.        
  66.            timeseriescollection.addSeries(timeseries);  
  67.             timeseriescollection.addSeries(timeseries1);  
  68.             timeseriescollection.setDomainIsPointsInTime(true); //domain轴上的刻度点代表的是时间点而不是时间段  
  69.            JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("合法 & General Unit Trust Prices",  
  70.                   "日期",  
  71.                   "暗示的话发神经提防",  
  72.                   timeseriescollection,  
  73.                   true,  
  74.                   true,  
  75.                   false);  
  76.                   jfreechart.setBackgroundPaint(Color.white);  
  77.                   TextTitle textTitle = jfreechart.getTitle();  
  78.                 textTitle.setFont(new Font("宋体", Font.BOLD, 20));  
  79.                 LegendTitle legend = jfreechart.getLegend();  
  80.                 if (legend != null) {  
  81.                     legend.setItemFont(new Font("宋体", Font.BOLD, 20));  
  82.                 }  
  83.                   XYPlot xyplot = (XYPlot)jfreechart.getPlot(); //获得 plot : XYPlot!!  
  84.                   ValueAxis domainAxis=xyplot.getDomainAxis();  
  85.                   domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体  
  86.                   domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的标题的字体  
  87.                   ValueAxis rangeAxis=xyplot.getRangeAxis();  
  88.                   rangeAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体  
  89.                   rangeAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体  
  90.                   xyplot.setBackgroundPaint(Color.lightGray);  
  91.                   xyplot.setDomainGridlinePaint(Color.white);  
  92.                   xyplot.setRangeGridlinePaint(Color.white);  
  93.                   xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));  
  94.                   xyplot.setDomainCrosshairVisible(true);  
  95.                   xyplot.setRangeCrosshairVisible(true);  
  96.                   ChartFrame  frame=new ChartFrame ("折线图 ",jfreechart,true);  
  97.                   frame.pack();  
  98.                   frame.setVisible(true);  
  99.     }  

原文链接:http://zengzhaoshuai.iteye.com/blog/1000378

【编辑推荐】

  1. Java***图形解决方案 JFreeChart学习总结
  2. Java精确截取字符串
  3. Java I/O系统基础知识
  4. Java 远程文件对接
  5. 解读Java环境变量配置
责任编辑:林师授 来源: zengzhaoshuai博客
相关推荐

2010-05-17 14:49:43

MySQL中文乱码

2010-05-12 16:47:54

MySQL 中文乱码

2009-07-24 11:24:33

ASP.NET中文乱码

2011-12-20 12:53:43

JavaJFreeChart

2010-05-31 18:33:00

MySQL中文乱码

2011-02-23 17:13:19

FileZilla

2010-05-17 09:49:46

MySQL中文问题

2011-04-01 15:09:08

MRTG乱码

2010-03-12 18:22:51

Python文本乱码

2010-05-27 12:49:30

MySQL中文乱码

2010-05-17 14:17:25

MySQL pytho

2010-05-31 12:38:48

Nagios中文

2011-03-29 14:35:34

2010-05-04 09:34:18

Oracle em

2011-12-22 12:37:17

JavaJFreeChart

2010-08-06 09:42:39

2010-06-07 09:22:21

2011-02-24 10:48:51

FireFTP

2009-11-26 16:30:52

Suse中文乱码问题

2018-12-03 12:17:27

Semptian解决方案
点赞
收藏

51CTO技术栈公众号