As it was said before, the chart context, represented by the GenericGraph class, descends
from JPanel. A JPanel object has to be laid out on a valid container that descends from the Window
class, like the JFrame class. When an instance of a container is created, it is assigned a peer, which
translates the behavior of the Java container to the platform's native container.
Every subclass of
the Component class has a peer. When a user interface with components and containers is constructed,
no peers are created. But when the user interface is ready to be displayed, the peers for each
component are created, by a call to the addNotify() method of the top-level container. The
call is then propagated and the addNotify() method of each children component is invoked, creating
their peers. After that, the user interface is displayed on the screen.
To generate a JPEG,GIF or PNG image from an invisible application, it is necessary to make a
explicit call to the addNotify() method of the top-level container and validate the chart context,
so it can be laid out properly. Invisible applications using JetChart can implement these steps or
simply place a call to the method GenericGraph.setOffScreenGraphEnabled(boolean isOffScreenGraphEnabled),
and JetChart takes care of these details.
The methods below must always be invoked on the GenericGraph instance when generating offscreen images:
import java.awt.*; import java.awt.event.*; import com.jinsight.jetchart.*; import java.io.*; public class Main { public static void main(String[] args) { Graph graph=new Graph(new String[]{"l1","l2","l3","l4","l5"}); graph.setOffScreenGraphEnabled(true); graph.setSize(500,400); // The chart size has to be set. graph.setTitle(new String[]{"The JetChart Library","Generating GIF/JPEG/PNG images from offscreen applications"}); graph.set3DEnabled(true); graph.setHorizontalGraphEnabled(true); StackBarSerie sb1=new StackBarSerie(new double[]{100,70,80,90,50},"Stacked bar series 1"); sb1.setColor(Color.green); StackBarSerie sb2=new StackBarSerie(new double[]{-40,-60,70,-80,90},"Stacked bar series 2"); sb2.setColor(Color.orange); graph.addSerie(sb1); graph.addSerie(sb2); graph.getGraphSet(0).getGrid().setEnabled(true); ChartEncoder chartEncoder=new ChartEncoder(graph); FileOutputStream out=null; try { File f=new File("output.jpg"); out=new FileOutputStream(f); // Encodes chart image into a jpeg file, setting quality to 85%. System.out.println("Image encoding started..."); chartEncoder.jpegEncode(out,85); System.out.println("Finished."); // To encode chart into a GIF or PNG format, disable the line above and // enable one of the following lines. Change the file extension accordingly. //chartEncoder.gifEncode(out); //chartEncoder.pngEncode(out,9); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out!=null) out.close(); // A call to System.exit(0) has to be placed at this point to stop // application, because the AWT thread remains active. // There is no need for the line below if this application is run using // JDK 1.4 or newer versions. System.exit(0); } catch (IOException e) { } } } }