NetCharts® Applets and JSP with Flat Files
NetCharts Applets can be used in combination with Java Server Pages (JSP) technology to dynamically produce chart-enabled HTML pages.
This example uses JSP to extract data from a flat file called regionalsales, populate variables with the data, and build an HTML page that contains a NetCharts Applets applet that uses this data.
Configuring the ODBC data source
Running the example
Examining the JSP code
The complete JSP source code
When run, the example will generate a page containing the following barchart applet.

Configuring the Data Source
This example extracts data from a small
ASCII data file
called regionalsales.dat. On a Windows machine, the default location for this file is
c:\program files\visual mining\netcharts 4.5\netcharts\examples\dataimport.
This file should be placed in in the JSP enabled application servers file hierarchy at the root directory of application containing the example JSP. For example, if this example is to be run in the default application of a JRun server on an NT machine, the data file should be placed in
C:\Program Files\Allaire\JRUN\Servers\default\default-app
Running the Example
The example JSP script can be configured and run using the following steps
- Copy regionalsales.dat to the root directory of the application that will contain this example.
- Copy the getSalesDataChartFF.jsp file into the default application of the JSP-enabled Web Application Server. For instance, when using JRun 3.0, the destination would be
c:\program files\allaire\jrun\servers\default\default-app. - Make a copy of the NetCharts Applets 4.5 netcharts4.jar and NFLicense.dat in this same directory. The jar file can be found in the \classes directory under the NetCharts Applets 4.5 installation directory. The default installation directory for NetCharts Applets 4.5 on Windows machines is c:\program files\visual mining\netcharts 4.5\netcharts\.
- Start a web browser and load the JSP page. For example, if the JSP page was installed as in steps 2 and 3 above, the URL would look like http://localhost:8100/getSalesDataChart.jsp. This points to the root directory of the JRun installation, which is listening on port 8100 of the host machine.
When you load the page, you should see some a page the regional sales chart shown above.
Examining the Code
This JSP script performs the following steps to create chart enabled web page.
Define the variables
Open the file
Read file line by line, and parse the data into Vector
Convert the Vectors into their respective variables
Close the connection to the file
Instantiate the applet and pass the variables in through the NFParamScript parameter
The first section of code defines several variables. These variables are used to open the flat file in a BufferedReader, hold the data in a vector, and then prepare the data in string format to be passed to the NetCharts Applets applet:
// variables for connecting to the flat file data source
// Note: this is set up to look in the same directory as the jsp file.
String fileName = "regionalsales.dat";
BufferedReader breader = null;// variables for holding the data from the data source
Vector row = new Vector();
String ds1 = "";
String ds2 = "";
String ds3 = "";
String ds4 = "";
String labels = "";
The next section of code opens the file in a BufferedReader, and prepares the StringTokenizer and variables for parsing the data that is extracted from regionalsales.dat.
try {
// open a bufferedreader to read the file
// get the current path of the jsp under the application server.
// 'application' is a freebie from the parent servlet.
String inputPath = application.getRealPath("/") + File.separatorChar;
breader = new BufferedReader (new FileReader (inputPath + fileName));
String buffer;
StringTokenizer token;
boolean firstLine = true;
int rowNum = 0;
ds1 = ds2 = ds3 = ds4 = labels = "";
Next, the code loops through each line of the file. The first line of the file is read and ignored. Each line is parsed with a StringTokenizer. The resulting data is placed in a Vector. The results are then placed in strings. These strings contain the data in a comma-separated format, which NetCharts Applets can understand. Ultimately, the data will look something like "5,10,15,20". After all of the data is extracted, the file is closed.
// loop through the bufferedreader one line at a time
while ((buffer = breader.readLine()) != null)
{
token = new StringTokenizer (buffer,",");// if it's the first line of the file, ignore
// else, place into tokenizer and parse
if (!firstLine)
{
// place string tokens into row Vector
while (token.hasMoreTokens())
{
row.addElement (token.nextToken());
}// if it's after the first line of data, then tack
// on a comma.
if (rowNum > 0)
{
ds1 += ",";
ds2 += ",";
ds3 += ",";
ds4 += ",";
labels += ",";
}// copy strings from Vector into data strings
labels += (String) row.elementAt(0);
ds1 += (String) row.elementAt(1);
ds2 += (String) row.elementAt(2);
ds3 += (String) row.elementAt(3);
ds4 += (String) row.elementAt(4);row.removeAllElements();
rowNum++;
}
else
{
firstLine = false;
}
The applet tag can now be constructed. NetCharts Applets uses an applet parameter called NFParamScript to pass in chart definition strings. A simple name=value format is used to construct complete chart definitions that can be processed by the NetCharts Applets applet.
Note: The "code=" attribute of the applet is pointing to the netcharts4.apps.* package. This is done to avoid any classpath conflicts that may occur on the system running NetCharts Applets. If, for instance, an older version of NetCharts Applets is installed on the system, and the CLASSPATH environment variable is pointing to that package, then a conflict could occur that might keep this example from running. By pointing specifically to the netcharts4.apps.* package, no conflict will occur. Note that a change will need to be made to the "code=" attribute in all of the applet tags with every major new version upgrade of NetCharts Applets. (NetCharts Applets 5.0, 6.0, etc.)
If this potential classpath conflict is not a concern, then set "code=netcharts.apps.*". This will allow the applets to run across multiple versions of NetCharts Applets, without any changes.
<applet name=Quarterly Sales
code=netcharts4.apps.NFBarchartApp
codebase=/classes
archive=netcharts4.jar
width=600 height=400>
<param name=NFParamScript value='
#Populate the chart with all of the static template information;
ChartName = "Basic Grouped Barchart";
DebugSet = ALL;
ChartWidth = 600;
ChartHeight = 400;
Background = (white,NONE,3,null,TILE,black);
...
The relevant parameters for this chart are DataSet1, DataSet2, DataSet3, DataSet4, and BarLabels:
#Now populate the chart with the dynamic data extracted from the datafile via JSP;
DataSet1 = <%=ds1%>;
DataSet2 = <%=ds2%>;
DataSet3 = <%=ds3%>;
DataSet4 = <%=ds4%>;
BarLabels = <%=labels%>;
When this JSP page runs, it will convert the variables into the strings created earlier. These strings will then be passed in to the applet, and the chart will be created.
![]()



