您的位置:首页 > 其它

JFreeChart柱状图的类的封装

2009-10-09 00:01 302 查看
根据公司数据分析的需求, 花了很长时间研究了JFreeChart生成柱状图的类,如下是一个通用生成柱状图的类。如有问题,可以留言或QQ: devid_pitoushi@Live.cn

package com.mycompany.helper.common.service;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;

/**
* 统计图表类(采用JFreeChart控件)
*/
public class StatChart {

/**
* 得到生成的图表的路径
* @throws IOException
* data: 用于显示的数据; rowKeys: (Y轴)图例;
* columnKeys: (X轴); totalTitle: 总标题;
* xTitle: x轴标题; yTitle: y轴标题; direction: 图显示的方向
* width: 宽; height: 高
*/
public static String getChartURL(HttpServletRequest request, HttpSession session,
double[][] data, String[] rowKeys, String[] columnKeys,
String totalTitle, String xTitle, String yTitle, String direction,
int width, int height)
throws IOException {
//数据集
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);

//图表的工厂
JFreeChart chart = null;
if(direction == "HORIZONTAL") {

//chart = ChartFactory.createBarChart(totalTitle, xTitle,
//yTitle, dataset, PlotOrientation.HORIZONTAL, true, true, false);
chart = ChartFactory.createBarChart3D(totalTitle, xTitle,
yTitle, dataset, PlotOrientation.HORIZONTAL, true, true, false);
} else {
chart = ChartFactory.createBarChart3D(totalTitle, xTitle,
yTitle, dataset, PlotOrientation.VERTICAL, true, true, true);

}
chart.setBackgroundPaint(Color.white);

//设置属性
setAttribute(chart);
//得到文件名
String filename = ServletUtilities.saveChartAsPNG(chart, width, height,
null, session);
String graphURL = request.getContextPath()
+ "/DisplayChart?filename=" + filename;
return graphURL;
}

/**
* 设置字体
*/
public static void setAttribute(JFreeChart chart) {
/*------------设置文字操作--------------*/
CategoryPlot plot = chart.getCategoryPlot();//获得图表区域对象
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(true);
plot.setDomainAxis(domainAxis);
ValueAxis rAxis = plot.getRangeAxis();

//设置标题的文字
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 14));
//设置X轴坐标上的文字
domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
//设置X轴的标题文字
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
//设置Y轴坐标上的文字
rAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
//设置Y轴的标题文字
rAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

//CategoryPlot plot = chart.getCategoryPlot();
//设置网格背景颜色
plot.setBackgroundPaint(Color.white);
//设置网格竖线颜色
plot.setDomainGridlinePaint(Color.decode("#799AE1"));
//设置网格横线颜色
plot.setRangeGridlinePaint(Color.decode("#799AE1"));
plot.setBackgroundAlpha(0.9f);
//显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseFillPaint(Color.decode("#799AE1"));
renderer.setMaximumBarWidth(0.03); //设置柱状图的width

//renderer.setItemLabelPaint(Color.decode("#799AE1"));
//默认的数字显示在柱子中,通过如下两句可调整数字的显示
//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
//设置每个地区所包含的平行柱的之间距离
//renderer.setItemMargin(0.3);
plot.setRenderer(renderer);
//设置地区、销量的显示位置
//将下方的“”放到上方
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
//将默认放在左边的“”放到右方
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
plot.setWeight(100);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: