JFreeChart Hacking <本文允许任意转载,请注明出处!最后一块骨头.2005-05-16> JFreeChart 是什么?我就不再重复介绍了,网上文章大把... 官方主页:http://www.jfree.org/jfreechart/index.html <--本文以jfreechart-1.0.0-pre2版本为基准--> 那么为什么要Hacking JFreeChart呢? 罪状一:中文Label显示模糊; 罪状二:无简体中文LocalizationBundle.properties文件; 罪状三:向下兼容性不好,官方说jdk1.2+,其实是jdk1.4+; 兼容问题A:另存菜单输出PNG文件只能运行于jdk1.4+; 兼容问题B:另存菜单不能输出JPEG格式图片; 兼容问题C:jdk1.3-环境下中文ToolTips不能正常显示; 如果一定要加一条罪状的话:目前开源免费的chart项目无出其右; 十分感谢JFreeChart开发者的辛苦努力!虽然它现在还不完美。 Ok,下面为Hacking做一些准备工作... 1.下载jfreechart-1.0.0-pre2.zip解压; 2.将\解压目录\source\下的源码加入任意project并配置jdk&lib; 3.安装一个有批量文件查询/替换功能的工具,比如UtralEdit; <本文允许任意转载,请注明出处!最后一块骨头.2005-05-16> Hacking Begin 1.解决中文Label显示模糊问题 原因分析:JFreeChart默认字体对中文的支持不完善; 解决步骤:搜索\解压目录\source\下含有"new Font("的文件; 发现JFreeChart默认字体为 "foo" "Serif" "Dialog" "SansSerif" "Bitstream Vera Sans" 批量替换他们为任意中文字体(推荐"黑体"); 当然你也可以把字体写入properties文件, 但这里介绍的是最简单的方法; 然后重新编译你修改过的文件,Ok,搞定! 2.无简体中文LocalizationBundle.properties文件 org\jfree\chart\LocalizationBundle.properties 主要用来显示右键功能菜单的文字,下面我们来汉化: 注意:LocalizationBundle.properties的编码不是Unicode, 简体中文要用GBK编码。 获得GBK编码的方法是: cd ***\j2sdk1.*.*\bin native2ascii -encoding GBK 1.txt 2.txt 注:1.txt放中文文本,2.txt输出GBK编码。 # org.jfree.chart.ChartPanel ResourceBundle properties file # # Changes (from 31-Aug-2003) # -------------------------- # 31-Aug-2003 : Initial version (AL); # 16-May-2005 : Add GBK version (FinalBone); # 16-May-2005 : Add JPG_Image_Files (FinalBone); #---------------EN---------------# #Auto_Range=Auto Range #All_Axes=Both Axes #Chart_Properties=Chart Properties #Copy=Copy #Print...=Print... #Save_as...=Save as... #Properties...=Properties... #PNG_Image_Files=PNG Image Files #JPG_Image_Files=JPG Image Files #Domain_Axis=Domain Axis #Range_Axis=Range Axis #Zoom_In=Zoom In #Zoom_Out=Zoom Out #---------------GBK---------------# Auto_Range=\u81ea\u52a8\u8c03\u6574 All_Axes=\u6240\u6709\u5750\u6807\u8f74 Chart_Properties=\u56fe\u8868\u5c5e\u6027 Copy=\u590d\u5236 Print...=\u6253\u5370... Save_as...=\u53e6\u5b58\u4e3a... Properties...=\u5c5e\u6027... PNG_Image_Files=PNG\u56fe\u50cf JPG_Image_Files=JPG\u56fe\u50cf Domain_Axis=\u57df\u5750\u6807\u8f74 Range_Axis=\u503c\u5750\u6807\u8f74 Zoom_In=\u653e\u5927 Zoom_Out=\u7f29\u5c0f 另外还有两个同名文件,希望大家有时间也汉化一下。 org\jfree\chart\ui\LocalizationBundle.properties org\jfree\chart\plot\LocalizationBundle.properties 3.兼容性问题 a.PNG输出问题 b.无JPEG输出菜单 原因分析:javax.imageio.ImageIO这个类只有jdk1.4+才有。 解决步骤:1.打开org.jfree.chart.ChartPanel, 查看doSaveAs()方法,了解其结构; 2.改写doSaveAs()方法,重新编译; 3.如果运行环境是jdk1.3- 打开org.jfree.chart.encoders.SunPNGEncoderAdapter 注释掉javax.imageio.ImageIO,重新编译。 /** * Updated by FinalBone 2005-5-16 PNG --> JPEG * * Opens a file chooser and gives the user an opportunity * to save the chart in JPEG format. * * @throws IOException if there is an I/O error. */ public void doSaveAs() throws IOException {
JFileChooser fileChooser = new JFileChooser(); ExtensionFileFilter filter = new ExtensionFileFilter( localizationResources.getString("JPG_Image_Files"), ".jpg" ); fileChooser.addChoosableFileFilter(filter); int option = fileChooser.showSaveDialog(this); if (option == JFileChooser.APPROVE_OPTION) { String filename = fileChooser.getSelectedFile().getPath(); if (isEnforceFileExtensions()) { if (!filename.endsWith(".jpg")) { filename = filename + ".jpg"; } } ChartUtilities.saveChartAsJPEG( new File(filename), this.chart, getWidth(), getHeight() ); } } c.中文ToolTips不能正常显示(显示为小方框) 原因分析:相关字体找不到。 解决步骤:1.打开org.jfree.chart.block.LabelBlock 查看其构造函数public LabelBlock(String label); 2.修改构造函数的字体定义,重新编译。 /** * Creates a new label block. * * @param label the label. */ public LabelBlock(String label) { /** * Updated by FinalBone 2005-05-16 * "Bitstream Vera Sans" --> "黑体" * For Chinese-Simplified ToolTips * JDK 1.3- Environment */ this(label, new Font("黑体", Font.PLAIN, 11)); } Hacking End <本文允许任意转载,请注明出处!最后一块骨头.2005-05-16> 这时的 JFreeChart 才是真正jdk1.2+兼容,完美支持中文。 希望下一版本的 JFreeChart 的字体定义写入properties文件, 并且不同路经下的properties文件最好不要重名。 本人接触 JFreeChart 刚刚几天,错漏难免,请多多指正! <本文允许任意转载,请注明出处!最后一块骨头.2005-05-16> 
|