使用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);
}
}
好像不太智能 ,但数据很少 或数据很多时,使用不太理想,哪位大侠找到了!共享一下!