Using JFreeChart to draw graph in Servlet

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
Stanley Chan

Using JFreeChart to draw graph in Servlet

Post by Stanley Chan » Fri Aug 25, 2000 10:08 am


I has problem in drawing graph in servlet by using JFreeChart. Below is a part of my servlet program :


xyData = createXYDataSource2();
chart = JFreeChart.createTimeSeriesChart(xyData);
StandardTitle title = (StandardTitle)chart.getTitle();
title.setTitle("Value of GBP");
chart.setChartBackgroundPaint(new GradientPaint(0, 0,,0, 1000, Col;
Plot myPlot = chart.getPlot();
Axis myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("USD per GBP");

try {

frame = new Frame();

Image image = frame.createImage(WIDTH, HEIGHT);
g = image.getGraphics();
chart.draw((Graphics2D)g,new Rectangle2D.Double(10, 10, 430,250));
GifEncoder encoder = new GifEncoder(image, out);


GifEncoder is a class for encoding GIF image that I downloaded in Acme. My program can be compiled successfully. However when I run it, it will return a blank image. What's wrong ?

Please help since I am urgent in doing it. Thank you very much.


Rawat Subodh Kumar

RE: Using JFreeChart to draw graph in Servlet

Post by Rawat Subodh Kumar » Mon Aug 28, 2000 7:36 am

By right it should work, you may try with following codes:

String[] seriesName=new String[]{"X Series"};
XYDataSource myDataSource = new DefaultXYDataSource(seriesName,data);

JFreeChart myChart = JFreeChart.createXYChart(myDataSource);
String title="Servlet graph";

Plot myPlot=myChart.getPlot();
Axis myHorizontalAxis=myPlot.getAxis(Plot.HORIZONTAL_AXIS);
myHorizontalAxis.setLabel("Lable Name");

Axis myVerticalAxis=myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("Vertical lable");
DefaultCategoryDataSource d=(DefaultCategoryDataSource)myDataSource;
String[] names=new String[]{"ABC 1","ABC 2","ABC 3"};
Frame frame = new Frame();
Image image = frame.createImage(600,300);
Graphics g = image.getGraphics();
myChart.draw((Graphics2D) g, new Rectangle(600,300));

ServletOutputStream out = res.getOutputStream();
GifEncoder encoder = new GifEncoder(image,out);

if (g!=null) g.dispose();
if (frame != null) frame.removeNotify();

Post Reply