Tomcat下使用Jfreechart 简单例子(servlet)

dachao0525

dachao0525

2016-02-19 15:33

最近很多朋友喜欢上设计,但是大家却不知道如何去做,别担心有图老师给你解答,史上最全最棒的详细解说让你一看就懂。

  这个例子是我参考JFreeChart Developer Guide做的.用Google搜一下,可以下载到.

  1.Tomcat的安装不描述.现在假如Tomcat 安装目录为C:Tomcat 5.0,新建C: Tomcat 5.0 webapps  myServlets目录.

  安装JDK  安装目录C:j2sdk1.4.2_09 并将C:j2sdk1.4.2_09libjfreechart-1.0.0-rc1.jar加到CLASSPATH中.

  2.去http://www.jfree.org/jfreechart/index.php下载jfreechart-1.0.0-rc1,后解压缩,将jfreechart-1.0.0-rc1.jar,jcommon-1.0.0-rc1.jar,gnujaxp.jar,放到C: Tomcat 5.0webapps  myServletsWEB-INFlib下 和 C: j2sdk1.4.2_09lib下.

  3.新建index.html 放到C: Tomcat 5.0  webapps  myServlets下

  

HTML
HEADER
TITLEJFreeChart 简单的例子/TITLE
/HEADER
BODY
H2JFreeChart: 简单的例子/H2
P

  下面有两个简单的例子,调用servlet,生成统计图.

  

ul
li条形统计图a
href="servlet/ServletDemo1"条形统计图/a;/li
li可以选择统计图类型 a
href="chart.html"三种类型/a.
/li
/ul
/BODY
/HTML

  4.新建Chart.html 放到C: Tomcat 5.0  webapps  myServlets下

  

HTML
HEADER
TITLEJFreeChart Servlet Demo 2/TITLE
/HEADER
BODY
H2JFreeChart Servlet Demo/H2
P
Please choose a chart type:
FORM ACTION="servlet/ServletDemo2" METHOD=POST
INPUT TYPE="radio" NAME="chart" VALUE="pie" CHECKED Pie Chart
INPUT TYPE="radio" NAME="chart" VALUE="bar" Bar Chart
INPUT TYPE="radio" NAME="chart" VALUE="time" Time Series Chart
P
INPUT TYPE="submit" VALUE="Generate Chart"
/FORM
/BODY
/HTML

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

  5.新建ServletDemo1.java 并用JDK编译成class 文件,当然要将C:  j2sdk1.4.2_09  lib  jfreechart-1.0.0-rc1.jar加到CLASSPATH中,才能正确编译.

  

package com.jrefinery.chart.demo;
import java.io.IOException;
import java.io.OutputStream;
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.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class ServletDemo1 extends HttpServlet {
public ServletDemo1() {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = response.getOutputStream();
try {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(10.0, "S1", "C1");
dataset.addValue(4.0, "S1", "C2");
dataset.addValue(15.0, "S1", "C3");
dataset.addValue(14.0, "S1", "C4");
dataset.addValue(-5.0, "S2", "C1");
dataset.addValue(-7.0, "S2", "C2");
dataset.addValue(14.0, "S2", "C3");
dataset.addValue(-3.0, "S2", "C4");
dataset.addValue(6.0, "S3", "C1");
dataset.addValue(17.0, "S3", "C2");
dataset.addValue(-12.0, "S3", "C3");
dataset.addValue( 7.0, "S3", "C4");
dataset.addValue(7.0, "S4", "C1");
dataset.addValue(15.0, "S4", "C2");
dataset.addValue(11.0, "S4", "C3");
dataset.addValue(0.0, "S4", "C4");
dataset.addValue(-8.0, "S5", "C1");
dataset.addValue(-6.0, "S5", "C2");
dataset.addValue(10.0, "S5", "C3");
dataset.addValue(-9.0, "S5", "C4");
dataset.addValue(9.0, "S6", "C1");
dataset.addValue(8.0, "S6", "C2");
dataset.addValue(null, "S6", "C3");
dataset.addValue(6.0, "S6", "C4");
dataset.addValue(-10.0, "S7", "C1");
dataset.addValue(9.0, "S7", "C2");
dataset.addValue(7.0, "S7", "C3");
dataset.addValue(7.0, "S7", "C4");
dataset.addValue(11.0, "S8", "C1");
dataset.addValue(13.0, "S8", "C2");
dataset.addValue(9.0, "S8", "C3");
dataset.addValue(9.0, "S8", "C4");
dataset.addValue(-3.0, "S9", "C1");
dataset.addValue(7.0, "S9", "C2");
dataset.addValue(11.0, "S9", "C3");
dataset.addValue(-10.0, "S9", "C4");
JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart",
"Category",
"Value",
dataset,
PlotOrientation.VERTICAL,
true, true, false
);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
}
catch (Exception e) {
System.err.println(e.toString());
}
finally {
out.close();
}
}
}

  6.新建ServletDemo2 .java 并用JDK编译

  

package com.jrefinery.chart.demo;
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;
public class ServletDemo2 extends HttpServlet {
public ServletDemo2() {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = new PrintWriter(response.getWriter());
try {
String param = request.getParameter("chart");
response.setContentType("text/html");
out.println("HTML");
out.println("HEAD");
out.println("TITLEJFreeChart Servlet Demo 2/TITLE");
out.println("/HEAD");
out.println("BODY");
out.println("H2JFreeChart Servlet Demo/H2");
out.println("P");
out.println("Please choose a chart type:");
out.println("FORM ACTION="ServletDemo2" METHOD=POST");
String pieChecked = (param.equals("pie") ? " CHECKED" : "");
String barChecked = (param.equals("bar") ? " CHECKED" : "");
String timeChecked = (param.equals("time") ? " CHECKED" : "");
out.println("INPUT TYPE="radio" NAME="chart" VALUE="pie""+ pieChecked + " Pie Chart");
out.println("INPUT TYPE="radio" NAME="chart" VALUE="bar""+ barChecked + " Bar Chart");
out.println("INPUT TYPE="radio" NAME="chart" VALUE="time""+ timeChecked + " Time Series Chart");
out.println("P");
out.println("INPUT TYPE="submit" VALUE="Generate Chart"");
out.println("/FORM");
out.println("P");
out.println("IMG SRC="ServletDemo2ChartGenerator?type="+ param + "" BORDER=1 WIDTH=800 HEIGHT=600/");
out.println("/BODY");
out.println("/HTML");
out.flush();
out.close();
}
catch (Exception e) {
System.err.println(e.toString());
}
finally {
out.close();
}
}
}

  7.新建ServletDemo2ChartGenerator .java 并用JDK编译.

  

package com.jrefinery.chart.demo;
import java.io.IOException;
import java.io.OutputStream;
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.data.xy.*;
import org.jfree.data.general.*;
import org.jfree.data.time.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class ServletDemo2ChartGenerator extends HttpServlet {
public ServletDemo2ChartGenerator() {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = response.getOutputStream();
try {
String type = request.getParameter("type");
JFreeChart chart = null;
if (type.equals("pie")) {
chart = createPieChart();
}
else if (type.equals("bar")) {
chart = createBarChart();
}
else if (type.equals("time")) {
chart = createTimeSeriesChart();
}
if (chart != null) {
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 800, 600);
}
}
catch (Exception e) {
System.err.println(e.toString());
}
finally {
out.close();
}
}
private JFreeChart createPieChart() {
// create a dataset...
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("One", new Double(43.2));
data.setValue("Two", new Double(10.0));
data.setValue("Three", new Double(27.5));
data.setValue("Four", new Double(17.5));
data.setValue("Five", new Double(11.0));
data.setValue("Six", new Double(19.4));
JFreeChart chart = ChartFactory.createPieChart(
"Pie Chart",
data,
true, true, false
);
return chart;
}
private JFreeChart createBarChart() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(10.0, "S1", "C1");
dataset.addValue(4.0, "S1", "C2");
dataset.addValue(15.0, "S1", "C3");
dataset.addValue(14.0, "S1", "C4");
dataset.addValue(-5.0, "S2", "C1");
dataset.addValue(-7.0, "S2", "C2");
dataset.addValue(14.0, "S2", "C3");
dataset.addValue(-3.0, "S2", "C4");
dataset.addValue(6.0, "S3", "C1");
dataset.addValue(17.0, "S3", "C2");
dataset.addValue(-12.0, "S3", "C3");
dataset.addValue( 7.0, "S3", "C4");
dataset.addValue(7.0, "S4", "C1");
dataset.addValue(15.0, "S4", "C2");
dataset.addValue(11.0, "S4", "C3");
dataset.addValue(0.0, "S4", "C4");
dataset.addValue(-8.0, "S5", "C1");
dataset.addValue(-6.0, "S5", "C2");
dataset.addValue(10.0, "S5", "C3");
dataset.addValue(-9.0, "S5", "C4");
dataset.addValue(9.0, "S6", "C1");
dataset.addValue(8.0, "S6", "C2");
dataset.addValue(null, "S6", "C3");
dataset.addValue(6.0, "S6", "C4");
dataset.addValue(-10.0, "S7", "C1");
dataset.addValue(9.0, "S7", "C2");
dataset.addValue(7.0, "S7", "C3");
dataset.addValue(7.0, "S7", "C4");
dataset.addValue(11.0, "S8", "C1");
dataset.addValue(13.0, "S8", "C2");
dataset.addValue(9.0, "S8", "C3");
dataset.addValue(9.0, "S8", "C4");
dataset.addValue(-3.0, "S9", "C1");
dataset.addValue(7.0, "S9", "C2");
dataset.addValue(11.0, "S9", "C3");
dataset.addValue(-10.0, "S9", "C4");
JFreeChart chart = ChartFactory.createBarChart3D(
"Bar Chart",
"Category",
"Value",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
return chart;
}
private JFreeChart createTimeSeriesChart() {
// here we just populate a series with random data...
TimeSeries series = new TimeSeries("Random Data");
Day current = new Day(1, Month.JANUARY, 2001);
for (int i = 0; i 100; i++) {
series.add(current, Math.random() * 100);
current = (Day) current.next();
}
XYDataset data = new TimeSeriesCollection(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Time Series Chart",
"Date",
"Rate",
data,
true,
true,
false
);
return chart;
}
}

  8,如果顺利,通过5.6.7步,我们手上有会有三个CLASS文件.我们将这三个CLASS文件拷到C:  Tomcat 5.0  webapps  myServlets  WEB-INF  classescom  jrefinery  chart   demo目录下

  9.新建WEB.XML拷贝到C:  Tomcat 5.0   webapps   myServlets   WEB-INF下

  

?xml version="1.0" encoding="ISO-8859-1"?
!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"
web-app
servlet
servlet-name
ServletDemo1
/servlet-name
servlet-class
com.jrefinery.chart.demo.ServletDemo1
/servlet-class
/servlet
servlet
servlet-name
ServletDemo2
/servlet-name
servlet-class
com.jrefinery.chart.demo.ServletDemo2
/servlet-class
/servlet
servlet
servlet-name
ServletDemo2ChartGenerator
/servlet-name
servlet-class
com.jrefinery.chart.demo.ServletDemo2ChartGenerator
/servlet-class
/servlet
servlet-mapping
servlet-nameServletDemo1/servlet-name
url-pattern/servlet/ServletDemo1/url-pattern
/servlet-mapping
servlet-mapping
servlet-nameServletDemo2/servlet-name
url-pattern/servlet/ServletDemo2/url-pattern
/servlet-mapping
servlet-mapping
servlet-nameServletDemo2ChartGenerator/servlet-name
url-pattern/servlet/ServletDemo2ChartGenerator/url-pattern
/servlet-mapping
/web-app

  10.重启TOMCAT http://172.25.49.1:8080/myServlets/  .

  最后:小心上面的拷贝路径,不知道为什么斜杠出不来

  我参考JFreeChart Developer Guide中的例子,我发现生成统计图真的很漂亮,由于我是JAVA初学者,其中的很多东西我也是直接拷贝过来,不过上面代码应该是没有问题的,能正常运行.

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
展开更多 50%)
分享

猜你喜欢

Tomcat下使用Jfreechart 简单例子(servlet)

编程语言 网络编程
Tomcat下使用Jfreechart 简单例子(servlet)

jsp JFreeChart使用心得与例子

Web开发
jsp JFreeChart使用心得与例子

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

Tomcat下JSP、Servlet和JavaBean环境配置

Web开发
Tomcat下JSP、Servlet和JavaBean环境配置

Tomcat 下使用itext生成报表

编程语言 网络编程
Tomcat 下使用itext生成报表

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

java类中使用Jfreechart的简单实例

编程语言 网络编程
java类中使用Jfreechart的简单实例

Servlet实现访问次数的例子

Web开发
Servlet实现访问次数的例子

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

在C++ Builder中显示透明位图

在C++ Builder中显示透明位图

Ajax实现页面loading效果

Ajax实现页面loading效果
下拉加载更多内容 ↓