JAVA-WEB项目Jfreechart树状图显示在jsp中

第一步 创建一个webjfreechart项目

数据库编写

prompt PL/SQL Developer import file
prompt Created on 2012年12月12日 by Administrator
set feedback off
set define off
prompt Creating BONUS...
create table BONUS
(
  ENAME VARCHAR2(10),
  JOB   VARCHAR2(9),
  SAL   NUMBER,
  COMM  NUMBER
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );


prompt Creating DEPT...
create table DEPT
(
  DEPTNO NUMBER(2) not null,
  DNAME  VARCHAR2(14),
  LOC    VARCHAR2(13)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
alter table DEPT
  add constraint PK_DEPT primary key (DEPTNO)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );


prompt Creating EMP...
create table EMP
(
  EMPNO    NUMBER(4) not null,
  ENAME    VARCHAR2(10),
  JOB      VARCHAR2(9),
  MGR      NUMBER(4),
  HIREDATE DATE,
  SAL      NUMBER(7,2),
  COMM     NUMBER(7,2),
  DEPTNO   NUMBER(2)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
alter table EMP
  add constraint PK_EMP primary key (EMPNO)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
alter table EMP
  add constraint FK_DEPTNO foreign key (DEPTNO)
  references DEPT (DEPTNO);


prompt Creating SALGRADE...
create table SALGRADE
(
  GRADE NUMBER,
  LOSAL NUMBER,
  HISAL NUMBER
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );


prompt Disabling triggers for BONUS...
alter table BONUS disable all triggers;
prompt Disabling triggers for DEPT...
alter table DEPT disable all triggers;
prompt Disabling triggers for EMP...
alter table EMP disable all triggers;
prompt Disabling triggers for SALGRADE...
alter table SALGRADE disable all triggers;
prompt Disabling foreign key constraints for EMP...
alter table EMP disable constraint FK_DEPTNO;
prompt Loading BONUS...
prompt Table is empty
prompt Loading DEPT...
insert into DEPT (DEPTNO, DNAME, LOC)
values (10, 'ACCOUNTING', 'NEW YORK');
insert into DEPT (DEPTNO, DNAME, LOC)
values (20, 'RESEARCH', 'DALLAS');
insert into DEPT (DEPTNO, DNAME, LOC)
values (30, 'SALES', 'CHICAGO');
insert into DEPT (DEPTNO, DNAME, LOC)
values (40, 'OPERATIONS', 'BOSTON');
commit;
prompt 4 records loaded
prompt Loading EMP...
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7369, 'SMITH', 'CLERK', 7902, to_date('17-12-1980', 'dd-mm-yyyy'), 800, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7499, 'ALLEN', 'SALESMAN', 7698, to_date('20-02-1981', 'dd-mm-yyyy'), 1600, 300, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7521, 'WARD', 'SALESMAN', 7698, to_date('22-02-1981', 'dd-mm-yyyy'), 1250, 500, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7566, 'JONES', 'MANAGER', 7839, to_date('02-04-1981', 'dd-mm-yyyy'), 2975, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7654, 'MARTIN', 'SALESMAN', 7698, to_date('28-09-1981', 'dd-mm-yyyy'), 1250, 1400, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7698, 'BLAKE', 'MANAGER', 7839, to_date('01-05-1981', 'dd-mm-yyyy'), 2850, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7782, 'CLARK', 'MANAGER', 7839, to_date('09-06-1981', 'dd-mm-yyyy'), 2450, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7788, 'SCOTT', 'ANALYST', 7566, to_date('19-04-1987', 'dd-mm-yyyy'), 3000, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981', 'dd-mm-yyyy'), 5000, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7844, 'TURNER', 'SALESMAN', 7698, to_date('08-09-1981', 'dd-mm-yyyy'), 1500, 0, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7876, 'ADAMS', 'CLERK', 7788, to_date('23-05-1987', 'dd-mm-yyyy'), 1100, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7900, 'JAMES', 'CLERK', 7698, to_date('03-12-1981', 'dd-mm-yyyy'), 950, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7902, 'FORD', 'ANALYST', 7566, to_date('03-12-1981', 'dd-mm-yyyy'), 3000, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7934, 'MILLER', 'CLERK', 7782, to_date('23-01-1982', 'dd-mm-yyyy'), 1300, null, 10);
commit;
prompt 14 records loaded
prompt Loading SALGRADE...
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (1, 700, 1200);
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (2, 1201, 1400);
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (3, 1401, 2000);
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (4, 2001, 3000);
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (5, 3001, 9999);
commit;
prompt 5 records loaded
prompt Enabling foreign key constraints for EMP...
alter table EMP enable constraint FK_DEPTNO;
prompt Enabling triggers for BONUS...
alter table BONUS enable all triggers;
prompt Enabling triggers for DEPT...
alter table DEPT enable all triggers;
prompt Enabling triggers for EMP...
alter table EMP enable all triggers;
prompt Enabling triggers for SALGRADE...
alter table SALGRADE enable all triggers;
set feedback on
set define on
prompt Done.

项目中集成sturts hibernate框架

导入项目中 jfreechart jar 包  



创建处理pie的工具类

package action;


import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;


import org.hibernate.Session;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;


import antlr.CharFormatter;


import commons.HibernateSessionFactory;
import entity.Dept;
import entity.Emp;


public class PieChart {


private  DefaultPieDataset getDataset() {
Session session = HibernateSessionFactory.getSession();
List<Dept> depts = session.createQuery("from Dept").list();
DefaultPieDataset pd = new DefaultPieDataset();
for (Dept dept : depts) {
pd.setValue(dept.getDname(), dept.getEmps().size());
}
HibernateSessionFactory.closeSession();
return pd;


}


public  JFreeChart makePieChart3D() {


String title = "department total";


DefaultPieDataset pd = getDataset();


JFreeChart chart = ChartFactory.createPieChart3D(title, pd, true,
false, false);


Font font = new Font("Times New Roman", Font.BOLD, 25);


TextTitle textTitle = new TextTitle(title);// chart.getTitle();


textTitle.setFont(font);


chart.setTitle(textTitle);


chart.setBackgroundPaint(new Color(255, 255, 255));


LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("Times New Roman", 1, 15));


PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("Times New Roman", Font.TRUETYPE_FONT, 12));


plot.setForegroundAlpha(0.55f);
// ͼƬ����ʾ�ٷֱ�:�Զ��巽ʽ��{0} ��ʾѡ� {1} ��ʾ��ֵ�� {2} ��ʾ��ռ���� ,С������λ
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// ͼ����ʾ�ٷֱ�:�Զ��巽ʽ�� {0} ��ʾѡ� {1} ��ʾ��ֵ�� {2} ��ʾ��ռ����
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
"{0} ({2})"));
// ���õ�һ�������濪ʼ��λ�ã�Ĭ����12���ӷ���
plot.setStartAngle(90);

return chart;

}

}创建action struts 的处理类

package action;


import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;


import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;


import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;


import com.opensymphony.xwork2.ActionSupport;


public class ShowChartAction extends ActionSupport {

private JFreeChart deptChart;



public JFreeChart getDeptChart() {
return deptChart;
}


public void setDeptChart(JFreeChart deptChart) {
this.deptChart = deptChart;
}


private InputStream imgInputStream;
public InputStream getImgInputStream() {

return imgInputStream;
}


public void setImgInputStream(InputStream imgInputStream) {
this.imgInputStream = imgInputStream;
}


@Override
public String execute() throws Exception {

PieChart pc=new PieChart();
deptChart=pc.makePieChart3D();

/*

  //BufferedImage image  = dchart.createBufferedImage(300, 300);
  
   ByteArrayInputStream input = null;
ByteArrayOutputStream output = new ByteArrayOutputStream();
//ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
//ImageIO.write(image, "JPEG", imageOut);
ChartUtilities.writeChartAsJPEG(output, dchart, 300, 300);
//imageOut.close();
input = new ByteArrayInputStream(output.toByteArray());
setImgInputStream(input);

*/
return SUCCESS;
}





}


配置struts 文件


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   
<package name="chart" extends="jfreechart-default">
 <result-types>
  //注意这里是使用自己定义的chart 的名称  自己创建的结果但这个继承了 chartresult   当你调用chart 时你必须要按照他的规范写返回的类型  
   <result-type name="mychart" class="result.MyChartResult"></result-type>
 </result-types>
 <action name="showChart" class="action.ShowChartAction">
   <result type="mychart">  
   <param name="chartName">deptChart</param>    
     <param name="width">400</param>
     <param name="height">400</param>      
   </result>
 
 </action>

</package>

</struts>    

//自己创建的结果返回


package result;


import org.apache.struts2.dispatcher.ChartResult;
import org.jfree.chart.JFreeChart;


import com.opensymphony.xwork2.ActionContext;


public class MyChartResult extends ChartResult {

private String chartName;


public String getChartName() {
return chartName;
}


public void setChartName(String chartName) {
this.chartName = chartName;
setChart((JFreeChart) ActionContext.getContext().getValueStack().findValue(chartName, JFreeChart.class));
 System.out.println(getChart());

}



}



编写前台jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
  sdfdsf
    <img alt="char" src="showChart" />
  </body>
</html>





你可能感兴趣的:(JAVA-WEB项目Jfreechart树状图显示在jsp中)