使用JFreeCharts画一些折线图、柱形图、饼图

package cn.qiuzhping.jfreecharts;


import java.awt.Color;
import java.awt.Font;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


import org.hibernate.Session;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;
import org.jfree.util.Rotation;


import www.huarui.biz.hibernate.Eventlevel;
import www.huarui.biz.hibernate.Eventrange;
import www.huarui.biz.hibernate.Eventtype;
import www.huarui.biz.hibernate.HibernateUtil;


public class Charts {
private StandardChartTheme standardChartTheme = null;// 标准的主题样式
private static Session session = null;


private int typeId[];


public int[] getTypeId() {
return typeId;
}


public void setTypeId(int[] typeId) {
this.typeId = typeId;
}


public Charts() {
System.out.println("--------正常启动Charts服务------" + new Date());
this.standardChartTheme = new StandardChartTheme("CN");
}


@SuppressWarnings("unchecked")
public List getList(String hql) {
session = HibernateUtil.getSession();
if (hql != null && session != null) {
session.beginTransaction();
List list = session.createQuery(hql).list();
session.close();
return list;
}
return null;
}


public String[] getRowKeys(String filter) {
int[] typeId = getTypeId();
if (filter != null && !filter.trim().equals("") && typeId != null) {
String[] keys = new String[typeId.length];
if (filter.equals("eventtype")) {
for (int i = 0; i < typeId.length; i++) {
Eventtype type = (Eventtype) HibernateUtil.get(
Eventtype.class, typeId[i]);
if (type != null)
keys[i] = type.getName();
}
} else if (filter.equals("eventlevel")) {
for (int i = 0; i < typeId.length; i++) {
Eventlevel type = (Eventlevel) HibernateUtil.get(
Eventlevel.class, typeId[i]);
if (type != null)
keys[i] = type.getName();
}
} else if (filter.equals("eventrange")) {
for (int i = 0; i < typeId.length; i++) {
Eventrange type = (Eventrange) HibernateUtil.get(
Eventrange.class, typeId[i]);
if (type != null)
keys[i] = type.getName();
}
}
return keys;
}


return new String[] {};
}


@SuppressWarnings("unchecked")
public double[][] getData(List list) {
if (list != null && list.size() > 0) {
int size = list.size();
double[][] data = new double[size][1];
int typeId[] = new int[size];
double[] count = new double[size];
double[] buff;
int index = 0;
for (Iterator iterator = list.iterator(); iterator.hasNext(); index++) {
Object[] obj = (Object[]) iterator.next();
try {
typeId[index] = Integer.parseInt(obj[0].toString());// 显示列id
count[index] = Double.parseDouble(obj[1].toString());// 显示列统计值
} catch (Exception e) {
System.out.println("类型转换异常");
typeId[index] = 0;
count[index] = 0;
}
}
setTypeId(typeId);
for (int i = 0; i < count.length; i++) {// 给data赋值,只给行赋值
buff = new double[] { count[i] };
data[i] = buff;
}
return data;
}
return new double[][] {};
}


public String handerHql(Date start, Date end, String filter) {
if (filter != null && !filter.trim().equals("")) {
StringBuffer hql = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
if (filter.equals("eventtype")) {
hql = new StringBuffer("SELECT ca.typeid");
hql.append(",COUNT(ca) from Case ca");
if (start != null && end != null) {
hql.append(" WHERE ca.time BETWEEN");
hql.append(" " + format.format(start));
hql.append(" AND " + format.format(end));
}
hql.append(" GROUP BY ca.typeid");
} else if (filter.equals("eventlevel")) {
hql = new StringBuffer("SELECT ca.levelid");
hql.append(",COUNT(ca) from Case ca");
if (start != null && end != null) {
hql.append(" WHERE ca.time BETWEEN");
hql.append(" " + format.format(start));
hql.append(" AND " + format.format(end));
}
hql.append(" GROUP BY ca.levelid");
} else if (filter.equals("eventrange")) {
hql = new StringBuffer("SELECT ca.rangeid");
hql.append(",COUNT(ca) from Case ca");
if (start != null && end != null) {
hql.append(" WHERE ca.time BETWEEN");
hql.append(" " + format.format(start));
hql.append(" AND " + format.format(end));
}
hql.append(" GROUP BY ca.rangeid");
}
return hql.toString();
}
return null;
}


public String getPieKeysName(String filter, int id) {
if (filter != null && !filter.trim().equals("")) {
if (filter.equals("eventtype")) {
Eventtype type = (Eventtype) HibernateUtil.get(Eventtype.class,
id);
if (type != null)
return type.getName();
} else if (filter.equals("eventlevel")) {
Eventlevel type = (Eventlevel) HibernateUtil.get(
Eventlevel.class, id);
if (type != null)
return type.getName();
} else if (filter.equals("eventrange")) {
Eventrange type = (Eventrange) HibernateUtil.get(
Eventrange.class, id);
if (type != null)
return type.getName();
}
}
return null;
}


@SuppressWarnings("unchecked")
public DefaultPieDataset getPieData(List list, String filter) {
if (list != null && filter != null && list.size() > 0) {
int id;
double count = 0;
String name = null;
DefaultPieDataset dataset = new DefaultPieDataset();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Object[] obj = (Object[]) iterator.next();
try {
id = Integer.parseInt(obj[0].toString());// 显示列id
name = getPieKeysName(filter, id);
count = Double.parseDouble(obj[1].toString());// 显示列统计值
dataset.setValue(name, count);
} catch (Exception e) {
System.out.println("类型转换异常");
id = 0;
count = 0;
}
}
return dataset;
}
return new DefaultPieDataset();
}


@SuppressWarnings("unchecked")//开始时间,结束时间,类型 饼图
public JFreeChart getPieChart(Date start, Date end, String filter) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String scope = "";
if (start != null && end != null) {
String startTime = format.format(start);
String endTime = format.format(end);
scope = startTime + " 至 " + endTime;
}
String hql = handerHql(start, end, filter);
String title = null;
if (filter != null && !filter.trim().equals("")) {
if (filter.equals("eventtype"))
title = "事件类型分布统计图";
else if (filter.equals("eventlevel"))
title = "事件紧急程度分布统计图";
else if (filter.equals("eventrange"))
title = "事件涉及范围分布统计图";
}
List list = getList(hql);
DefaultPieDataset dataset = getPieData(list, filter);// 设置饼图数据集
standardChartTheme.setExtraLargeFont(new Font("微软雅黑", Font.BOLD, 18));
// 设置图例的字体
standardChartTheme.setRegularFont(new Font("微软雅黑", Font.PLAIN, 14));
// 设置轴向的字体
standardChartTheme.setLargeFont(new Font("微软雅黑", Font.PLAIN, 16));
// 应用主题样式
ChartFactory.setChartTheme(standardChartTheme);
// 通过工厂类生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true,
true, false);
chart.addSubtitle(new TextTitle(scope));
PiePlot pieplot = (PiePlot) chart.getPlot();
pieplot.setLabelFont(new Font("宋体", 0, 14));
StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator(
"{0}({1},{2})", NumberFormat.getNumberInstance(), NumberFormat
.getPercentInstance());
pieplot.setBackgroundPaint(Color.white);//设置背景
pieplot.setLabelGenerator(standarPieIG);


// 没有数据的时候显示的内容
pieplot.setNoDataMessage("无数据显示");
pieplot.setLabelGap(0.02D);


PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot();
// 设置开始角度
pieplot3d.setStartAngle(120D);
// 设置方向为”顺时针方向“
pieplot3d.setDirection(Rotation.CLOCKWISE);
// 设置透明度,0.5F为半透明,1为不透明,0为全透明
pieplot3d.setForegroundAlpha(0.7F);
return chart;
}


@SuppressWarnings( { "unchecked", "deprecation" })//开始时间,结束时间,类型  柱形图
public JFreeChart getColumnChart(Date start, Date end, String filter) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String scope = "";
if (start != null && end != null) {
String startTime = format.format(start);
String endTime = format.format(end);
scope = startTime + " 至 " + endTime;
}
String hql = handerHql(start, end, filter);
String title = null;
if (filter != null && !filter.trim().equals("")) {
if (filter.equals("eventtype"))
title = "事件类型分布统计图";
else if (filter.equals("eventlevel"))
title = "事件紧急程度分布统计图";
else if (filter.equals("eventrange"))
title = "事件涉及范围分布统计图";
}
List list = getList(hql);
double[][] data = getData(list);// 数据是二维数组(我只需要将行初始化便可)
String[] rowKeys = getRowKeys(filter);
String[] columnKeys = {};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
standardChartTheme.setExtraLargeFont(new Font("微软雅黑", Font.BOLD, 18));
// 设置图例的字体
standardChartTheme.setRegularFont(new Font("微软雅黑", Font.PLAIN, 14));
// 设置轴向的字体
standardChartTheme.setLargeFont(new Font("微软雅黑", Font.PLAIN, 16));
// 应用主题样式
ChartFactory.setChartTheme(standardChartTheme);
JFreeChart chart = ChartFactory.createBarChart3D(title, "", "数量",
dataset, PlotOrientation.VERTICAL, true, true, false);
chart.addSubtitle(new TextTitle(scope));// 设置标题
CategoryPlot plot = chart.getCategoryPlot();
// 设置网格背景颜色
plot.setBackgroundPaint(Color.white);
// 设置网格竖线颜色
plot.setDomainGridlinePaint(Color.pink);
// 设置网格横线颜色
plot.setRangeGridlinePaint(Color.pink);


plot.setNoDataMessage("暂无数据显示!");// 没有数据显示的时候显示这个提示
// 显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer = new BarRenderer3D();
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);


// 默认的数字显示在柱子中,通过如下两句可调整数字的显示
// 注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelFont(new Font("黑体", Font.PLAIN, 18));// 设置数字的字体大小
renderer.setItemLabelAnchorOffset(10D);


// 设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.3);
plot.setRenderer(renderer);
return chart;
}
}

你可能感兴趣的:(java,饼图,折线图,柱形图,JFreecharts)