Visual Mining contact us | site map | search

Products Solutions Resource Library Services Customers Partners Developers Company
Developers

NetCharts® Applets and PHP with ODBC/JDBC Import

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 an ODBC data source 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 ODBC Data Source

This example extracts data from a small Access database called regionalsales.mdb. This sample database is provided as part of the NetCharts Applets distribution and can be be registered as a System DSN called regionalsales as follows.

  1. Launch the Control Panel.
  2. Select Data Sources (ODBC).
  3. Select the System DSN tab.
  4. Add a "Microsoft Access Driver (*.mdb)" data source
    • Name the data source regionalsales.
    • Select the regionalsales.mdb file. Its default location is
      c:\program files\visual mining\netcharts4.5\Netcharts\examples\dataimport.

Running the Example

The example JSP script can be configured and run using the following steps:

  1. Make sure that regionalsales.mdb is available as a System DSN (see above).
  2. Copy the getSalesDataChart.php file to any subdirectory under the PHP-enabled Web Server. For instance, when using IIS or PWS, and enabling it to run PHP files, it could be in the c:\inetpub\wwwroot directory.
  3. 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 is
    c:\program files\visual mining\netcharts 4.5\netcharts\
    .
  4. Start a web browser and load the PHP page. For example, if the PHP page was installed as in steps 2 and 3 above, the URL would look like http://localhost/getSalesDataChart.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.

  • Connect to the database and pass in the SQL statement
  • Open the database and extract the data
  • Populate variables with the data from the database
  • Close the connection to the database
  • Instantiate the applet and pass the variables in through the NFParamScript parameter.

The first section of code constructs a connection to the ODBC data source and a query, executes the connection and query, and gets the resultset back:

// connect to a DSN "regionalsales" with a username and password of ""
$connect = odbc_connect("regionalsales","","");

// query the Sales table for region, quarter1, quarter2, quarter3, and quarter4
$query = "SELECT * from Sales";

// perform the query
$result = odbc_exec($connect, $query);

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.

// fetch the data from the database row by row
$count = 0;
while(odbc_fetch_row($result)){
$region = odbc_result($result, 2);
$quarter1 = odbc_result($result, 3);
$quarter2 = odbc_result($result, 4);
$quarter3 = odbc_result($result, 5);
$quarter4 = odbc_result($result, 6);
if ($count == 0)
{
$labels = $region;
$ds1 = $quarter1;
$ds2 = $quarter2;
$ds3 = $quarter3;
$ds4 = $quarter4;
}
else
{
$labels = $labels . "," . $region;
$ds1 = $ds1 . "," . $quarter1;
$ds2 = $ds2 . "," . $quarter2;
$ds3 = $ds3 . "," . $quarter3;
$ds4 = $ds4 . "," . $quarter4;
}
$count++;
}

The data base connection can be closed.

// close the connection
odbc_close($connect);

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.

^^back to top^^



© 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: Intermountain Health Care