使用JFreeChart 輸出柱狀圖 基本參數設定:
package com.*****************.domain.util;
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
/**
* 格式化 JFreeChart 輸出圖檔
*
*
* Author :
* Date : Nov 26, 2009
* Time : 11:50:41 AM
* Version: 1.0
*/
public class FormatPic {
/**
* 格式化縱向柱狀圖使用
*
* @param chart
* @returnType: void
* @author:
* @data: Nov 26, 2009
* @time: 11:51:26 AM
*/
public static void setView(JFreeChart chart){
// 初始化字型
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
Font noFont = new Font("SansSerif", Font.TRUETYPE_FONT, 48);
// 格式化 圖檔
CategoryPlot plot = chart.getCategoryPlot();
// 沒有資料是顯示的消息
plot.setNoDataMessage("沒有資料!");
// 沒有資料時顯示的消息字型
plot.setNoDataMessageFont(noFont);
// 沒有資料時顯示的消息顔色
plot.setNoDataMessagePaint(Color.RED);
// 資料軸精度
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// 資料軸資料标簽的顯示格式
CategoryAxis domainAxis = plot.getDomainAxis();
// 設定刻度必須從0開始
vn.setAutoRangeIncludesZero(true);
// 設定縱坐标資料精度
// DecimalFormat df = new DecimalFormat("#0.00");
// vn.setNumberFormatOverride(df);
// x軸設定
domainAxis.setLabelFont(labelFont);// 軸标題
domainAxis.setTickLabelFont(labelFont);// 軸數值
// y軸設定
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
BarRenderer renderer = new BarRenderer();
// 設定柱子寬度
renderer.setMaximumBarWidth(0.05);
// 設定柱子高度
// renderer.setMinimumBarLength(0.2);
// 設定柱子邊框顔色
renderer.setBaseOutlinePaint(Color.BLACK);
// 設定距離圖檔左端距離
domainAxis.setLowerMargin(0.01);
// 設定距離圖檔右端距離
// domainAxis.setUpperMargin(0.2);
// 設定顯示位置
// plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
// plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setDomainAxis(domainAxis);
// 設定柱圖背景色(注意,系統取色的時候要使用16位的模式來檢視顔色編碼,這樣比較準确)
plot.setBackgroundPaint(new Color(255, 255, 204));
// 設定每個平行柱之間距離
renderer.setItemMargin(0.05);
// 顯示每個柱的數值,并修改該數值的字型屬性
renderer.setIncludeBaseInRange(true);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
plot.setRenderer(renderer);
}
}
好像不太智能 ,但資料很少 或資料很多時,使用不太理想,哪位大俠找到了!共享一下!