Topic: jfreechart servlet sample

  Print this page

1.jfreechart servlet sample Copy to clipboard
Posted by: flytiger
Posted on: 2003-03-19 09:35

http://www.jfree.org/

package test;

/* Here is a sample writen by myself
Wish it can help you getting started with JFreeChart--
a nice chart package*/

import java.io.IOException;
import java.io.OutputStream;
import java.awt.GradientPaint;
import java.awt.Color;
import java.lang.Integer;
import java.lang.Math;
import java.lang.Comparable;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartUtilities;
import com.jrefinery.chart.Legend;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.data.CategoryDataset;
import com.jrefinery.data.DefaultCategoryDataset;

public class JFreeChartServlet extends HttpServlet {
  public void doGet(HttpServletRequest request,
           HttpServletResponse response)
        throws ServletException, IOException {    
    
    int width = 600;
    int height = 450;
    OutputStream out = response.getOutputStream();
    JFreeChart chart = createChart(height);
    
    response.setContentType("image/jpeg");
    ChartUtilities.writeChartAsJPEG(out, chart, width, height);
    out.flush();
    out.close();
  }
  
  public void doPost(HttpServletRequest request,
           HttpServletResponse response)
        throws ServletException, IOException {
    doGet(request, response);
  }
  
protected JFreeChart createChart(int height) {
int initColor;
int finalColor;
boolean showLegend;
String chartTitle_;
Legend l1;
int type;
JFreeChart chart;
showLegend = true;
chart = null;
type = 1;

initColor = 0;
finalColor = 20;

chartTitle_ = "JFreeChart Servlet";
showLegend = true;

chart = createCategoryChart(type);
if (chart != null) {
chart.setBackgroundPaint(new GradientPaint(0.0f, 0.0f, getColor(initColor), 0.0f, (float) height, getColor(finalColor)));

if (showLegend) {
l1 = chart.getLegend();
l1.setAnchor(2);
}
else {
chart.setLegend(null);
}
if (chartTitle_ != null) {
chart.setTitle(chartTitle_);
}
}
return chart;
}

public static Color getColor(int color) {
switch ((color % 11)) {
case 0:
return Color.white;
case 1:
return Color.black;
case 2:
return Color.blue;
case 3:
return Color.green;
case 4:
return Color.red;
case 5:
return Color.yellow;
case 6:
return Color.gray;
case 7:
return Color.orange;
case 8:
return Color.cyan;
case 9:
return Color.magenta;
case 10:
return Color.pink;
case 11:
return Color.getHSBColor(60.0f, 50.0f, 100.0f);
}
return Color.white;
}

protected JFreeChart createCategoryChart(int type) {
CategoryDataset chartData = createCategoryDataset();
JFreeChart chart;
try {
switch (type) {
case 0:
chart = ChartFactory.createVerticalBarChart3D("", "", "", chartData, true, true, true);
break;
case 1:
chart = com.jrefinery.chart.ChartFactory.createStackedVerticalBarChart("", "", "", chartData, true, true, true);
break;
case 2:
chart = com.jrefinery.chart.ChartFactory.createStackedVerticalBarChart3D("", "", "", chartData, true, true, true);
break;
case 3:
chart = com.jrefinery.chart.ChartFactory.createHorizontalBarChart("", "", "", chartData, true, true, true);
break;
case 4:
chart = com.jrefinery.chart.ChartFactory.createHorizontalBarChart3D("", "", "", chartData, true, true, true);
break;
default:
chart = com.jrefinery.chart.ChartFactory.createVerticalBarChart("", "", "", chartData, true, true, true);
}
return chart;
}
catch (Exception e1) {
e1.printStackTrace();
return null;
}
}

// generate random dataset
public static CategoryDataset createCategoryDataset() {
DefaultCategoryDataset data = new DefaultCategoryDataset();
int i, j;
for (i=0;i<3;i++) {
  for (j=0;j<12;j++) {
    data.addValue(Math.random()*20.0, (Comparable) Integer.toStringLight Bulb, (Comparable) Integer.toString(j));
  }
}
return data;
}
}

// good luck

2.Re:jfreechart servlet sample [Re: flytiger] Copy to clipboard
Posted by: jianhua23
Posted on: 2003-03-20 11:21

用的是那个 JFreeChart 版本啊?!在 0.9.3 中好像编译不过。
chart.setTitle(chartTitle_);
chartTitle 是个 String 型,而新版本中chart.setTitle(List型).
好像这个 Demo 有点老了Smile

3.Re:jfreechart servlet sample [Re: flytiger] Copy to clipboard
Posted by: flytiger
Posted on: 2003-03-20 17:38

I am using 0.9.6, it works fine.

4.Re:jfreechart servlet sample [Re: flytiger] Copy to clipboard
Posted by: yamakasy
Posted on: 2003-03-29 20:03

JFreeChart的确是我所见过的免费的最不错的Java Chart库了,而JFreeReport,jasperReport则是相当不错的报表库,都推荐放进自己的工具箱~

5.Re:jfreechart servlet sample [Re: flytiger] Copy to clipboard
Posted by: Julian13
Posted on: 2003-03-30 00:24

yamakasy, have you tried integrating JFreeChart and JasperReports?

6.Re:jfreechart servlet sample [Re: flytiger] Copy to clipboard
Posted by: nonametxt
Posted on: 2003-04-29 08:46

thank you very much ,that is great!Smile

7.Re:jfreechart servlet sample [Re: nonametxt] Copy to clipboard
Posted by: wander
Posted on: 2003-05-15 08:20

我是学jsp的,最近刚接触jfreechart,觉得这个java类库很好,我想学它,希望有这方面的好的书籍向我介绍一下,谢谢!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923