JFreeChart亂碼解決方法

jfreechart主要是用來動態產生各種數據圖形的,可最初使用的時候大都會碰到圖片中的中文亂碼或是一個小方塊的情況。
仔細研究主要有以下2種原因:

1:服務器缺少中文字體,這多發生在Hp等unix操作系統上,解決的方法就是下載可用字體庫到系統中,
有人也提出在Windows上產生圖片在傳回到Unix主機上的方法。
2:軟件版本問題,jfreechart-1.0.10有人說沒有問題,但jfreechart-1.0.11到13都有問題,我用的最新的jfreechart-1.0.13不做設置是有問題的。
究其原因,是它代碼的內部設置的字體有問題.
先來跟蹤一下它的代碼:

JFreeChart chart = ChartFactory.createBarChart(
   "數據統計圖",
   "設備號",
   "積累值",
   dataset,
   PlotOrientation.VERTICAL,
   true, true, false
   );
它的原型
public static JFreeChart createBarChart(String title,
                                            String categoryAxisLabel,
                                            String valueAxisLabel,
                                            CategoryDataset dataset,
                                            PlotOrientation orientation,
                                            boolean legend,
                                            boolean tooltips,
                                            boolean urls) {
上面的原型又調用了
   JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
                plot, legend);
currentTheme.apply(chart);
看看缺省字體的定義:
public static final Font DEFAULT_TITLE_FONT
            = new Font("SansSerif ", Font.BOLD, 18);
看看當前主題currentTheme是什麼
private static ChartTheme currentTheme = new StandardChartTheme("JFree");
看它的原型定義
public StandardChartTheme(String name) {
        if (name == null) {
            throw new IllegalArgumentException("Null 'name' argument.");
        }
        this.name = name;
        this.extraLargeFont = new Font("Tahoma ", Font.BOLD, 20);
        this.largeFont = new Font("Tahoma ", Font.BOLD, 14);
        this.regularFont = new Font("Tahoma", Font.PLAIN, 12);
        this.smallFont = new Font("Tahoma ", Font.PLAIN, 10);
……
看到了吧,默認的標題字體是SansSerif,在很多中文系統中是沒有這種字體的,這可能是用老外開發開源產品的弊端吧。
首先說標題的亂碼吧:
public JFreeChart(String title, Font titleFont, Plot plot,
                      boolean createLegend) {
……
對標題設置的代碼:
if (title != null) {
            if (titleFont == null) {
                titleFont = DEFAULT_TITLE_FONT;
            }
            this.title = new TextTitle(title, titleFont);
            this.title.addChangeListener(this);
        }
它使用了默認字體,因此要解決這個問題只要,對標題重新設置字體就可以了。
……
TextTitle textTitle = chart.getTitle();
       textTitle.setFont(new Font("黑體", Font.PLAIN, 20));    
圖例和其它亂碼一樣處理,更換字體。
CategoryPlot plot = chart.getCategoryPlot();//獲得圖表區域對象
    CategoryAxis domainAxis = plot.getDomainAxis();
       domainAxis.setVisible(true);
       plot.setDomainAxis(domainAxis);
ValueAxis rAxis = plot.getRangeAxis();
/*------設置X軸座標上的文字-----------*/
       domainAxis.setTickLabelFont(new Font("宋體",Font.PLAIN,15));
       /*------設置X軸的標題文字------------*/
       domainAxis.setLabelFont(new Font("宋體",Font.PLAIN,15));        
       /*------設置Y軸座標上的文字-----------*/
       rAxis.setTickLabelFont(new Font("宋體",Font.PLAIN,15));
       /*------設置Y軸的標題文字------------*/
       rAxis.setLabelFont(new Font("黑體",Font.PLAIN,15));
這裏需要注意的是,哪裏出現了亂碼就修改哪裏的字體,將字體轉換爲系統有的就可以了。
另外有人提出將jfreechart源文件裏面的涉及到SansSerif 字體的地方都替換成中文字體在重新編譯,來個一勞永逸,我沒有試,不知可不可以,我主要採用了重新設置字體的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章