NetCharts® Applets and PHP with Flat Files
NetCharts Applets can be used in combination with Hypertext Pre-processor (PHP) technology to dynamically produce chart-enabled HTML pages.
This example uses PHP 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 PHP code
The complete PHP source code
Download the PDF of the 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 next to the example PHP file in the file hierarchy of a PHJP enabled web server.
Running the Example
The example JSP script can be configured and run using the following steps:
- Copy the getSalesDataChartFF.php and regionalsales.dat files to any subdirectory under the PHP-enabled Web Server. For instance, when using IIS or PWS on a Windows machine, and enabling it to run PHP files, it could be in the c:\inetpub\wwwroot directory.
- Make a copy of the NetCharts Applets 4.5 netcharts4.jar file under 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 a Windows machine is
c:\program files\visual mining\netcharts 4.5\netcharts\. - Start a web browser and load the PHP page. For example, if the PHP page was installed on a Windows machine as in steps 2 and 3 above, the URL would look like http://localhost/getSalesDataChartFF.php. This points to the root directory of the IIS or PWS web server running on the host machine.
When you load the page, you should see some a page the regional sales chart shown above.
Examining the Code
This PHP script performs the following steps to create chart enabled web page.
- Open the file
- Read file line by line, and parse the data 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 some variables, and opens the flat file. The first line of the file is read and ignored:
*/
// assign filename here. this file needs to be placed in the same
// subdirectory as getSalesDataChartFF.php. otherwise, you will
// need to modify $filename to reflect the path of the data file
// on the host machine.
$filename = "regionalsales.dat";// open file; if not open, will return 'false' and quit.
if (!$file=fopen($filename, "r"))
{
echo ("Could not open file: $filename");
}
else
{
$count = 0;// reading first line to flush it from system.
$text = (fgets ($file,45));
Next, the code loops through each line of the file. The resulting data is placed into 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 read in and parsed, the file is closed.
// looping through file, line by line, to extract data.
// - column1 = label
// - column2 = dataset1 (quarter1)
// - column3 = dataset2 (quarter2)
// - column4 = dataset3 (quarter3)
// - column5 = dataset4 (quarter4)
while ($text = (fgets ($file,45)))
{
// use split(..) to tokenize the string.
$dataArray = split (",", $text);// first row, assign directly to variable
if ($count == 0)
{
$labels = $dataArray[0];
$ds1 = $dataArray[1];
$ds2 = $dataArray[2];
$ds3 = $dataArray[3];
$ds4 = $dataArray[4];
}
// second+ time, append to variable, with comma
else
{
$labels = $labels . "," . $dataArray[0];
$ds1 = $ds1 . "," . $dataArray[1];
$ds2 = $ds2 . "," . $dataArray[2];
$ds3 = $ds3 . "," . $dataArray[3];
$ds4 = $ds4 . "," . $dataArray[4];
}
$count++;
}// close the file.
fclose($file);
}
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 key CDL parameters for this chart are DataSet1-4, and BarLabels. These parameters are assigned variables for their values.
#Now populate the chart with the dynamic data extracted from the database via ODBC;
DataSet1 = <?php echo($ds1);?>;
DataSet2 = <?php echo($ds2);?>;
DataSet3 = <?php echo($ds3);?>;
DataSet4 = <?php echo($ds4);?>;
BarLabels = <?php echo($labels);?>;
When this PHP page runs, it will substitute the values for the variables ds1, ds2,ds3 and labels creating a complete chart definition.
![]()



