JFreeChart(一.柱状图.1)

来源:互联网 发布:360截图软件 编辑:程序博客网 时间:2024/04/19 18:06
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;


public class BarChart1 {
    //主要是返回图片的名称
    public static String getBarChart(HttpSession session)throws Exception{
//构造数据集
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.addValue(510, "苏州", "苹果");
dataset.addValue(510, "苏州", "香蕉");
dataset.addValue(510, "苏州", "橘子");
dataset.addValue(510, "苏州", "梨子");
//创建图片
JFreeChart chart=ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, true);

String filename=ServletUtilities.saveChartAsPNG(chart,700,500,null,session);
return filename;
    }

}

//*********************************************************************************************

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.ygl.chart.bar.BarChart1;"%>
<%
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 'barChart.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>
  <%


String filename= BarChart1.getBarChart(session);
  System.out.println(filename);
   %>

//此处参数"?filename"filename为底层写好的名字
   <img src="DisplayChart?filename=<%=filename %>" width="700" height="500" border="0"/>
  </body>
</html>

//*********************************************************************************

//需要调用org.jfree.chart.servlet.DisplayChart生成图片

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="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-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
  
</web-app>.



0 0
原创粉丝点击