打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JFreeChart 中文乱码解决方法方案
 由于JFreeChart组件的版本、操作平台、JDK的设置等因素,在使用JFreeChart组件时可能会出现中文乱码的现象。遇到此问题时,可通过设置文字的字体来解决问题。在此提供以下两种解决此问题的方法。
一、设置主题的样式(强烈推荐)
  在制图前,创建主题样式并制定样式中的字体,通过ChartFactory的setChartTheme()方法设置主题样式。
Java代码
 
  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);  

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

二、制定乱码文字的字体
使用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))


下面一个实例:

Java代码
 
  1. package com.zzs.jfreechart.demo;   
  2.   
  3. 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,   
  •                 truetruetrue);   
  •         // 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);   
  •   
  •     }   
  •   
  • }  

  • 下面这个修改坐标轴:

    Java代码
     
    1. package com.zzs.jfreechart.demo;   
    2.   
    3. import java.awt.Color;   
    4. import java.awt.Font;   
    5.   
    6. import org.jfree.chart.ChartFactory;   
    7. import org.jfree.chart.ChartFrame;   
    8. import org.jfree.chart.JFreeChart;   
    9. import org.jfree.chart.axis.CategoryAxis;   
    10. import org.jfree.chart.axis.ValueAxis;   
    11. import org.jfree.chart.plot.XYPlot;   
    12. import org.jfree.chart.title.LegendTitle;   
    13. import org.jfree.chart.title.TextTitle;   
    14. import org.jfree.data.time.Month;   
    15. import org.jfree.data.time.TimeSeries;   
    16. import org.jfree.data.time.TimeSeriesCollection;   
    17. import org.jfree.ui.RectangleInsets;   
    18.   
    19. public class ShiJianXuLieTu01 {   
    20.   
    21.     /**  
    22.      * @param args  
    23.      */  
    24.     public static void main(String[] args) {   
    25.         // TODO Auto-generated method stub   
    26.         //时间序列图   
    27.             
    28.            TimeSeries timeseries = new TimeSeries("L&G European Index Trust",Month.class);   
    29.         
    30.            timeseries.add(new Month(22001), 181.8D);//这里用的是Month.class,同样还有Day.class Year.class 等等   
    31.         
    32.            timeseries.add(new Month(32001), 167.3D);   
    33.         
    34.            timeseries.add(new Month(42001), 153.8D);   
    35.         
    36.            timeseries.add(new Month(52001), 167.6D);   
    37.         
    38.            timeseries.add(new Month(62001), 158.8D);   
    39.         
    40.            timeseries.add(new Month(72001), 148.3D);   
    41.         
    42.            timeseries.add(new Month(82001), 153.9D);   
    43.         
    44.            timeseries.add(new Month(92001), 142.7D);   
    45.         
    46.            timeseries.add(new Month(102001), 123.2D);   
    47.         
    48.            timeseries.add(new Month(112001), 131.8D);   
    49.         
    50.            timeseries.add(new Month(122001), 139.6D);   
    51.         
    52.            timeseries.add(new Month(12002), 142.9D);   
    53.         
    54.            timeseries.add(new Month(22002), 138.7D);   
    55.         
    56.            timeseries.add(new Month(32002), 137.3D);   
    57.         
    58.            timeseries.add(new Month(42002), 143.9D);   
    59.         
    60.            timeseries.add(new Month(52002), 139.8D);   
    61.         
    62.            timeseries.add(new Month(62002), 137D);   
    63.         
    64.            timeseries.add(new Month(72002), 132.8D);   
    65.         
    66.         
    67.         
    68.            TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust曾召帅",Month.class);   
    69.         
    70.            timeseries1.add(new Month(22001), 129.6D);   
    71.         
    72.            timeseries1.add(new Month(32001), 123.2D);   
    73.         
    74.            timeseries1.add(new Month(42001), 117.2D);   
    75.         
    76.            timeseries1.add(new Month(52001), 124.1D);   
    77.         
    78.            timeseries1.add(new Month(62001), 122.6D);   
    79.         
    80.            timeseries1.add(new Month(72001), 119.2D);   
    81.         
    82.            timeseries1.add(new Month(82001), 116.5D);   
    83.         
    84.            timeseries1.add(new Month(92001), 112.7D);   
    85.         
    86.            timeseries1.add(new Month(102001), 101.5D);   
    87.         
    88.            timeseries1.add(new Month(112001), 106.1D);   
    89.         
    90.            timeseries1.add(new Month(122001), 110.3D);   
    91.         
    92.            timeseries1.add(new Month(12002), 111.7D);   
    93.         
    94.            timeseries1.add(new Month(22002), 111D);   
    95.         
    96.            timeseries1.add(new Month(32002), 109.6D);   
    97.         
    98.            timeseries1.add(new Month(42002), 113.2D);   
    99.         
    100.            timeseries1.add(new Month(52002), 111.6D);   
    101.         
    102.            timeseries1.add(new Month(62002), 108.8D);   
    103.         
    104.            timeseries1.add(new Month(72002), 101.6D);   
    105.         
    106.            TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();   
    107.         
    108.            timeseriescollection.addSeries(timeseries);   
    109.         
    110.             timeseriescollection.addSeries(timeseries1);   
    111.         
    112.            timeseriescollection.setDomainIsPointsInTime(true); //domain轴上的刻度点代表的是时间点而不是时间段   
    113.         
    114.            JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("合法 & General Unit Trust Prices",   
    115.         
    116.                   "日期",   
    117.         
    118.                   "暗示的话发神经提防",   
    119.         
    120.                   timeseriescollection,   
    121.         
    122.                   true,   
    123.         
    124.                   true,   
    125.         
    126.                   false);   
    127.         
    128.                   jfreechart.setBackgroundPaint(Color.white);   
    129.                   TextTitle textTitle = jfreechart.getTitle();   
    130.                 textTitle.setFont(new Font("宋体", Font.BOLD, 20));   
    131.                 LegendTitle legend = jfreechart.getLegend();   
    132.                 if (legend != null) {   
    133.                     legend.setItemFont(new Font("宋体", Font.BOLD, 20));   
    134.                 }   
    135.         
    136.                   XYPlot xyplot = (XYPlot)jfreechart.getPlot(); //获得 plot : XYPlot!!   
    137.                   ValueAxis domainAxis=xyplot.getDomainAxis();   
    138.                   domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体   
    139.                   domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的标题的字体   
    140.                      
    141.                   ValueAxis rangeAxis=xyplot.getRangeAxis();   
    142.                   rangeAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体   
    143.                   rangeAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体   
    144.         
    145.                   xyplot.setBackgroundPaint(Color.lightGray);   
    146.         
    147.                   xyplot.setDomainGridlinePaint(Color.white);   
    148.         
    149.                   xyplot.setRangeGridlinePaint(Color.white);   
    150.         
    151.                   xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));   
    152.         
    153.                   xyplot.setDomainCrosshairVisible(true);   
    154.         
    155.                   xyplot.setRangeCrosshairVisible(true);   
    156.         
    157.                   ChartFrame  frame=new ChartFrame ("折线图 ",jfreechart,true);   
    158.         
    159.                   frame.pack();   
    160.         
    161.                   frame.setVisible(true);   
    162.   
    163.   
    164.   
    165.     }   
    166.   
    167. }  
    本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
    打开APP,阅读全文并永久保存 查看更多类似文章
    猜你喜欢
    类似文章
    【热】打开小程序,算一算2024你的财运
    JFreeChart中文乱码和文字模糊问题的通用解决方案
    jfreechart热点图纯jsp无乱码版(饼图)
    struts2+hibernate整合jfreechart
    JFreeChart-饼状图-初级|http://www.sentom.net
    ChartDirector与JFreeChart两款主要web图表工具调研报告
    第一个JFreeChart实例
    更多类似文章 >>
    生活服务
    热点新闻
    分享 收藏 导长图 关注 下载文章
    绑定账号成功
    后续可登录账号畅享VIP特权!
    如果VIP功能使用有故障,
    可点击这里联系客服!

    联系客服