java 从零开始,学习笔记之基础入门<统计图>(四十四)
2014-02-25 11:41
831 查看
统计图(JFreeChart)
JSP内嵌代码单独实现
(在web.xml配置如下代码)
Servlet实现:
Servlet中的代码
Struts实现:
Web.xml
JSP内嵌代码单独实现
(在web.xml配置如下代码)
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/servlet/DisplayChart</url-pattern> </servlet-mapping> |
Index.jsp |
<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%> <%@pageimport="org.jfree.data.category.*"%> <%@pageimport="org.jfree.chart.JFreeChart"%> <%@pageimport="org.jfree.chart.ChartFactory"%> <%@pageimport="org.jfree.chart.plot.PlotOrientation"%> <%@pageimport="org.jfree.chart.servlet.ServletUtilities"%> <%@pageimport="org.jfree.chart.title.TextTitle"%> <%@pageimport="java.awt.Font"%> <%@pageimport="org.jfree.chart.axis.CategoryAxis"%> <%@pageimport="org.jfree.chart.plot.CategoryPlot"%> <%@pageimport="org.jfree.chart.axis.ValueAxis"%> <% // request.setCharacterEncoding("utf-8"); // response.setCharacterEncoding("utf-8"); // // //数据结果集 // DefaultCategoryDataset dataset = new DefaultCategoryDataset(); // dataset.addValue(200,"广州","苹果"); // dataset.addValue(100,"广州","梨子"); // // //创建JFreeChart // JFreeChart chart = ChartFactory.createBarChart("水果销售统计图","水果","销售",dataset,PlotOrientation.VERTICAL,false,false,false); // // //生成一张图片,以供页面中的img标签使用 // String pngName=ServletUtilities.saveChartAsPNG(chart,500,300,null,session); // System.out.println("图片名称"+pngName); // String grapgURL=request.getContextPath()+"/servlet/DisplayChart?filename="+pngName; //第二种写法 //注意在web.xml文件中进行配置 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(300, "广州","苹果"); dataset.addValue(200, "广州","梨子"); dataset.addValue(500, "广州","葡萄"); dataset.addValue(340, "广州","芒果"); dataset.addValue(280, "广州","荔枝"); JFreeChart chart = ChartFactory.createBarChart3D("水果销量统计图", "水果",//x轴名称 "销量",//y轴名称 dataset, // 绘图数据集 PlotOrientation.VERTICAL, // 柱形图绘制方向 false,// 显示图例 false,// 显示图例名称 false);// 生成链接 TextTitle textTitle=chart.getTitle(); textTitle.setFont(new Font("黑体",Font.PLAIN,20)); //对图像区域的字体进行设置 CategoryPlot plot=chart.getCategoryPlot(); //横向图像与对象 CategoryAxis domainAxis=plot.getDomainAxis(); domainAxis.setVisible(true); //plot.setDomainAxes(domainAxis); //横向坐标 domainAxis.setTickLabelFont(new Font("黑体",Font.PLAIN,15)); //横向标题 domainAxis.setLabelFont(new Font("黑体",Font.PLAIN,20)); //纵向图像域对象 ValueAxis rAxis=plot.getRangeAxis(); //纵向坐标 rAxis.setTickLabelFont(new Font("黑体",Font.PLAIN,20)); //纵向标题 rAxis.setLabelFont(new Font("黑体",Font.PLAIN,15)); String filename =ServletUtilities.saveChartAsJPEG(chart, 500, 300,null, session); //String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; %> <!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="This is my page"> <!-- <link rel="stylesheet" type="text/css"href="styles.css"> --> </head> <body> This is my JSP page. <br> <imgsrc="<%=graphURL%>"/> </body> </html> |
Servlet实现:
Servlet中的代码
ChartAction.java | |
package com.ibm.action; import java.awt.Font; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; public class ChartAction extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); //指定数据结果集对象 DefaultCategoryDataset dataset = getDataSet(); //利用ChartFactory创建一个图标对象 并指定默认格式 JFreeChart chart = ChartFactory.createBarChart3D("水果销量统计图", "水果",//x轴名称 "销量",//y轴名称 dataset, // 绘图数据集 PlotOrientation.VERTICAL,// 柱形图绘制方向 false,// 显示图例 false,// 显示图例名称 false);// 生成链接 TextTitle textTitle=chart.getTitle(); textTitle.setFont(new Font("黑体",Font.PLAIN,20)); //对图像区域的字体进行设置 CategoryPlot plot=chart.getCategoryPlot(); //横向图像与对象 CategoryAxis domainAxis=plot.getDomainAxis(); domainAxis.setVisible(true); //plot.setDomainAxes(domainAxis); //横向坐标 domainAxis.setTickLabelFont(new Font("黑体",Font.PLAIN,15)); //横向标题 domainAxis.setLabelFont(new Font("黑体",Font.PLAIN,20)); //纵向图像域对象 ValueAxis rAxis=plot.getRangeAxis(); //纵向坐标 rAxis.setTickLabelFont(new Font("黑体",Font.PLAIN,20)); //纵向标题 rAxis.setLabelFont(new Font("黑体",Font.PLAIN,15)); //创建一张图片 //图片流数据最终将在客户端img标签中被捕获到 ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 0.99f, chart, 600, 450, null); } /** * * 获取一个简单数据集对象 * */ private static DefaultCategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(300, "广州","苹果"); dataset.addValue(200, "广州","梨子"); dataset.addValue(500, "广州","葡萄"); dataset.addValue(340, "广州","芒果"); dataset.addValue(280, "广州","荔枝"); return dataset; } } 页面部分
|
Struts实现:
Web.xml
Web.xml |
<?xmlversion="1.0"encoding="UTF-8"?> <web-appversion="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ChartAction</servlet-name> <servlet-class>com.ibm.action.ChartAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/servlet/DisplayChart</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ChartAction</servlet-name> <url-pattern>/ChartAction</url-pattern> </servlet-mapping> <filter> <filter-name>filterDispatcher</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>filterDispatcher</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
Struts.xml |
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEstruts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <packagename="chartpackage"extends="struts-default"> <!-- 视图类型声明 --> <result-types> <result-typename="chart"class="org.apache.struts2.dispatcher.ChartResult"></result-type> </result-types> <actionname="chart"class="com.ibm.action.ChartOperAction"> <!-- 以图表的方式展示结果 --> <resultname="success"type="chart"> <paramname="width">400</param> <paramname="height">300</param> </result> </action> </package> </struts> |
ChartOperAction.java | |
package com.ibm.action; import java.awt.Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; public class ChartOperAction { //注意名称 JFreeChart chart; public String execute(){ //指定数据结果集对象 DefaultCategoryDataset dataset = getDataSet(); //利用ChartFactory创建一个图标对象(创建饼状图) // chart=new JFreeChart("Moderation Function",JFreeChart.DEFAULT_TITLE_FONT,new PiePlot(data),false); //利用ChartFactory创建一个图标对象 并指定默认格式(创建柱状图) chart = ChartFactory.createBarChart3D("水果销量统计图", "水果",//x轴名称 "销量",//y轴名称 dataset, // 绘图数据集 PlotOrientation.VERTICAL,// 柱形图绘制方向 false,// 显示图例 false,// 显示图例名称 false);// 生成链接 TextTitle textTitle=chart.getTitle(); textTitle.setFont(new Font("黑体",Font.PLAIN,20)); //对图像区域的字体进行设置 CategoryPlot plot=chart.getCategoryPlot(); //横向图像与对象 CategoryAxis domainAxis=plot.getDomainAxis(); domainAxis.setVisible(true); //plot.setDomainAxes(domainAxis); //横向坐标 domainAxis.setTickLabelFont(new Font("黑体",Font.PLAIN,15)); //横向标题 domainAxis.setLabelFont(new Font("黑体",Font.PLAIN,20)); //纵向图像域对象 ValueAxis rAxis=plot.getRangeAxis(); //纵向坐标 rAxis.setTickLabelFont(new Font("黑体",Font.PLAIN,20)); //纵向标题 rAxis.setLabelFont(new Font("黑体",Font.PLAIN,15)); return "success"; } /** * * 获取一个简单数据集对象 * */ private static DefaultCategoryDataset getDataSet() { //创建柱状图结果集 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(300, "广州","苹果"); dataset.addValue(200, "广州","梨子"); dataset.addValue(500, "广州","葡萄"); dataset.addValue(340, "广州","芒果"); dataset.addValue(280, "广州","荔枝"); //饼状图结果集 // DefaultPieDataset dataset = new DefaultPieDataset(); // dataset.setValue("苹果", 100); // dataset.setValue("梨子", 200); // dataset.setValue("葡萄", 300); // dataset.setValue("香蕉", 400); // dataset.setValue("荔枝", 500); return dataset; } public JFreeChart getChart() { returnchart; } } 页面部分
|
相关文章推荐
- java 从零开始,学习笔记之基础入门<SQL_Server_视图_函数_存储过程_触发器等>(二十三)
- java 从零开始,学习笔记之基础入门<线程及实例分析>(十九)
- java 从零开始,学习笔记之基础入门<SQL_Server>(二十一)
- java 从零开始,学习笔记之基础入门<Hibernate_配置详解>(三十六)
- java 从零开始,学习笔记之基础入门<对象_属性>(二)
- java 从零开始,学习笔记之基础入门<集合>(十六)
- java 从零开始,学习笔记之基础入门<封装类>(十一)
- java 从零开始,学习笔记之基础入门<IO流>(十八)
- java 从零开始,学习笔记之基础入门<Date类_Calendar类>(十七)
- java 从零开始,学习笔记之基础入门<Oracle_查询>(三十四)
- java 从零开始,学习笔记之基础入门<内部类>(十四)
- java 从零开始,学习笔记之基础入门<Struts2_实战演练(上)>(三十八)
- java 从零开始,学习笔记之基础入门<网络编程_带QQ模拟功能>(二十)
- java 从零开始,学习笔记之基础入门<tomcat>(二十五)
- java 从零开始,学习笔记之基础入门<集合>(十六)
- java 从零开始,学习笔记之基础入门<Struts2_实战演练(下)>(四十)
- java 从零开始,学习笔记之基础入门<Struts2_Spring_整合>(四十一)
- java 从零开始,学习笔记之基础入门<正则表达式>(三十一)
- java 从零开始,学习笔记之基础入门<信息的隐藏>(四)
- java 从零开始,学习笔记之基础入门<Switch>(八)