JFreeChart中文亂碼解決方案
由于JFreeChart組件的版本、操作平臺、JDK的設置等因素,在使用JFreeChart組件時可能會出現(xiàn)中文亂碼的現(xiàn)象。遇到此問題時,可通過設置文字的字體來解決問題。在此提供以下兩種解決此問題的方法。
一、設置主題的樣式(強烈推薦)
在制圖前,創(chuàng)建主題樣式并制定樣式中的字體,通過ChartFactory的setChartTheme()方法設置主題樣式。
- //創(chuàng)建主題樣式
- 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);
例如:
- 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) {
- // 創(chuàng)建類別圖(Category)數(shù)據(jù)對象
- 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, "廣州", "荔枝");
- //創(chuàng)建主題樣式
- 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("水果產(chǎn)量圖", "水果", "水果", dataset, PlotOrientation.VERTICAL, true, true, true);
- // 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 ("水果產(chǎn)量圖 ",chart,true);
- frame.pack();
- frame.setVisible(true);
- }
- }
二、制定亂碼文字的字體
使用JFreeChart繪制圖表的時候,如果使用默認的字體會導致圖標中的漢字顯示為亂碼。解決方法如下:
JFreeChart是用戶使用該庫提供的各類圖標的統(tǒng)一接口,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的不同的實現(xiàn)類,設置字體的方法也不完全相同。
對于使用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的圖標(如餅狀圖):
- PiePlot plot = (PiePlot)freeChart.getPlot();
- plot.setLabelFont(new Font("宋體",Font.BOLD,15));
下面一個實例:
- 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);
- }
- // 利用靜態(tài)方法設定數(shù)據(jù)源(餅狀圖)
- public static PieDataset createPieDataset() {
- DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
- defaultpiedataset.setValue("管理人員", 10.02D);
- defaultpiedataset.setValue("市場人員", 20.23D);
- defaultpiedataset.setValue("開發(fā)人員", 60.02D);
- defaultpiedataset.setValue("OEM人員", 10.02D);
- defaultpiedataset.setValue("其他人員", 5.11D);
- return defaultpiedataset;
- }
- // 通過ChartFactory創(chuàng)建JFreeChart的實例
- public static JFreeChart createJFreeChart(PieDataset p)
- {
- JFreeChart a = ChartFactory.createPieChart("CityInfoPort公司組織架構圖", p,
- true, true, true);
- // 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);
- }
- }
下面這個修改坐標軸:
- 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(2, 2001), 181.8D);//這里用的是Month.class,同樣還有Day.class Year.class 等等
- timeseries.add(new Month(3, 2001), 167.3D);
- timeseries.add(new Month(4, 2001), 153.8D);
- timeseries.add(new Month(5, 2001), 167.6D);
- timeseries.add(new Month(6, 2001), 158.8D);
- timeseries.add(new Month(7, 2001), 148.3D);
- timeseries.add(new Month(8, 2001), 153.9D);
- timeseries.add(new Month(9, 2001), 142.7D);
- timeseries.add(new Month(10, 2001), 123.2D);
- timeseries.add(new Month(11, 2001), 131.8D);
- timeseries.add(new Month(12, 2001), 139.6D);
- timeseries.add(new Month(1, 2002), 142.9D);
- timeseries.add(new Month(2, 2002), 138.7D);
- timeseries.add(new Month(3, 2002), 137.3D);
- timeseries.add(new Month(4, 2002), 143.9D);
- timeseries.add(new Month(5, 2002), 139.8D);
- timeseries.add(new Month(6, 2002), 137D);
- timeseries.add(new Month(7, 2002), 132.8D);
- TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust曾召帥",Month.class);
- timeseries1.add(new Month(2, 2001), 129.6D);
- timeseries1.add(new Month(3, 2001), 123.2D);
- timeseries1.add(new Month(4, 2001), 117.2D);
- timeseries1.add(new Month(5, 2001), 124.1D);
- timeseries1.add(new Month(6, 2001), 122.6D);
- timeseries1.add(new Month(7, 2001), 119.2D);
- timeseries1.add(new Month(8, 2001), 116.5D);
- timeseries1.add(new Month(9, 2001), 112.7D);
- timeseries1.add(new Month(10, 2001), 101.5D);
- timeseries1.add(new Month(11, 2001), 106.1D);
- timeseries1.add(new Month(12, 2001), 110.3D);
- timeseries1.add(new Month(1, 2002), 111.7D);
- timeseries1.add(new Month(2, 2002), 111D);
- timeseries1.add(new Month(3, 2002), 109.6D);
- timeseries1.add(new Month(4, 2002), 113.2D);
- timeseries1.add(new Month(5, 2002), 111.6D);
- timeseries1.add(new Month(6, 2002), 108.8D);
- timeseries1.add(new Month(7, 2002), 101.6D);
- TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
- timeseriescollection.addSeries(timeseries);
- timeseriescollection.addSeries(timeseries1);
- timeseriescollection.setDomainIsPointsInTime(true); //domain軸上的刻度點代表的是時間點而不是時間段
- JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("合法 & General Unit Trust Prices",
- "日期",
- "暗示的話發(fā)神經(jīng)提防",
- 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);
- }
- }
原文鏈接:http://zengzhaoshuai.iteye.com/blog/1000378
【編輯推薦】