jfreechart使用及其向linux移植

类别:Java 点击:0 评论:0 推荐:

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

 

本文地址:http://com.8s8s.com/it/it12728.htm