NetCharts® Pro: Understanding and Developing
Data Models
In addition to objectizing the various components of a chart, NetCharts Pro also introduces the concept of data models. Data models are object-based representations of the data to be displayed in the chart. They can be used to provide a standard interface into any data source that can be programmatically accessed. The list of data sources available include:
- Any file-based data (XML, CSV, text, etc.)
- Any source available via JDBC (most major databases)
- Any source available via ODBC (accessed via some JDBC-ODBC bridge or JDBC-ODBC driver)
- Any source available via HTTP
- And others
Various components of NetCharts Pro are used to specifically define the data that will be charted. Some examples would be the NFBarSet, NFStockSet, NFLineSet, etc. Each of these components can be initialized using data models, by using the loadDataModel(DataModel model) method of the component. By creating or using an existing data model implementation that defines access to the data, a developer can quickly integrate data from other systems into a NetCharts Pro chart.

NetCharts Pro comes packaged with a number of sample data model implementations that can be used for access to existing data or as a basis for your own data model implementation. To create new data models, a developer will need to implement one of the abstract classes in the netcharts.pro.datamodel package. Within this package are a set of classes designed for the various scenarios in which data can be represented to the components of NetCharts Pro. Each class is designed for a particular data type (One-Dimensional, Two-Dimensional, Time, Stock, etc.) and includes descriptive information in the JavaDoc API documentation to help developers select the correct class to extend and implement.
Developing a Data Model
The NetCharts Pro distribution package contains a sample application that shows a Performance Dashboard. This example uses a custom data model to load file-based data into various NetCharts Pro components to dynamically generate the chart. The example servlet creates a NetCharts Pro NFGraph instance based upon a default templates for each chart type used within the dashboard. The various chart types used within the dashboard include the dial chart, bar chart, bubble chart and line chart. Then the servlet uses the ncpro.examples.datamodel.CSV1DDataModel to load the CSV-based data from the data files into the chart. The NFGraph instance is then used to invoke the NetCharts Pro getDrillDownPage() method to generate the interactive chart image which is returned to the client. The performance dashboard example uses a Model-II architecture in which the image is placed in memory by the servlet and later accessed by another page. (See "NetCharts Pro Servlet Programming.") The following example will describe the creation of the CSV1DDataModel class and the basic steps to create a customer data model.
1. Deciding Which Abstract Data Model Class to Implement
The first step in creating a custom data model is deciding which abstract data model class from the set available (in the netcharts.pro.datamodel package) to implement. The name of each class describes the data type the class is intended to represent. For instance, the NFDataModel1D class should be extended and implemented when the data is one-dimensional, as in bar graph sets (e.g. value, value, value, …). And the NFDataModelStock should be used when representing stock values (i.e. open, low, high, close). Each class includes a description in the JavaDoc API to help developers determine the correct class to implement.
The data being accessed by the ncpro.examples.datamodel.CSV1DataModel class will be used to create chart components such as BarLabels, BarSets and HandValues. This data is of a single dimension, so the CSV1DataModel class should extend the netcharts.pro.datamodel.NFDataModel1D class:
public class CSV1DDataModel extends NFDataModel1D {
2. Implementing Any Abstract Methods
Next, you must implement the abstract methods of the data model class being extended— netcharts.pro.datamodel.NFDataModel1D in this case. The abstract methods are used to load the data being accessed by the custom data model into the chart components. The methods of the netcharts.pro.datamodel.NFDataModel1D class that need to be implemented are:
- public Object getData(int row)—Returns the data value corresponding to this row
- public int getNumPoints()—Returns the total number of points available in the data object
3. Additional Implementation to Load and Access Data
The CSV1DDataModel class contains some properties that are used to identify the rows and columns that contain the data to be retrieved from the CSV-based data. These properties can be set via the constructor:
CSV1DDataModel(InputStream dataObject,
String columnRanges,
String rowRanges,
String fieldDelim)
or via set methods on the class. They are strictly related to the class implementation alone and do not reflect any needed implementation imposed by the NFDataModel classes. Each custom data model implementation may be different based upon the type of data being represented. Two-dimensional data may use a vector of vectors to store the data, or a two-dimensional array. Developers may create additional classes to simplify access to specialized data like stock values.
In the implementation of the CSV1DDataModel class, Vectors are used to store the data after initialization. These Vectors are populated from data that falls within the column and row ranges specified. The following code is from the processString method and loads the Vectors from the data:
while (br.ready()) {
boolean notGonnaUse = false;
String line = br.readLine();
if (line == null)
notGonnaUse = true;
if (valueInRange(lineNumber, rowRanges) == false)
notGonnaUse = true;
if (notGonnaUse) {
lineNumber++;
continue;
}StringTokenizer lt = new StringTokenizer(line, fieldDelim, false);
int fieldNumber = 0;
while (lt.hasMoreTokens()) {
String tok = lt.nextToken();
if (valueInRange(fieldNumber, colRanges) == true) {
// System.out.println(tok);
barData.addElement(tok);
}
fieldNumber++;
}
lineNumber++;
}
After the CSV1DDataModel instance is initialized from the data from the CSV file given via the InputStream, the two NFDataModel1D methods, getNumPoints and getData, are now able to return the proper data. The data is retrieved from the now-initialized Vectors.
public int getNumPoints() {
if (barData == null)
return 0;
else
return barData.size();
}public Object getData(int row) {
if (barData == null) return null;
if (row >= barData.size()) {
System.out.println("Invalid row: "+row);
return null;
}
return barData.elementAt(row);
}
![]()



