一个数据统计图在程序
发布网友
发布时间:2022-09-29 19:07
我来回答
共1个回答
热心网友
时间:2024-11-27 04:26
下面我要做的是一个饼状统计图,首先建立javaBean文件
文件名:Tryitshow.java
package com.tryit;
import java.io.*;
import java.awt.Insets;
import java.awt.Font;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Locale;
import java.text.NumberFormat;
import javax.servlet.http.HttpSession;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.renderer.VerticalBarRenderer;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.plot.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.tooltips.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.servlet.*;
import org.jfree.chart.TextTitle;
import java.net.URL;
public class Tryitshow
{
public static String generatePieChart(String infoStr, String titles,HttpSession session, PrintWriter pw)
{
String filename = null;
Font font;
try {
// 建立PieDataSet
DefaultPieDataset data = new DefaultPieDataset();
String ss[]=infoStr.split(",");
for (int n=0;n<ss.length;n++)
{
String ssPart[]=ss[n].split("#");
int tt=Integer.parseInt(ssPart[1]);
String mm=ssPart[0];
data.setValue(mm,tt);
}
// 生成chart物件
PiePlot plot = new PiePlot(data);
plot.setInsets(new Insets(0, 5, 5, 5));
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
font=new Font("黑体",Font.CENTER_BASELINE,20);//这个地方是设置统计图标题的字体和大小
JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
TextTitle tt=new TextTitle(titles);
tt.setFont(font);
chart.setBackgroundPaint(java.awt.Color.white);//统计图片的底色
chart.setTitle(tt);
// 把生成的文件写入到临时的目录中
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
//选择存储成png格式的文件,当然你也可以使用saveChartAsJPEG的方法生成jpg图片
// 把image map 写入到 PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush();
}
catch (Exception ex) {
System.out.println("error:"+ex.getMessage());
}
return filename;
}
}
接着是jsp文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import = "com.tryit.Tryitshow" %>
<%@ page import = "java.io.PrintWriter" %>
<%@ page import = "java.text.SimpleDateFormat" %>
<%@ page import = "java.text.ParseException" %>
<%@ page import = "java.util.ArrayList" %>
<%@ page import = "java.util.Date" %>
<%@ page import = "java.util.Iterator" %>
<%@ page import = "java.util.Locale" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<%
String filename = Tryitshow.generatePieChart("本科#100,专科#200","人才学历信息统计",session, new PrintWriter(out));
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
<BODY>
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>">
</BODY>
</HTML> web.xml文件的配置
<web-app>
<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>
</web-app>