JFreeChart 案例1
来源:互联网 发布:演奏音乐的软件 编辑:程序博客网 时间:2024/06/03 20:02
JFreeChart 是一个非常流行的一个免费开源的图表软件
已经有11年以上的历史
首先要下载jar包
www.jfree.org
在http://www.jfree.org/jfreechart 下点击Project Page at SourceForge 去下载
要下载JFreeChart 与JCommon
做JFreeChart的时候,一般是以下三步
1:要有数据 比如DefaultPieDataset
2:根据数据生成 JFreeChart 对象
3:显示JFreeChart对象(显示在swing中或生成一个图片在jsp中显示)
主要是用JFreeChart做的饼图和柱状图,用到了struts2
1.Action类(一个处理部门的饼图,一个地市柱形图):
public class DeptPieChartAction extends ActionSupport {/** * Comment for <code>serialVersionUID</code> */private static final long serialVersionUID = 1L;private JFreeChart chart; private Date startTime;private String startTimeStr; @Override public String execute() throws Exception { if(startTime != null){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); startTimeStr = sdf.format(startTime); startTimeStr = startTimeStr + "-01 00:00:00"; } TJByChart tc = new TJByChart(); chart = tc.createDeptPieChart(startTimeStr); return SUCCESS; } public JFreeChart getChart() { return chart; }public void setChart(JFreeChart chart) {this.chart = chart;}public Date getStartTime() {return startTime;}public void setStartTime(Date startTime) {this.startTime = startTime;}public String getStartTimeStr() {return startTimeStr;}public void setStartTimeStr(String startTimeStr) {this.startTimeStr = startTimeStr;} }
public class RNumBarChartAction extends ActionSupport {/** * Comment for <code>serialVersionUID</code> */private static final long serialVersionUID = 1L;private JFreeChart chart; @Override public String execute() throws Exception { TJByChart tc = new TJByChart(); chart = tc.createReceiveCaseBarChart(); return SUCCESS; } public JFreeChart getChart() { return chart; }public void setChart(JFreeChart chart) {this.chart = chart;} }
2.service 类:
import java.awt.Font;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import org.apache.struts2.ServletActionContext;import org.jfree.chart.ChartFactory;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.CategoryLabelPositions;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PiePlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.category.BarRenderer3D;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.data.general.DefaultPieDataset;import org.jfree.data.general.PieDataset;import org.jfree.data.jdbc.JDBCPieDataset;import com.metarnet.cssf.common.PropertiesParser;import com.metarnet.cssf.common.UserManager;import com.metarnet.cssf.common.db.ConnectionPool;import com.metarnet.cssf.model.User;/* * 图形统计表-统计概览 * */public class TJByChart {private User user = UserManager.getInstance().getUserBySessionId(ServletActionContext.getRequest().getRequestedSessionId());//接单/驳单统计public JFreeChart createReceiveCaseBarChart(){CategoryDataset dataset = initReceiveCaseBarDate(); Calendar cal = Calendar.getInstance();cal.add(Calendar.MONTH, -1);String name = cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH) + 1) + "月"; JFreeChart chart = ChartFactory.createBarChart3D( name + "处理/驳回统计", // 图表标题 "地市", "数量", dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) true, // 是否生成工具 true // 是否生成URL链接 ); chart.getTitle().setFont(new Font("隶书",Font.BOLD,18));chart.getLegend().setItemFont(new Font("宋体",Font.BOLD, 14));chart.setBorderVisible(true);CategoryPlot plot = chart.getCategoryPlot();plot.setForegroundAlpha(0.8F);plot.setBackgroundAlpha(0.5F);CategoryAxis categoryAxis = plot.getDomainAxis();categoryAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));categoryAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);ValueAxis valueAxis = plot.getRangeAxis();valueAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));BarRenderer3D renderer = new BarRenderer3D();renderer.setItemMargin(0.32);plot.setRenderer(renderer); return chart; }private CategoryDataset initReceiveCaseBarDate(){DefaultCategoryDataset dataSet = new DefaultCategoryDataset();Connection conn = null;Statement stat = null;ResultSet rs = null;try {String sql = "select city_id,city_name,isreject,count(*) as cnum from cssf_ts_statistics where 1 = 1 ";if(user.getCityId() > 0){//省份sql = sql + " and city_id = " + user.getCityId();}//时间Calendar cal = Calendar.getInstance();cal.add(Calendar.MONTH, -1);cal.set(Calendar.DAY_OF_MONTH, 1);cal.set(Calendar.HOUR_OF_DAY, 0);cal.set(Calendar.MINUTE, 0);cal.set(Calendar.SECOND, 0);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String start = sdf.format(cal.getTime());cal.add(Calendar.MONTH, 1);String end = sdf.format(cal.getTime());sql = sql + " and sponsor_time >= to_date('" + start + "','yyyy-mm-dd hh24:mi:ss') and sponsor_time < to_date('"+ end + "','yyyy-mm-dd hh24:mi:ss') group by city_id,city_name,isreject order by city_id";conn = ConnectionPool.getInstance().getConnection("cssf");stat = conn.createStatement();rs = stat.executeQuery(sql);Map<String, int[]> retMap = new LinkedHashMap<String, int[]>();while(rs.next()){String cityname = rs.getString("city_name");int cityId = rs.getInt("city_id");int isreject = rs.getInt("isreject");int num = rs.getInt("cnum");if(retMap.containsKey(cityname)){int[] arr = retMap.get(cityname);if(isreject == 1){arr[1] = num;}else{arr[0] = num;}arr[2] = cityId;}else{int[] arr = new int[3];if(isreject == 1){arr[1] = num;}else{arr[0] = num;}arr[2] = cityId;retMap.put(cityname, arr);}}Iterator<String> iter = retMap.keySet().iterator();while(iter.hasNext()){String cityName = iter.next();int[] arr = retMap.get(cityName);dataSet.addValue(arr[0], "处理", cityName);dataSet.addValue(arr[1], "驳回", cityName);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try { rs.close(); } catch (Exception e) { }try { stat.close(); } catch (Exception e) { }try { conn.close(); } catch (Exception e) { }}return dataSet;}//创建处理部门统计饼图public JFreeChart createDeptPieChart(String startTime){Calendar cal = Calendar.getInstance();cal.add(Calendar.MONTH, -1);String name = cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH) + 1) + "月";JFreeChart chart = ChartFactory.createPieChart3D(name + "投诉处理部门统计图", initDeptPieData(), true, true, false);chart.getTitle().setFont(new Font("隶书",Font.BOLD,18));chart.getLegend().setItemFont(new Font("宋体",Font.BOLD, 14));chart.setBorderVisible(true);PiePlot plot = (PiePlot) chart.getPlot();plot.setForegroundAlpha(0.5f);plot.setLabelFont(new Font("宋体", Font.PLAIN, 12));plot.setCircular(true);//设置分类标签的格式plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}",new DecimalFormat("0.00%"),new DecimalFormat("0.00%")));return chart;}//加载处理部门统计数据private PieDataset initDeptPieData(){JDBCPieDataset dataset = null;PropertiesParser prop = new PropertiesParser();try {dataset = new JDBCPieDataset(prop.getProperty("cssf.url"),prop.getProperty("cssf.driver"),prop.getProperty("cssf.user"),prop.getProperty("cssf.password"));//select duty_dept,count(*) from cssf_ts_statistics group by duty_deptString sql = "select duty_dept,count(*) from cssf_ts_statistics where 1 = 1 and duty_dept is not null";if(user.getCityId() > 0){//省份sql = sql + " and city_id = " + user.getCityId();}//时间Calendar cal = Calendar.getInstance();cal.add(Calendar.MONTH, -1);cal.set(Calendar.DAY_OF_MONTH, 1);cal.set(Calendar.HOUR_OF_DAY, 0);cal.set(Calendar.MINUTE, 0);cal.set(Calendar.SECOND, 0);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String start = sdf.format(cal.getTime());cal.add(Calendar.MONTH, 1);String end = sdf.format(cal.getTime());sql = sql + " and sponsor_time >= to_date('" + start + "','yyyy-mm-dd hh24:mi:ss') and sponsor_time < to_date('"+ end + "','yyyy-mm-dd hh24:mi:ss') group by duty_dept";System.out.println("dept: " + sql);dataset.executeQuery(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if(dataset != null) dataset.close();}return dataset;}}3.struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="jFreeChartPkg" extends="struts-default" namespace="/jfcpkg"><result-types><result-type name="chart"class="org.apache.struts2.dispatcher.ChartResult"></result-type></result-types><action name="deptPieChart" class="com.metarnet.cssf.action.report.common.complaint.DeptPieChartAction"><result type="chart"><param name="width">330</param><param name="height">200</param></result></action><action name="rnumBarChart" class="com.metarnet.cssf.action.report.common.complaint.RNumBarChartAction"><result type="chart"><param name="width">1020</param><param name="height">350</param></result></action></package></struts>
4.jsp
<body> <div align="center"> <img src="jfcpkg/deptPieChart.action"> <hr> <img src="jfcpkg/rnumBarChart.action"> </div> </body>
5.需要的JAR包
jcommon-1.0.17.jar,jfreechart-1.0.14.jar;这两个是必须的,当用到struts2时,还需用到struts2-jfreechart-plugin-2.3.14.jar
下载地址:http://download.csdn.net/detail/lifuxiangcaohui/5729185
- JFreeChart 案例1
- jFreeChart案例
- jfreechart 应用讲解,案例
- 首次接触jfreechart案例分析
- JFreeChart详细使用说明,附案例
- JFreeChart 1
- JFreeChart(1)
- jfreechart总结1
- 初涉JFreeChart (1)
- Struts2.1之Jfreechart
- JFreeChart教程(1)
- JFreeChart学习1 ---饼状图
- jfreeChart---学习1
- JFreeChart(一.柱状图.1)
- JFreeChart(二.柱状图.1)
- JFreeChart(三.饼状图.1)
- (1)JfreeChart之柱状图
- 1、JFreeChart简单入门
- 动态调用DLL函数有时正常,有时报Access violation的异常
- Oracle数据库编程:PL/SQL编程基础
- GUI系统之SurfaceFlinger(9)Project Butter黄油计划
- poj 2342 Anniversary party
- Linux 多线程应用中编写安全的信号处理函数
- JFreeChart 案例1
- Poj_1088_滑雪(DP)
- FFMPEG SDK 开发介绍 -- Filter 篇
- 用贪心算法解决金钱兑换问题(基于JAVA)
- jquery 控制 multiple select 所有选择 且 排序
- 链栈的实现
- equals 和 hashcode 方法
- 【学习笔记】《如何构建敏捷项目管理团队》第五章 教练是导师
- linux目录中递归删除同名文件夹