JFreeChart亂碼解決方法
文章分類:Java編程
整個圖標分成三部分chart title,chart 的plot還有chart的 legend三個部分需要對他們分別設置字體就對了。
先看解決方法( 把這幾個全部設置了,都搞定了就可以了):
標題亂碼
chart.getTitle().setFont(new Font("宋體", Font.BOLD,12));
其他
- CategoryAxis domainAxis = plot.getDomainAxis();
- // NumberAxis valueAxis=(NumberAxis) plot.getRangeAxis();
- //有人說這個是水平方向設置的 方法。
- ValueAxis numberaxis = plot.getRangeAxis();
- /*------設置X軸座標上的文字-----------*/
- domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
- /*------設置X軸的標題文字------------*/
- domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12));
- /*------設置Y軸座標上的文字-----------*/
- numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
- /*------設置Y軸的標題文字------------*/
- numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));
- /*------這句代碼解決了底部漢字亂碼的問題-----------*/
- jfreechart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12));
對於曲線圖;
用於下面兩種方法得到的來設置設置水平的和垂直的方法是不一樣的。
JFreeChart chart = ChartFactory.createTimeSeriesChart("", "時間", "價格", lineDataset, true, true, true);
XYPlot plot = (XYPlot) chart.getPlot();
垂直的:
ValueAxis valueaxis=plot.getDomainAxis();
valueaxis.setLabelFont(new Font("宋體",Font.BOLD,12));
valueaxis.setTickLabelFont(new Font("宋體",Font.BOLD,12));
水平的:
NumberAxis valueAxis=(NumberAxis) plot.getRangeAxis();
valueAxis.setLabelFont(new Font("宋體",Font.BOLD,12));
JFreeChart jfreechart = ChartFactory.createLineChart("‘大豆’別按小時計算拆線圖", "時間", "價格", categoryDataset,PlotOrientation.VERTICAL, true, false,false);
CategoryPlot plot = (CategoryPlot) jfreechart.getPlot();
CategoryAxis domainaxis=plot.getDomainAxis();
水平的:
domainaxis.setLabelFont(new Font("宋體",Font.BOLD,20));
domainaxis.setTickLabelFont(new Font("宋體", Font.PLAIN, 12));
垂直的:
NumberAxis valueAxis=(NumberAxis) plot.getRangeAxis();
valueAxis.setLabelFont(new Font("宋體",Font.BOLD,20));
上面的是針對柱狀圖的,下面的是 設置餅狀圖的。
標題:chart.setTitle(new TextTitle("我的標題",new Font("宋體",Font.BOLD,20)));
圖例: LegendTitle legendtitle=chart.getLegend(0);
legendtitle.setItemFont(new Font("我的標題",Font.ITALIC,20));
餅上面的文字:
PiePlot plot=(PiePlot)chart.getPlot();
plot.setLabelFont(new Font("宋體",Font.BOLD,20));
原因:
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);
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/kinble/archive/2009/05/06/4155085.aspx
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字體的地方都替換成中文字體在重新編譯,來個一勞永逸,我沒有試,不知可不可以,我主要採用了重新設置字體的方法。