NetCharts® Applets and PHP with JDBC/ODBC:

 

The complete PHP code for Sales Data example

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Sales Data Applet</title>
</head>
<body>
<h2>Sales Data</h2>
This JSP page will extract data from an ODBC data source and populate an applet with it.
<p>
<%@ page import = "java.lang.*,java.io.*,java.util.*,java.sql.*" %>
<%
/*
Copyright 2001 by Visual Mining, Inc, Rockville, MD USA
Permission to use, copy, modify, or distribute this software
and its documentation for any purpose is hereby granted.
This notice must appear in all copies and this copyright
permission notice must appear in supporting documentation.
Visual Mining Inc. disclaims all warranties with regard to this software,
including all implied warranties of merchantability and fitness.
In no event shall Visual Mining Inc. be liable for any special, indirect
or consequential damages or any damages whatsoever resulting from
loss of use, data or profits, whether in an action of contract,
negligence or other tortuous action, arising out of or in
connection with the use or performance of this software.

' The purpose of this JSP example is to demonstrate extracting
' data from an ODBC data source and populating a NetCharts
' chart with this data. The basic order of events is as follows:
' - Create the variables
' - Connect to the database and pass in the SQL statement
' - Open the database and extract the data
' - Populate the 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.
*/

// variables for connecting to the ODBC data source
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String serverURLBase = "jdbc:odbc:";
String dbName = "regionalsales";
String sql = "Select region, quarter1, quarter2, quarter3, quarter4 from Sales";

// variables for holding the data from the data source
String ds1 = "25,50,75,100";
String ds2 = "100,200,300,400";
String ds3 = "100,125,150,200";
String ds4 = "100,75,50,25";
String labels = "\"North\", \"South\", \"East\", \"West\"";

Connection connection = null;
Statement statement;
boolean isdata;
ResultSet rs;
int updateCount = 0;

try {
Class.forName(driverName);
} catch (Exception e){
}

try {
// Open connection to db
connection = DriverManager.getConnection(serverURLBase+dbName);

// create statement to handle SQL
statement = connection.createStatement();
isdata = statement.execute(sql);

int loopCount=0;
// This loop handles multiple sql statements or stored procedures
// that return multiple result sets.
do {
loopCount++;
if (loopCount > 50){
// This prevents errors, like with the Symantec
// driver, always returning an update count of 0
break;
}

rs = statement.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
int numColumns = rsmd.getColumnCount();

boolean isdatainrow;
ds1 = ds2 = ds3 = ds4 = labels = "";
for (int rowNum=0; rs.next(); rowNum++){
Vector row = new Vector();

if (rowNum > 0)
{
ds1 += ",";
ds2 += ",";
ds3 += ",";
ds4 += ",";
labels += ",";

}

Object o = null;
for (int i=1; i <= numColumns; i++){
// we would return the actual object, but
// the JDBC-ODBC bridge crashes. :-(
o = rs.getString(i);
row.addElement(o);
}

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);
}
rs.close();
System.gc();
} while (statement.getMoreResults() == true);

// close the statement to clean up cursors.
statement.close();
if (!connection.isClosed())
connection.close();

} catch (Exception e) {
// The JDBC-ODBC bridge sometimes does not recognize when a connection
// has been dropped by the ODBC driver (notably with MS-Excel)
// and closing it in this case will cause a JVM crash. To prevent
// this, we will not explicitly close the connection if an error
// occurs, but we will remove it from our local list of connections.
try {
if (!connection.isClosed())
connection.close();
} catch (Exception ex){
}

}

%>

<!--
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. If,
for instance, an older version of NetCharts 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.
(NetCharts 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, 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);
BottomTics = ("ON",black,"Helvetica",10,0,null);
DataSets = ("Quarter1",null,BAR,4,FILLED),
("Quarter2",null,BAR,4,FILLED),
("Quarter3",null,BAR,4,FILLED),
("Quarter4",null,BAR,4,FILLED);
DwellLabel = ("",black,"Courier",12,0);
GraphType = GROUP;
TopAxisTitle = ("Regional Sales by Quarter",black,"Helvetica",12,0);
HeaderBox = (null,NONE,2,null,TILE,black);
LeftScale = (null,null,null);
LeftTics = ("ON",black,"Helvetica",10,0,null);
BarWidth = 61;
Bar3DDepth = -1;
DataAxis = (BOTTOM,LEFT),(BOTTOM,LEFT);
Grid = (lightgray,null,black,null,TILE);
DwellLabelBox = (yellow,RAISED,3,null,TILE,black);
HeaderActiveLabel = ("","","");
FooterActiveLabel = ("","","");
LeftTitleActiveLabel = ("","","");
RightTitleActiveLabel = ("","","");
Legend = ("Legend",black,"Helvetica",10,0);
LegendAxis = (BOTTOM,LEFT);
LegendLayout = (VERTICAL,RIGHT,0,0,TOPLEFT,-1);
LegendBox = (null,NONE,1,null,TILE,black);
ColorTable = x6c5d94,x999966,x315394,x213321,x00566f,x690931,x515f23;
LeftColor = black;
GridAxis = (BOTTOM,LEFT);
GridLine = (HORIZONTAL,SOLID,1);
BottomColor = black;
BarBorder = (NONE,1,black);
AntiAlias = "ON";
StackLabel = ITEM;
BottomTicLayout = (AUTO,0,1);
BottomAxisTitle = ("Region",black,"Helvetica",12,0);
DwellLabel = ("ON", navy, "Helvetica", 10, 0);
DwellLabelBox = (lightgrey, RAISED, 2);

#Now populate the chart with the dynamic data extracted from the database via JSP;
DataSet1 = <%=ds1%>;
DataSet2 = <%=ds2%>;
DataSet3 = <%=ds3%>;
DataSet4 = <%=ds4%>;
BarLabels = <%=labels%>;
'>
</applet>

</body>
</html>