Generating an image chart

To create a chart and render the chart in JPEG format, you must create the XML input file that represents the chart you want to render. The XML file should contain the raw data and configuration data needed by the chart service to create the chart. The following is a fragment of code that will allow you to generate the chart.

    1 FileInputStream inputStream = new FileInputStream(new File("chart.xml")); 
    2 IChart chart = SerializerImpl.instance().read(inputStream); 
    3 IRenderer renderer = new StaticImageRenderer();
    4 FileOutputStream outputStream = new FileOutputStream(new File("chart.jpg"));
    5 renderer.setProperty(IRenderer.OUTPUTSTREAM_IDENTIFIER, outputStream);
    6 renderer.setProperty(StaticImageRenderer.STATIC_IMAGE_TYPE, StaticImageRenderer.JPEG);
    7 Generator.instance().run(renderer, chart);   

Line 1 - In this line an input stream from an XML file is created. The XML file is formatted based on the input schema and contains the raw data and configuration data for the chart.

Line 2 - The SerializerImpl class provides a read method that will allow one to take the XML stream and construct an internal representation of the chart object model. It should be noted that the serializer interface can also consume a XML document object model instead of an input stream. Therefore, a developer can construct the XML document object model in memory and pass in this DOM to the serializer.

Line 3 - Construct a static image renderer. This renderer provides the ability to create static images such as jpeg, png and pdf chart images.

Line 4 - An output stream is created that will generate a jpeg file called chart.jpg.

Line 5 - We use the setProperty method on the renderer interface to pass in configuration information to the renderer. Here we set the outputstream that the static image renderer will use to write the generated jpeg chart.

Line 6 - We set the type of static image we want to generate. In this case we want to create a jpeg file so we set the STATIC_IMAGE_TYPE parameter to StaticImageRenderer.JPEG.

Line 7 - This line generates and writes the chart to the output stream, created on line 4, based on the chart model which was parsed in Line 2.

Related reference
Programmer's Guide
Formatting input data for charting