JFreeChart中文乱码解决方案

来源:互联网 发布:手机局域网限速软件 编辑:程序博客网 时间:2024/05/04 18:49

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

二、制定乱码文字的字体

使用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));
  4. }

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

下面这个修改坐标轴:

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

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

【编辑推荐】

  1. Java最佳图形解决方案 JFreeChart学习总结
  2. Java精确截取字符串
  3. Java I/O系统基础知识
  4. Java 远程文件对接
  5. 解读Java环境变量配置
原创粉丝点击