XmlJChart - Tutorial

XmlJChart is a pure Java API for creating charts dynamically from a server from a XML file. You can also create charts programmatically by creating a model and then creating a view using the model. Following are the advantages of using XmlJChart:


. Create chart from XML data - Create a simple to type XML in memory or as a file and call the viewer to view it as Java Swing chart, call image creator to produce an image of the chart. XML metafile supports specifying of user event handler classes.

. Create chart image - Create chart image in server without displaying the chart in an UI. Simply create XML in memory and call image creator program to create images on fly.

. Programmatically create charts - You have option to create chart programmatically from your program by creating model and then associating the model to a view.

. Browser chart out of box - Use XML metafile as an URL to the xml_jchart_viewer.jsp viewer page and to the xml_jchart_to_image.jsp image creator web page to create UI and image chart respectively.

. Other unility functions - such as printing of the chart by simply calling the print() method.

How to create an XML input metafile

It is easy to create XML metafile by simply adding some XML like tags. Here is an example also made available as part of the downloaded archive. There are several other XML chart metafile examples for producing different kinds of charts. This sample is for creating a CATEGORY_CHART (known as bar chart). It has some user action handlers configred to capture user interaction so that the user action can be captured and some action may be taken from your handler code. The demo event handler samples typically does nothing but serves the purpose of understanding how it works so you can create your own handlers.

To know more about the tag names refer javadoc on XmlProperties interface which has all the supported XML tags with some description and example about how it use.

Folowing are the two ways you can create chart on fly:

Example XML metadata input file (available in the downloaded archive)

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xmljchart name="chart1" title="XmlJChart - XML Java Chart (BAR Chart Demo 2)">
  <xAxisLabel>Years </xAxisLabel>
  <yAxisLabel>Population Growth (%)</yAxisLabel>
  <chartSize>720, 400</chartSize>
    {1990, 1995, 2000}
    {North America, South America, Africa, Asia}
      {6, 3, -1.5, 5},
      {5, -1.25, -2.5, 2.5},
      {5, 4, 2, 3}


Types of charts that you can create

Following types of charts are supported at this release.

Chart visual styles

These are the styles supported to create chart of different kind of look and feel.

Image Conversion

Application can easily generate very high quality image files from the charting APIs without displaying in a UI. Following are the types of images supported. As we have mentioned before there are two ways to test the conversion.

Image convert from standalone application

Run the "SaveAsXXX.bat" or "SaveAsXXX.sh" batch file to convet sample charts to images. For browser based sample call the image converter page as mentioned next and also run the browser demo page to see how the image converter page xml_jchart_to_image.jsp is used.

Supported image types

How to run standalone samples to create chart images Go to the "bin" directory and run one of the "SaveAsXXX.bat" (on Windows) or "SaveAsXXX.sh" (on Unix or Linux) and it will create a sample chart.

Browser based image conversion sample

Here is the typical URL one can call to create a Chart image in the server web page and display it back to the browser page.



Printing of charts

You can directly print from a UI or generate image to print to a printer. PrintDemo.java sample shows how to print chart to a printer.

Chart refreshing

When a chart is already shown, if you make further changes to it, make sure to call the redraw() method to have the latest changes to take effect.

How to use browser edition for showing chart and images to a browser page

Download the browser edition and deploy under a web container. For example, if you are using a tomcat server simply copy the downloaded WAR file to the "webapps" directory and Tomcat will automatically deply it.

Example: let's say the downloaded browser archive is renamed as "xmljchart.war". Copy this war file under TomcayInstallDir/webapps/. Then access the main page from a browser with an URL something like http://localhost:8080/jchart/demo/demo_main.jsp. General URL format is http://host:port/webAppName/demo/demo_main.jsp.

Run demo samples

Download the standalone ZIP archive and unzip in a local file folder. Open a command shell window and follow the instruction to run the script files. The best way to learn is to follow the examples made available in the downloaded archive.

Run the ".bat" or ".sh" script files made available in the "bin" directory.

- Open a command window
- Go to the unzip directory where the downloaded archive is unzipped
- Go to "bin" directory
- Simply type the BAT file on Windows machine (or SH file name on UNIX/LINUX machine)
- It will open a view showing the chart


Downloaded archive has lots of JAVA files as examples. These samples are used in the BAT (Windows shell script) and SH (UNIX/LINUX shell script) files to execute from a command line. Java source examples are located under "examples" directory. Only few line of code is required to create your Java chart.

API Reference (Javadoc)

Downloaded archive has a javadoc ZIP archive providing API reference. Unzip the javadoc zip file to a directory and open the "index.html" page from a browser page.

Copyright © Activetree, Inc. All rights reserved.
Web: http://www.activetree.com
Email: sales@activetree.com
Tel: +1 408-791-8036
Fax: +1 408-716-8450