Reading data from an XML source

The previous topic describes JetChart database connectivity using some methods implemented for this purpose.
Although those features bring more flexibility to JetChart, a chart application remains tied to a particular database. If the database engine is changed, it is necessary to modify the JDBC driver and connection parameters, and possibly the SQL clauses, should the SQL engines are incompatible with each other, in terms of differences found in the syntax of the SQL language.

The XML language is used by JetChart to define a data structure not dependent on proprietary specifications. JetChart implements XML data parsing, reading chart labels and series values from an XML stream, whatever the source, provided this source adheres to some rules to generate the XML data.

Chart properties are read from an XML stream using the method GenericGraph.readXMLData(Reader reader). Subclasses of GenericGraph override this method to meet the characteristics of different chart contexts.

Below we can see the XML structure used to read labels and values to be passed to the chart context represented by the Graph class and the series it supports, like line series, bar series, area series, and others.

<chart>
  <labels>
     l1
     l2
     l3
     l4

  </labels>
  <series id="1">
    <values>
       80
       100
       70
       60

    </values>
  </series>
  <series id="2">
    <multiplevalues qty="4" delimiter="|">
       10|20|15|35
       22|38|12|40
       17|50|20|13
       25|14|38|55

    </multiplevalues>
  </series>
</chart>

In the xml sequence above, we can see the following xml elements and attributes:

<chart> - This is the root element, and it must be closed at the end of the xml sequence.

<labels> - Used to list textual data representing the chart labels.

<series> - Encloses the properties of a series. The id attribute is used to identify the series within the chart context. It is compared with the value returned by the AbstractSerie.getId() method, and if they match, the properties read from this xml file are assigned to the identified series.

<values> - Used to list textual data representing a series values.

<multiplevalues> - Used to set values of series that display data points associated with multiple values, like ohlc series, for example. The qty attribute is mandatory, and it is used to set the exact number of values expected from each line listed within the <multiplevalues> tag. The delimiter attribute is optional. It is used by the xml parser to tokenize values. If not informed, a space character is used.

Every opened xml element must be closed, otherwise the xml parser raises an exception.

The XML structure supported by a scatter chart context, represented by the class ScatterGraph, is similar to that of the Graph class. The only exceptions are the <values> and <labels> tags, not supported by ScatterGraph. XY series, XY line series and XY image series have two values associated with each data point, and a bubble series has three values. Multiple values are assigned to each series using the <multiplevalues> tag.

The structure of a XML source read by a pie chart differs from the one previously described. A pie chart context, represented by the PieGraph class, supports one pie series only, therefore it is not necessary to inform an identification number as required above. Additionaly, instead of chart labels, a pie chart expects slices titles.

The XML structure supported by a pie chart context is the following:

<chart>
  <titles>
     slice1
     slice2
     slice3
     slice4

  </titles>
  <values>
       80
       100
       70
       60

  </values>
  <colors>
       ff0000
       00ff00
       0000ff
       ffff00

  </colors>
</chart>

In the xml sequence above, we can see the following xml elements and attributes:

<chart> - This is the root element, and it must be closed at the end of the xml sequence.

<titles> - Used to list textual data representing slices titles.

<values> - Used to list textual data representing slices values.

<colors> - Used to list textual data representing slices colors. Colors must be informed as hexadecimal values.

Every opened xml element must be closed, otherwise the xml parser raises an exception.

The example below displays a combo chart that loads labels and series values from an XML file located at the application directory. Data is read from the XML file every 5 seconds. If the XML file content is modified, the chart is automatically updated to reflect changes.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.jinsight.jetchart.*;
import java.io.*;

public class Main extends JFrame  implements Runnable {

   Graph graph;

   public Main() {

        graph=new Graph();

        graph.setTitle(new String[]{"The JetChart Library","Reading data from an XML source"});

        BarSerie bs=new BarSerie();
        bs.setTitle("Bar series");
        bs.setColor(new Color(00,99,00));
        bs.setId(1);

        LineSerie ls=new LineSerie();
        ls.setTitle("Line series");
        ls.setColor(Color.red);
        ls.setMarksEnabled(false);
        ls.setThickness(2);
        ls.setId(2);

        graph.addSerie(bs);
        graph.addSerie(ls);

        Grid grid=graph.getGraphSet(0).getGrid();
        grid.setEnabled(true);
        //grid.setThickness(2);

        Container ct=getContentPane();
        ct.add(graph);

        setSize(550,300);
        setVisible(true);
        
        Thread t=new Thread(this);
        t.start();
   }
   
   public void run() {

        FileReader fr=null;

        while (true) {

           try {

              fr=new FileReader("chart.xml");

              graph.readXMLData(fr);
              graph.repaint();

              Thread.sleep(5000);

           }
           catch (InterruptedException e) {
              e.printStackTrace();
           }
           catch (IOException e) {
              e.printStackTrace();
           }
           finally {
              try {
                 if (fr!=null) 
                    fr.close();
              }
              catch (IOException e) {
              }
           }
        }
        
   }

   public static void main(String[] args) {
       new Main();
   }

}

The content of the chart.xml file is the following:

<chart>

   <labels>
      label1
      label2
      label3
      label4
      label5
      label6
   </labels>
   
   <series id="1">
      <values>
         100
         80
         90
         110
         120
         95
      </values>
   </series>

   <series id="2">
      <values>
         50
         60
         55
         65
         80
         90
      </values>
   </series>

</chart>

The next application displays a pie chart and reads pie series data from an XML file.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.jinsight.jetchart.*;
import java.io.*;

public class Main extends JFrame implements Runnable {

   PieGraph graph;

   public Main() {

        graph=new PieGraph();
        graph.set3DEnabled(true);

        graph.setTitle(new String[]{"The JetChart Library","Reading data from an XML source"});

        PieSerie ps=new PieSerie();
        graph.addSerie(ps);

        Container ct=getContentPane();
        ct.add(graph);

        setSize(550,400);
        setVisible(true);
        
        Thread t=new Thread(this);
        t.start();
   }
   
   public void run() {

        FileReader fr=null;

        while (true) {

           try {

              fr=new FileReader("piechart.xml");

              graph.readXMLData(fr);
              graph.repaint();

              Thread.sleep(5000);

           }
           catch (InterruptedException e) {
              e.printStackTrace();
           }
           catch (IOException e) {
              e.printStackTrace();
           }
           finally {
              try {
                 if (fr!=null) 
                    fr.close();
              }
              catch (IOException e) {
              }
           }
        }
   }
   
   public static void main(String[] args) {
       new Main();
   }

}
The content of the piechart.xml file is the following:

<chart>

   <titles>
      slice1
      slice2
      slice3
      slice4
      slice5
   </titles>
   
   <values>
      100
      80
      90
      110
      120
   </values>

   <colors>
      ff0000
      00ff00
      0000ff
      ffff00
      ff00ff
   </colors>

</chart>

Future versions of JetChart will support XML persistence, meaning that it will be possible to output chart properties into an XML file and later restore chart state by reading stored information.