XML has proven to be a very dynamic and flexible means of data storage. Chart FX for Java has been developed with this in mind and allows you to populate your charts with data directly from XML files.
The Load method is supported by the XMLDataProvider class. To use this feature properly, you must create a new XmlDataProvider object, load the XML data and finally set the DataSource property, as follows:
XmlDataProvider cfxXML = new XmlDataProvider(); cfxXML.load("d:\temp\myxml2.xml"); Chart1.setDataSource(cfxXML);
|
When passing the data using the XML data provider, the data is passed as columns and rows, the same as with a table. The following rules apply:
a) There must be a special tag indicating that the information enclosed is the column's definition. By default this tag is named <COLUMNS>. Each column will need a separate COLUMN tag. The format is:
<COLUMNS> <COLUMN NAME="Column1_Name" TYPE="Column1_Type" ... <COLUMN NAME="ColumnN_Name" TYPE="ColumnN_Type" DESCRIPTION="Column1_Desciption" /> </COLUMNS>
|
The Description attribute is optional. If it is included, it will be used as a Series or Value legend. If it is not present, the Name will be assigned as a Series or Value legend instead.
b) As you can see above, each column name is configured with a type. The supported data types are String, Integer, Double, Short, Float, Boolean, Byte and Date.
c) Every row is then defined using the following format:
<ROW Column1_Name="Value" ... ColumnN_Name="Value"/>
|
This is a sample of an XML file used to populate a chart:
<?xml version="1.0"?> <CHARTFX> <COLUMNS> <COLUMN NAME="Product" TYPE="String"/> <COLUMN NAME="Q1" TYPE="Integer"/> <COLUMN NAME="Q2" TYPE="Integer"/> <COLUMN NAME="Q3" TYPE="Integer"/> <COLUMN NAME="Q4" TYPE="Integer"/> </COLUMNS> <ROW Product="ChartFX 98" Q1="9200" Q2="7835" Q3="10245" Q4="8762"/> <ROW Product="ChartFX IE 3.5" Q1="14350" Q2="11233" Q3="16754" Q4="987"/> <ROW Product="ReportFX" Q1="12398" Q2="7654" Q3="5678" Q4="9087"/> <ROW Product="Image Toppings" Q1="8742" Q2="12358" Q3="14321" Q4="8702"/> <ROW Product="ChartFX IE 2000" Q1="15672" Q2="17529" Q3="16524" Q4="654"/> <ROW Product="WebBarFX" Q1="6890" Q2="12389" Q3="9804" Q4="10723"/> <ROW Product="WebTreeFX" Q1="11320" Q2="12908" Q3="6521" Q4="7590"/> </CHARTFX>
|
Reading XML as a String
The sample above illustrates how you can read xml data from an xml file. This is done using the load method of the XmlDataProvider class. However, there may be a situation where you would like to read xml data as a string. The following example will demonstrate how Chart FX can read xml data from a string using the LoadXml method. The string is a concatenated xml file:
XMLProvider sfxXml = new XMLProvider(); String myString = new String(); myString = "<?xml version=\"1.0\"?><CHARTFX><COLUMNS>"; myString += "<COLUMN NAME=\"Product\" TYPE=\"String\"/>"; myString += "<COLUMN NAME=\"Q1\" TYPE=\"Integer\"/>"; myString += "<COLUMN NAME=\"Q2\" TYPE=\"Integer\"/>"; myString += "<COLUMN NAME=\"Q3\" TYPE=\"Integer\"/>"; myString += "<COLUMN NAME=\"Q4\" TYPE=\"Integer\"/>"; myString += "</COLUMNS>; myString += "<ROW Product=\"ChartFX 98\" Q1=\"9200\" "; myString += "Q2=\"7835\" Q3=\"10245\" Q4=\"8762\"/>"; myString += "<ROW Product=\"ChartFX IE 3.5\" Q1=\"14350\" "; myString += "Q2=\"11233\" Q3=\"16754\" Q4=\"15672\"/>"; myString += "<ROW Product=\"ReportFX\" Q1=\"12398\" "; myString += "Q2=\"7654\" Q3=\"5678\" Q4=\"9087\"/>"; myString += "<ROW Product=\"Image Toppings\" Q1=\"8742\" "; myString += "Q2=\"12358\" Q3=\"14321\" Q4=\"8702\"/>"; myString += "<ROW Product=\"ChartFX IE 2000\" Q1=\"15672\" "; myString += "Q2=\"17529\" Q3=\"16524\" Q4=\"7654\"/>"; myString += "<ROW Product=\"WebBarFX\" Q1=\"6890\" "; myString += "Q2=\"12389\" Q3=\"9804\" Q4=\"10723\"/>"; myString += "<ROW Product=\"WebTreeFX\" Q1=\"11320\" "; myString += "Q2=\"12908\" Q3=\"6521\" Q4=\"7590\"/>"; myString += "</CHARTFX>"; SfxXML.loadXml(myString); chart1.getDataSourceSettings().setDataSource(SfxXML);
|
|