JFreeChart中文乱码解决方案

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

由于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); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

例如:

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) {  
   
//     创建类别图(Category)数据对象  
   
       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, truetruetrue);  
//        TextTitle textTitle = chart.getTitle();  
//      textTitle.setFont(new Font("宋体", Font.BOLD, 20));  
//      LegendTitle legend = chart.getLegend();  
//      if (legend != null) {  
//          legend.setItemFont(new Font("宋体", Font.BOLD, 20));  
//      }  
       ChartFrame  frame=new ChartFrame ("水果产量图 ",chart,true);  
       frame.pack();  
       frame.setVisible(true);  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.

二、制定乱码文字的字体

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

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

1.Title

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

2.Legent

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

  • 1.
  • 2.
  • 3.
  • 4.

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轴坐标上的标题的字体 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

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

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

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

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

下面一个实例:
 

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,  
                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);  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.

下面这个修改坐标轴:

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 {  
 
    /**  
     * @param args  
     */ 
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        //时间序列图  
           TimeSeries timeseries = new TimeSeries("L&G European Index Trust",Month.class);  
           timeseries.add(new Month(22001), 181.8D);//这里用的是Month.class,同样还有Day.class Year.class 等等  
           timeseries.add(new Month(32001), 167.3D);  
           timeseries.add(new Month(42001), 153.8D);  
           timeseries.add(new Month(52001), 167.6D);  
           timeseries.add(new Month(62001), 158.8D);  
           timeseries.add(new Month(72001), 148.3D);  
           timeseries.add(new Month(82001), 153.9D);  
           timeseries.add(new Month(92001), 142.7D);  
           timeseries.add(new Month(102001), 123.2D);  
           timeseries.add(new Month(112001), 131.8D);  
           timeseries.add(new Month(122001), 139.6D);  
           timeseries.add(new Month(12002), 142.9D);  
           timeseries.add(new Month(22002), 138.7D);  
           timeseries.add(new Month(32002), 137.3D);  
           timeseries.add(new Month(42002), 143.9D);  
           timeseries.add(new Month(52002), 139.8D);  
           timeseries.add(new Month(62002), 137D);  
           timeseries.add(new Month(72002), 132.8D);  
           TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust曾召帅",Month.class);  
       
           timeseries1.add(new Month(22001), 129.6D);  
           timeseries1.add(new Month(32001), 123.2D);  
           timeseries1.add(new Month(42001), 117.2D);  
           timeseries1.add(new Month(52001), 124.1D);  
           timeseries1.add(new Month(62001), 122.6D);   
           timeseries1.add(new Month(72001), 119.2D);  
           timeseries1.add(new Month(82001), 116.5D);  
           timeseries1.add(new Month(92001), 112.7D);  
           timeseries1.add(new Month(102001), 101.5D);  
           timeseries1.add(new Month(112001), 106.1D);  
           timeseries1.add(new Month(122001), 110.3D);  
           timeseries1.add(new Month(12002), 111.7D);  
           timeseries1.add(new Month(22002), 111D);  
           timeseries1.add(new Month(32002), 109.6D);  
           timeseries1.add(new Month(42002), 113.2D);  
           timeseries1.add(new Month(52002), 111.6D);  
           timeseries1.add(new Month(62002), 108.8D);  
           timeseries1.add(new Month(72002), 101.6D);  
           TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();  
       
           timeseriescollection.addSeries(timeseries);  
            timeseriescollection.addSeries(timeseries1);  
            timeseriescollection.setDomainIsPointsInTime(true); //domain轴上的刻度点代表的是时间点而不是时间段  
           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(); //获得 plot : XYPlot!!  
                  ValueAxis domainAxis=xyplot.getDomainAxis();  
                  domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体  
                  domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的标题的字体  
                  ValueAxis rangeAxis=xyplot.getRangeAxis();  
                  rangeAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体  
                  rangeAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体  
                  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);  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.

原文链接: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中文乱码

2024-11-08 13:47:35

中文乱码配置

2009-07-24 11:24:33

ASP.NET中文乱码

2011-12-20 12:53:43

JavaJFreeChart

2010-05-12 16:47:54

MySQL 中文乱码

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-05-17 14:17:25

MySQL pytho

2010-05-31 12:38:48

Nagios中文

2011-03-29 14:35:34

2010-03-12 18:22:51

Python文本乱码

2010-05-27 12:49:30

MySQL中文乱码

2011-12-22 12:37:17

JavaJFreeChart

2010-05-04 09:34:18

Oracle em

2010-08-06 09:42:39

2018-12-03 12:26:30

YADRO解决方案

2018-12-03 12:13:21

Mellanox解决方案

2018-12-03 11:59:42

Inventec解决方案
点赞
收藏

51CTO技术栈公众号