JfreeChart 解決中文亂碼問題

2021-05-24 00:32:09 字數 1604 閱讀 6627

中文亂碼的解決方案

由於jfreechart元件的版本、操作平台、jdk的設定等因素,在使用jfreechart元件時可能會出現中文亂碼的現象。遇到此問題時,可通過設定亂碼文字的字型來解決。在此提供以下兩種解決此問題的方法。

設定主題樣式

在製圖前,建立主題樣式並指定樣式中的字型,通過chartfactory的setcharttheme()方法設定主題樣式。在指定製圖樣式後,chartfactory物件建立的製圖物件將按此樣式進行顯示,圖表中的文字將按指定的字型進行顯示。

例如:

//建立主題樣式 

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

通過上述**設定主題樣式後,再通過chartfactory建立jfreechart的物件,即可解決中文亂碼問題。

指定亂碼文字的字型

此方法通過指定製圖物件中的中文字型來解決中文亂碼問題。在圖中任何用到中文的地方,都要對字型進行設定。此操作將涉及到jfreechart物件、plot物件、座標軸物件的屬性設定。

例如:

jfreechart chart = 

null

;  //省略部分**

//設定標題字型

chart.gettitle().setfont(

new font(

"隸書"

,font.bold,

25));  

//設定圖例類別字型

chart.getlegend().setitemfont(

new font(

"宋體"

,font.bold,

15));  

//獲得繪圖區物件

pieplot plot = (pieplot) chart.getplot();  

//設定分類標籤的字型

plot.setlabelfont(

new font(

"宋體"

,font.plain,

12)); 

不同的製圖物件型別,其相關字型的設定可能存在差異,對於各物件中所用到的字型設定,可參閱24.2節或jfreechart元件的api文件。

解決JfreeChart中文亂碼問題

jfreechart中文亂碼問題一直困擾很多的朋友,我學習得時候也一樣,下面幾段 可以用來解決這個問題。texttitle texttitle chart.gettitle texttitle.setfont new font 宋體 font.bold,20 legendtitle legend c...

Ubuntu Rhythmbox 解決中文亂碼

在這裡介紹的是乙個解決方法,修改變數。在終端輸入 gedit profile 在最後加入下面內容 exportgst id3 tag encoding gbk utf 8 gb18030 exportgst id3v2 tag encoding gbk utf 8 gb18030 儲存退出。sour...

解決 Flask sqlalchemy 中文亂碼

當在使用 flask sqlalchemy 操作 mysql 資料庫時中文亂碼或者直接報錯。解決方法如下 from flask sqlalchemy import sqlalchemy db sqlalchemy use native unicode utf8 配置 mysql 預設字元編碼為 ut...