程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Tomcat下使用Jfreechart 簡單例子(servlet)

Tomcat下使用Jfreechart 簡單例子(servlet)

編輯:JAVA編程入門知識

  這個例子是我參考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>
<TITLE>JFreeChart 簡單的例子</TITLE>
</HEADER>
<BODY>
<H2>JFreeChart: 簡單的例子</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>
<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();
}
}
}

  • 首頁
  • 上一頁
  • 1
  • 2
  • 3
  • 下一頁
  • 尾頁
  • 共3頁
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved