jfreechart使用及其向linux移植

來源:互聯網
上載者:User

jfreechart

jfreechart是一個免費(文檔收費40$)建立圖片的java工具.可以建立如形:
餅圖(pie charts;)
曲線圖(line charts )
柱狀圖(horizontal/vertical bar charts)
甘特圖(Gantt charts; )
XY plots and scatter plots;
time series, high/low/open/close charts and candle stick charts;
combination charts;
Pareto charts;
bubble charts;
wind plots, meter charts and symbol charts;

必看的幾個貼:

http://www-900.ibm.com/developerWorks/cn/wsdd/library/techarticles/yangyaping0307/waslinux.shtml

http://www.lslnet.com/linux/docs/linux-2940.htm

http://blog.blogchina.com/article_81038.344426.html

在向linux移植的時候會顯示不出中文,出現方塊。

解決方案:copy或引用/usr/share/fonts/zh_CN/TrueType目錄下的中文字型,修改/home/jdk/jre/lib/fonts.properties

設定檔,如下:

sansserif.0=-misc-ZYSong18030-medium-r-normal--*-%d-*-*-c-*-iso10646-1

sansserif.italic.0=-misc-ZYSong18030-medium-r-normal--*-%d-*-*-c-*-iso10646-1

sansserif.bold.0=-misc-ZYSong18030-medium-r-normal--*-%d-*-*-c-*-iso10646-1

sansserif.bolditalic.0=-misc-ZYSong18030-medium-r-normal--*-%d-*-*-c-*-iso10646-1

# Component Font Character Encodings
#
fontcharset.serif.0=sun.io.CharToByteISO8859_1
fontcharset.serif.1=sun.awt.motif.CharToByteX11GBK

fontcharset.sansserif.0=sun.io.CharToByteISO8859_1
fontcharset.sansserif.1=sun.awt.motif.CharToByteX11GBK

fontcharset.monospaced.0=sun.io.CharToByteISO8859_1
fontcharset.monospaced.1=sun.awt.motif.CharToByteX11GBK

fontcharset.dialog.0=sun.io.CharToByteISO8859_1
fontcharset.dialog.1=sun.awt.motif.CharToByteX11GBK

fontcharset.dialoginput.0=sun.io.CharToByteISO8859_1
fontcharset.dialoginput.1=sun.awt.motif.CharToByteX11GBK

fontset.sansserif.plain=/
-misc-ZYSong18030-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.sansserif.italic=/
-misc-ZYSong18030-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.sansserif.bold=/
-misc-ZYSong18030-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.sansserif.bolditalic=/
-misc-ZYSong18030-medium-r-normal--*-%d-*-*-c-*-iso10646-1

fontset.default=/
-misc-ZYSong18030-medium-r-normal--*-%d-*-*-c-*-iso10646-1

appendedfontpath=/usr/share/fonts/zh_CN/TrueType

然後對自己基於jfreechart寫的程式進行重新編譯、運行:

javac -encoding GBK   BarChartDemo.java //以GBK進行編碼
java -Djava.awt.headless=true BarChartDemo//使用awt時不用調用x11的圖形環境

 

在tomcat使用jfreechart時,

tomcat是5.0,在redhat8上,未啟動X,方法如下:
1)終止你的tomcat。即:
tomcat目錄/bin/shutdown.sh
2)設定環境變數:
CATALINA_OPTS="-Djava.awt.headless=true"
export CATALINA_OPTS
(如果你想每次開機自動生效,則可把這兩句寫入系統或者你的帳號啟動sh的.profile裡)
3)啟動你的tomcat。即:
tomcat目錄/bin/startup.sh

用的Web伺服器resin時,

修改resin/bin/下面的wrapper.pl中的一行
$JAVA_ARGS="-Djava.awt.headless=true";

 

==============================================================

最近項目中用到jfreechart,使用的版本是:jfreechart-0.9.8。
如果不進行相關的font的設定,產生的統計圖表顯示的中文非常模糊。
做了一個例子,可以解決這個問題。

[該方法是將一般字型替代為“黑體”使中文更加清楚,

因此必須保證你的OS上有該字型,並且jdk能夠識別得到]

核心代碼如下:

JFreeChart chart = ChartFactory.createVerticalBarChart3D(title, domain, range, dataset,true,true,false);

chart.setBackgroundPaint(new GradientPaint(0.0F, 0.0F, Color.white, 1000F, 0.0F, Color.red));
chart.setTitle(new TextTitle(title, new Font("隸書", Font.ITALIC, 15)));

Font font=new Font("黑體",Font.TRUETYPE_FONT, 12);

StandardLegend legend = (StandardLegend) chart.getLegend();
legend.setItemFont(font);

CategoryPlot plot = (CategoryPlot)chart.getPlot();
plot.setForegroundAlpha(0.9F);

CategoryAxis domain_axis = plot.getDomainAxis();
domain_axis.setTickLabelFont(font);

ValueAxis value_axis=plot.getRangeAxis();
value_axis.setTickLabelFont(font);

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.