Search code examples
javaservletsjfreechart

display jfreecharts using servlets


I have a scenario where I need to display charts(generated using jfreecharts) converted it into a png image and then display them using servlets.

When the chart code extends either ApplicationFrame or Jframe I see the following exception when I display it using servlets :

java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Window.(Window.java:317)
at java.awt.Frame.(Frame.java:419)
at javax.swing.JFrame.(JFrame.java:194)

I read through a couple of places and this seems to be because of the ApplicationFrame and JFrame would cause this error.

If i pass just the chart object this throws a

java.lang.IllegalArgumentException: Null 'chart' argument.
    org.jfree.chart.ChartUtilities.writeChartAsPNG(ChartUtilities.java:181)
    org.jfree.chart.ChartUtilities.writeChartAsPNG(ChartUtilities.java:136)

Is there any solution for this?


Solution

  • ChartUtilities is the right choice; streams are supported, too. One approach requires Using Headless Mode in the Java SE Platform, but I've also gotten it to work with VNC.

    Addendum: here's a related forum thread.