Using JFreeChart to draw graph in Servlet

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

Using JFreeChart to draw graph in Servlet

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

Hi,

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

...

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

try {

frame = new Frame();
frame.addNotify();

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

...

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.

Stanley

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";
myChart.setTitle(title);


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"};
d.setSeriesNames(names);
*/
Frame frame = new Frame();
frame.addNotify();
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);
encoder.encode();

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

Locked