Visual Mining contact us | site map | search

Products Solutions Resource Library Services Customers Partners Developers Company
Developers

NetCharts® Pro and a Stock Chart Program (using XML data)

StockChartWithXMLDataModel shows a standalone frame containing a stock chart. The stockchart is populated by a data model that represents an XML data file. The chart is configured first by loading in a template file. Then, this chart customizes the display by modifying data and parameters programmatically using the set() API. This chart also registers itself as a NFChartActionListener so that it can "hear" click events. When the Note 'X' is clicked on, the program is stopped. Note this program requires access to an XML parser. Add a parser such as xerces.jar to the classpath, or run with Sun's 1.4 JVM, which contains a built-in XML parser. The source code is below.

NetCharts Pro Stock Chart Program with XML

Source Code

/*
StockChartWithXMLDataModel
Visual Mining, Inc.

This example shows a standalone frame containing a stock chart.
The stockchart is populated by a data model that represents an XML data file.

The chart is configured first by loading in a template file,
Then, this chart customizes the display by modifying data and parameters programmatically.

This chart also registers itself as a NFChartActionListener so that
it can "hear" click events. When the Note 'X' is clicked on, the program is stopped.

The program can be compiled with a command similar to:
d:\jdk1.3\bin\javac -classpath "d:\ncpro.jar;." StockChartWithXMLDataModel.java

The program can be run with a command similar to:
d:\jdk1.3\bin\java -classpath "d:\ncpro.jar;." StockChartWithXMLDataModel
*/

package ncpro.examples.standalone;

import ncpro.examples.datamodel.*;
import netcharts.pro.charts.stock.*;
import netcharts.pro.common.barset.*;
import netcharts.pro.common.lineset.*;
import netcharts.pro.common.rectangular.*;
import netcharts.pro.common.*;
import netcharts.pro.event.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.net.*;
import netcharts.pro.*;
import javax.swing.*;

public class StockChartWithXMLDataModel implements NFChartActionListener {
/**
* Creates and returns a populated stock chart.
*
* @return the graph object
*/
public NFGraph initializeChart() {
// Create an instance of NFStockchart
NFStockchart sc = new NFStockchart();

// Don't update the underlying graphic until we are done
sc.setAlwaysUpdate(false);

// Load a template and initialize the chart
URL u = this.getClass().getResource("/cdltemplates/stockseparatebarvolume2.cdl");
sc.initializeFromURL(u);

try{
// Open up a stream to the data.
InputStream fis = this.getClass().getResourceAsStream("/exampledata/XMLStockData.xml");

// Create a custom data model for this data
XMLStockDataModel xmlData = new XMLStockDataModel(fis);
int dataSize = xmlData.getNumPoints();
fis.close();

// Get the list of stock data series. For this example, we know
// that our default template has a single stock set already defined
// so we want to get that, and populate it with our new data.
NFStockSeries series = sc.getStockSeries();

NFStockSet sset = (NFStockSet)series.elementAt(0);
if (sset == null) {
// Just in case
sset = new NFStockSet();
series.addElement(sset);
}
// Tell the stock set to get its data from the data model
sset.loadDataModel(xmlData);
// Set the legend name to use
sset.setLegendName("Stock");
// Tell the chart that it needs to reread the stock series
sc.setStockSeries(series);

// Get the list of bar series - we will be showing volume. This
// behaves much like the stock series described above.
NFBarSeries bs = sc.getBarSeries();
NFBarSet bset = (NFBarSet)bs.elementAt(0);

// This is a unique signature for loadDataModel. our defined
// data model "xmlData" extends NFDataModelStock, but that data
// model does not have elements for volume. In this way, we
// can tell the barset to load using the method named in
// the "xmlData.volumeMethod". Conceivably, any object will
// work like this, provided the method takes a single integer
// "row" variable. (See XMLStockDataModel source code to
// understand this better).
bset.loadDataModel(xmlData, xmlData.volumeMethod, dataSize);
bset.setLegendName("Volume");
sc.setBarBorder(null);
sc.setBarSeries(bs);

// Configure a line series to show a moving average
NFLineSeries ls = sc.getLineSeries();
NFLineSet lset = (NFLineSet)ls.elementAt(0);
lset.loadDataModel(xmlData, xmlData.averageMethod, dataSize);
lset.setLegendName("Moving Average");
sc.setLineSeries(ls);

// Configure the bottom axis to show months
NFAxis bottom = new NFAxis();
bottom.setTextFont(new Font("Courier", Font.PLAIN, 10));
NFVector mylabels = new NFVector();
mylabels.loadDataModel(xmlData, xmlData.axisLabelsMethod, dataSize);
NFVector locations = new NFVector();
for (int i=0; i < mylabels.size(); i++) {
Object o = mylabels.elementAt(i);
if (o != null)
locations.addElement(new Integer(i));
}
mylabels.skipNulls(true); // Don't render the nulls.
bottom.setTicLayoutMode(NFAxis.AUTOSKIP);
bottom.setTicLabels(mylabels);
bottom.setTicLocations(locations);
bottom.setScale(0, dataSize-1,1);
bottom.setScrollRange(0, dataSize-1);
sc.setBottomAxis(bottom);

// Configure the legend.
NFLegend legend = sc.getLegend();
legend.setLocation(NFLegend.BOTTOM);
legend.setOrientation(NFLegend.HORIZONTAL);
sc.setLegend(legend);

// Configure a note to be used as a "Close" button
Font f = new Font("Courier", Font.PLAIN, 14);
NFNoteSet ns = new NFNoteSet(new NFModalLabel(true,Color.white,f),
new NFRegion(Color.gray, NFRegion.NONE, 1),
new NFAxisMap(NFAxisMap.PERCENT, NFAxisMap.PIXEL));
ns.addActiveLabel(new NFActiveLabel("Click here to close",null,"CloseWindow"));
ns.setJustify(NFNoteSet.TOPRIGHT);
sc.addNote(new NFNote("X", new NFPoint(100,0)), ns);
sc.addChartActionListener(this);

// Update the chart with all of the changes.
sc.sendUpdate();
} catch (Exception ex){
System.out.println("initializeChart failed: "+ex);
}
return sc;
}

/**
* Implementation of NFChartActionListener. Recognizes when
* user has clicked on the 'X' at the top right of the chart.
*
* @param cae Event information
*/
public void chartActionPerformed(NFChartActionEvent cae) {
if (cae.getType() == NFChartActionEvent.DWELL)
return ; // Not interested in rollovers

NFActiveLabel al = cae.getActiveLabelObject();
if (al != null) {
String target = al.getTarget();
if (target != null && target.equals("CloseWindow"))
System.exit(0);
}
}

/**
* Main method - will display the chart in a standalone frame
*
*/
public static void main(String args[]) {
StockChartWithXMLDataModel chart = new StockChartWithXMLDataModel();
JFrame f = new JFrame("Stock Chart Showing XML Data");
NFGraph stockChart = chart.initializeChart();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(stockChart.getPanel());
f.setSize(stockChart.getPanel().getSize());
f.show();
}
}



© 2008 Visual Mining, Inc. All rights reserved.
1-800-308-0731 | info@visualmining.com | privacy statement | legal
15825 Shady Grove Rd., Suite 20, Rockville, MD 20850 USA

Quote: Maxager Technology