Converting JFreeChart to an Applet

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

Converting JFreeChart to an Applet

Post by Jim Kallenborn » Mon Jul 31, 2000 7:30 pm

Can JFreeChart be converted to an applet? If so, what are the basic steps that need to be taken?

David Gilbert

RE: Converting JFreeChart to an Applet

Post by David Gilbert » Tue Aug 01, 2000 9:12 am

Hi Jim,

I'm not very knowledgeable about applets, but I have put together a simple applet demo for the next release of JFreeChart. The demo simply displays the tabbed pane with the five sample charts inside an applet - I didn't attempt to get the extras working (printing, editing chart properties etc).

You need to use Sun's Java plug-in, because JFreeChart uses Java2D which is only included in version 1.2 or later of the development kit. I spent a little while trying to create the correct HTML to display the applet (because the plug-in doesn't work with the APPLET tag). In the end, I downloaded Sun's HTML converter which takes APPLET tags and converts them to some mess which seems to work - I didn't want to figure out why!

I've pasted the applet class below - wherever you see SampleChartFactory.createSampleChart(int) you just need to have a working JFreeChartPanel (that's what the method returns). If you only want to display one chart in the applet, you could do away with the JTabbedPane and just add the JFreeChartPanel to the content pane.

Anyway, here's the code:

package com.jrefinery.chart.demo;

import java.awt.*;
import javax.swing.*;

/** A very basic applet demonstration. */
public class JFreeChartDemoApplet extends JApplet {

/** Standard constructor - builds a demo applet that displays five different charts within a
JTabbedPane. */
public JFreeChartDemoApplet() {
setContentPane(createContent());
}

/** Create a panel containing a tabbed pane where each tab shows a different sample chart. */
private JPanel createContent() {
JPanel content = new JPanel(new BorderLayout());

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Vertical Bar Chart", SampleChartFactory.createSampleChart(0));
tabbedPane.addTab("Horizontal Bar Chart", SampleChartFactory.createSampleChart(1));
tabbedPane.addTab("Line Chart", SampleChartFactory.createSampleChart(2));
tabbedPane.addTab("XY Plot", SampleChartFactory.createSampleChart(3));
tabbedPane.addTab("Time Series", SampleChartFactory.createSampleChart(4));
tabbedPane.addTab("Debug", SampleChartFactory.createSampleChart(5));

content.add(tabbedPane);
return content;
}

}

Hope that helps,

DG.

David Berry

RE: Converting JFreeChart to an Applet

Post by David Berry » Wed Aug 02, 2000 8:44 am

Jim,

Have you considered pursuing a servlet solution instead? In my experience, the browser support for getting applets to work is extremely poor. Like David says, you really only have one choice--that being the Sun Java Plugin. The disadvantage of this is that user's must download and configure an additional piece of client software, and it is difficult to assure that all users will do so.

If you go with a servlet solution, you can create just standard HTML pages and submit parameters just like a normal HTML form. When you send your data off to the servlet engine, the servlet engine can create your chart on the fly, encode it to a gif/png/jpeg, and return it as the appropriate mime type, and the graph will show up as a gif/png/jpeg in the user's browser window.

The advantage to doing this is that the requirements on the browser end become quite low--no plugins needed, probably an old browser will work if you keep the html simple enough. It also means you will write your GUI in HTML, which is very simple.

The disadvantages to this approach I can think of is that first, HTTP/HTML request/response model is really not designed to do complicated application logic. You can do some simple forms, but if you have a lot of complicated application logic, HTTP/HTML is not really suited for this. The other thing you have to do is have a servlet engine on your web server. There are some excellent servlet engines availible (I myself use Apcahe Jserv), but your webmaster will have to tell you if this is availible on your server, or can be made availible.

Of course, the most important consideration is your application, and if you think using a servlet would deliver the feature set that you want to deliver to your users.

Nitin

converting charts to gifs....

Post by Nitin » Thu Nov 02, 2000 11:10 am

Hi All,

I am currently working on JFreechart for a development which requires charting.Now the requirement is that i need tio convert the chart to Gifs/Jpegs so that i can display it in the web browser.I have tried with the following code to convert Chart to gifs.I am using Acme GifEncoder class for this encoding purpose.I get an error "IOException:Too Many colors for the Gif"

Can any one tell me what's wrong with the code:Any help will be highly appreciated.

public void attempToChangeToGif()
{
Frame f = new Frame();
f.addNotify();
Image img = f.createImage(600,300);
Graphics g = img1.getGraphics();
mychart.draw((Graphics2D)g,new Rectangle(600,300));
try
{
FileOutputStream outfile = new FileOutputStream("testchart.gif" );
GifEncoder genc = new GifEncoder(img1,outfile,true);
genc.encode();
}
catch (Exception e)
{
System.out.println(e);
}
}

Though i am successfully able to change the chart to PNG format(using PngEncoder) but converting to Gif gives me the same error.

Thanks to everyone for putting effort to read this mail

Any or all help will be appreciated

Regards
Nitin

Locked