`

JFreeChart中文乱码解决方案

 
阅读更多

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

AD:2013云计算架构师峰会课程资料下载

 

由于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

分享到:
评论

相关推荐

    jfreechart中文乱码问题解决方案

    下面将详细阐述如何解决JFreeChart中文乱码问题,并提供不同图表类型的具体配置示例。 1. **柱状图(CategoryPlot)**:在柱状图中,我们需要设置`CategoryPlot`的`domainAxis`和`rangeAxis`的字体,以及图例的字体。...

    jfreechart linux乱码解决方案

    通过以上步骤,你应该能够在Linux环境中成功解决JFreeChart的中文乱码问题。记住,关键在于正确配置字体和确保Java能够找到并使用这些字体。在实际开发过程中,根据具体环境和需求,可能需要进行一些额外的调整。

    解决jfreechart中文乱码方案整理.doc

    因此,针对JFreeChart中的中文乱码问题,本文将汇总并整理一系列有效的解决方案。 #### JFreeChart简介 JFreeChart是一款开源Java图表库,它提供了丰富的API来创建高质量的图表。JFreeChart支持多种类型的图表,...

    Linux下的JFreeChart中文乱码口口解决

    JFreeChart是一个强大的Java库,它提供了一套完整的解决方案来创建各种图表,如饼图、柱状图、线图等。然而,由于编码问题,当图表中包含中文字符时,可能会显示为方框或无法识别的字符。解决这个问题需要对Java的...

    JFreeChart中文乱码问题解决

    本文将深入探讨如何解决JFreeChart中的中文乱码问题,并通过具体的代码示例来演示解决方案。 #### 一、问题背景 JFreeChart是一款开源的Java图表库,它能够生成各种类型的图表,如折线图、柱状图、饼图等。但在...

    jfreechart Linux服务器图片中文乱码解决方案(亲测,已解决多台服务器)

    通过以上步骤,你应该能够成功解决JFreeChart在Linux服务器上生成图片时的中文乱码问题。值得注意的是,这种方法不仅适用于JFreeChart,还适用于其他依赖于Java运行环境显示中文字符的程序。在实际操作中,应根据...

    jfreechart部署linux服务器生成图片乱码解决方案

    标题 "jfreechart部署在Linux服务器上生成图片乱码的解决方案" 涉及到的主要知识点是关于Java图形库JFreeChart在Linux环境中的使用,特别是解决中文字符乱码的问题。JFreeChart是一个广泛使用的开源Java库,它允许...

    jfreechart-1.0.13中文乱码,数值显示等问题

    JFreeChart是一款强大的Java图表库,它允许开发者创建多种类型的2D和3D图表,如柱状图、折线图和...在你的项目中,可以参考提供的文件“jfreechart”来查看具体的实现方式,这将有助于你更好地理解和应用这些解决方案。

    zysong.ttf jfreechart 解决中文乱码 问题

    本文将详细探讨这个问题及其解决方案,主要围绕“zysong.ttf”字体库在解决JFreeChart在Linux系统下的中文显示问题。 首先,我们需要理解JFreeChart是什么。JFreeChart是一个用Java编写的开源图表库,它提供了一套...

    JFreeChart乱码解决.doc

    JFreeChart是一款强大的Java图表库,它允许开发者创建多种类型的图表,...如果你遇到的特定问题仍未解决,可以搜索相关的解决方案,或者参考社区中的帖子,如文中提到的博客文章,那里通常会汇总各种场景下的解决方案。

    jfreechart乱码问题

    ### jfreechart乱码问题详解 #### 一、问题背景 在使用jFreeChart进行图表绘制时,可能会遇到中文乱码的问题。这个问题主要是由于jFreeChart默认使用的字体不支持中文字符,导致在图表中显示的中文无法正常识别,...

    jfreechart在LINUX下乱码问题的解决

    描述中的链接指向了一篇博客文章,虽然具体内容没有提供,但通常这类文章会详细阐述作者遇到的具体问题、尝试的解决方案以及最终的修复方法。可能包括代码示例、错误日志分析等。 标签中的“源码”可能暗示了解决...

    opensuse下如何解决jfreechart中文乱码的问题

    #### 解决方案 为了解决这个问题,我们需要手动添加中文字体文件到JRE中。下面是一步步的操作指南: 1. **获取中文字体文件** - 从Windows操作系统中找到一个名为`simsun.ttc`的字体文件。通常这个文件位于`C:\...

    linux 下 JFreeChart 乱码 JDK字符集

    3. **解决方案**: - **修改JFreeChart编码**:在生成图表时,可以通过设置`TextRenderer`的`charset`属性为`UTF-8`来改变JFreeChart的编码。 ```java DefaultCategoryAxis axis = (DefaultCategoryAxis) chart....

    JFreechart 在linux下不显示及中文乱码问题

    **解决方案** 1. **检查Java版本**:确认Java版本与JFreechart兼容,并更新到最新稳定版本。 2. **更新图形库**:如果必要,安装或更新系统的AWT和Swing库。 3. **修复环境变量**:确保`JAVA_HOME` 和 `PATH` ...

    JFreechart存储为PDF不能显示中文文字的解决办法

    《JFreechart生成PDF中文显示问题的解决方案》 在使用JFreechart库生成PDF文档时,经常遇到一个棘手的问题,即中文文字无法正常显示。这主要是由于JFreechart默认的字体映射策略不支持中文字符集导致的。本文将详细...

    Jfreechart

    本文将详细分析 Jfreechart 乱码的常见位置以及提供解决方案。 **一、乱码的位置** 1. **图的标题栏**:当图表的标题或副标题使用了不支持的字符集时,可能会出现乱码。 2. **图的内部**:这可能指的是图表的数据...

    jfreehchar在LINUX下中文乱码的解决

    以下是一份详细的解决方案,帮助你解决JFreeChart在Linux下的中文乱码问题。 首先,了解JFreeChart的编码机制。JFreeChart默认使用的是平台的默认字符集,而在Linux系统中,这个默认字符集可能不支持中文,因此会...

    ireport jfreechart 圖表 欄位 linux 亂碼問題1

    iReport JFreeChart 图表乱码问题解决方案 在使用 iReport 和 JFreeChart 生成报表时,可能会遇到图表中文乱码的问题, especially on Linux systems. 这篇文章将详细介绍这个问题的解决方案。 问题描述 当我们...

Global site tag (gtag.js) - Google Analytics