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>
<TITLE>JFreeChart 简单的例子</TITLE>
</HEADER>
<BODY>
<H2>JFreeChart: 简单的例子</H2>
<P>
下面有两个简单的例子,调用servlet,生成统计图.
<ul>
<li>条形统计图<a
href="/oblog312/servlet/ServletDemo1">条形统计图</a>;</li>
<li>可以选择统计图类型 <a
href="/oblog312/chart.html">三种类型</a>.
</li>
</ul>
</BODY>
</HTML>
4.新建Chart.html 放到C: Tomcat 5.0 webapps myServlets下
<HTML>
<HEADER>
<TITLE>JFreeChart Servlet Demo 2</TITLE>
</HEADER>
<BODY>
<H2>JFreeChart 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>
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("<TITLE>JFreeChart Servlet Demo 2</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<H2>JFreeChart 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="/oblog312/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-name>ServletDemo1</servlet-name>
<url-pattern>/servlet/ServletDemo1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletDemo2</servlet-name>
<url-pattern>/servlet/ServletDemo2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletDemo2ChartGenerator</servlet-name>
<url-pattern>/servlet/ServletDemo2ChartGenerator</url-pattern>
</servlet-mapping>
</web-app>
10.重启TOMCAT http://172.25.49.1:8080/myServlets/ .
分享到:
相关推荐
**JFreeChart在Tomcat下的应用** JFreeChart是一个强大的Java图表库,它提供了一种简单的方法来创建各种复杂的图表,如饼图、线图、柱状图、散点图等,广泛应用于数据分析和报告展示。在Java Web应用程序中,尤其是...
使用JFreeChart来创建基于web的图表需要准备好开发环境,包括Servlet引擎或者J2EE应用服务器(例如WebSphere、Tomcat等)。在搭建WEB环境时,需要安装JFreeChart引擎,并将其配置到项目中。 在使用JFreeChart生成...
Tomcat作为流行的Java Web服务器,对JSP、Servlet等技术有很好的支持,与JFreeChart结合可以构建高性能的数据可视化平台。 总结来说,JFreeChart是一个功能强大的Java图表库,通过提供的Eclipse项目实例,我们可以...
* 根目录下的 jfreechart-1.0.6-demo.jar 是例子程序,大家双击后可看到其中有很多例子的运行结果。 2. 环境配置 笔者的开发环境如下: * JDK:1.5 * MyEclipse:5.1.1 * Tomcat:5.5.28 为了后续的“用 ...
首先,你需要下载 JFreeChart 的包,并将其添加到你的项目类路径中,例如配置到Tomcat服务器的lib目录下。一旦完成这一步,就可以开始创建图表了。 以下是一个简单的饼状图示例,使用JavaBean来生成: ```java ...
如果是WEB开发,以TOMCAT中的一个WEB项目TEST为例子说明: 把上述三个文件拷贝到TEST\WEB-INF\LIB中,然后修改TEST\WEB-INF\web.xml文件,在其中加入如下代码: <servlet> <servlet-name>DisplayChart</servlet-...
为了使生成的图表能够被浏览器正确显示,还需要生成 `ChartRenderingInfo` 对象,并使用 JFreeChart 提供的 Servlet 来处理图表渲染的结果。具体的实现过程包括将图表输出为图片文件、生成对应于图表元素的 URL 映射...
接下来,我们将通过创建柱状图的例子来实践JFreeChart的使用。在Web应用的WebRoot目录下创建一个bar目录,用于存放柱状图的示例。创建一个名为sample1.jsp的文件,然后编写以下代码: ```jsp ;charset=GBK"%> , org...
如果是WEB开发,以TOMCAT中的一个WEB项目TEST为例子说明: 把上述三个文件拷贝到TEST\WEB-INF\LIB中,然后修改TEST\WEB-INF\web.xml文件,在其中加入如下代码: <servlet> <servlet-name>DisplayChart</servlet-...
在Java编程语言中,开发...通过这个例子,你可以学到Servlet生命周期、HTTP响应处理、以及如何使用第三方库如JFreeChart来增强Java应用的功能。在实际项目中,你可以根据需要调整代码,以满足更复杂的数据可视化需求。
WAR文件是Web应用程序的标准打包格式,可以直接部署到Servlet容器(如Tomcat)中。通过部署和访问这个示例,开发者可以直观地看到ChartDirector在实际Web项目中的工作方式,学习如何将图表集成到网页中。 五、使用...
源码可能包含了使用JDBC或ORM框架(如Hibernate)进行数据库查询的例子。 8. **RESTful API**:如果源码包含前后端分离的设计,那么会涉及RESTful API的创建,用以供前端调用获取数据。 9. **Web容器**:如Tomcat...
EJB中JNDI的使用源码例子 1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件...