% /* This is an example of a data-driven chart JSP, using a horizontal resultset (The X-axis values are the resultset column names). This JSP uses the NetCharts objects to build the chart on-the-fly, running inside the Tomcat container on the ISO-BLV-90 Server. The NetCharts developer's area can be found at: http://www.visualmining.com/developers/ The NetCharts API javadocs can be found at: http://www.visualmining.com/developers/api/ncs/index.html Includes: java.util has the Hashtable object and certain other methods java.sql has the data access objects netcharts.server has the chart building objects probly don't need servlet.http, but just in case... */ %> <%@ page import="java.util.*" %> <%@ page import="java.sql.*" %> <%@ page import="javax.servlet.http.*" %> <%@ page import="netcharts.server.api.*" %> <%@ page import="netcharts.server.util.*" %> <% /* GETTING THE DATA INTO THE NETCHARTS CHART OBJECT ================================================ The column names and data rows are first appended onto a string. This string is then loaded into a hash table, where the key exactly matches the key inside the CDX file. The resulting hash table can then be passed to the NetCharts object. */ /* name of the JSP on the netcharts server, in the testbed folder, in the aaMMOCHARTSPre project. This is for the footer code. */ String strFileName = "TEST_Chart_ByCode_SQL.jsp"; /* I want my work organized into solutions and projects, just like how one works in enterprise-level IDEs such as Visual Studio .NET and JBuilder. So, "projects" on the NetCharts server are my "solutions", and folders inside the server's projects are my "projects" that go with that particular solution. */ String strSolution = "aaMMOChartsPre"; String strProject = "testbed"; // The CDX language template that the NetCharts objects will use. String strCDX = "TEST_Chart_ByCode_SQL.cdx"; /* Connection Attributes. Someday soon this stuff will be a collection declared inside an include, so I can just call it like this: String strServer = objConnString["DataMart"].ServerName; */ String strDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String strURL = "jdbc:microsoft:sqlserver://"; String strServer = "SQL-EVT-68"; String strPort = "49603"; String strDB = "SMMDev"; String strUID = "linksqlid"; String strPWD = "linksqlpw"; String strCtype = "cursor"; /* build the connection string using the above attributes. This string is driver-specific. */ String strConn = strURL + strServer + ":" + strPort + ";databaseName=" + strDB + ";selectMethod=" + strCtype + ";"; // the data we want in this example String strQuery = "select * from NETCHARTSTEST_Data"; /* Try to open a connection to the data source */ Connection objConn = null; try { Class.forName(strDriver); objConn = java.sql.DriverManager.getConnection(strConn,strUID,strPWD); } catch(Exception e) { e.printStackTrace(); out.println("
Couldn't connect: " + e.getMessage() + ""); } /* If we have an open connection, we can try to get the data and build the chart */ Statement objStmt = objConn.createStatement(); try { // get the data and metadata ResultSet objRS = objStmt.executeQuery(strQuery); ResultSetMetaData objRSMeta = objRS.getMetaData(); int intColCount = objRSMeta.getColumnCount(); /* Build the data string. When we're finished, the NetCharts object will see a Hashtable that looks something like this: BarLabels = '1970','1980','1990','2000'; DataSet1 = 239.0,525.0,1000.0,2300.0; Each line is a parameter in the CDX language that gets included with the rest of the CDX data in the .CDX file. */ // initialize the local string String strTheData = "BarLabels = "; // append the metadata to the local string for(int intLooper = 1; intLooper <= intColCount; intLooper++) { strTheData = strTheData + "'" + objRSMeta.getColumnName(intLooper); if(intLooper < intColCount) { strTheData = strTheData + "',"; } } strTheData = strTheData + "';\n"; /* append the data row(s) to the string. The while loop will execute once for every data row to be displayed on the chart. In this case, the loop will execute only once. */ int intRow = 0; while(objRS.next()) { intRow = intRow + 1; strTheData = strTheData + "DataSet" + intRow + " = "; // append each column of the row to the local string for(int intLooper = 1; intLooper <= intColCount; intLooper++) { strTheData = strTheData + objRS.getObject(intLooper); if(intLooper < intColCount) { strTheData = strTheData + ","; } } // end this row strTheData = strTheData + ";"; } /* we have our data locally, we can close the data connection */ objRS.close(); objConn.close(); /* load the local string into the Hashtable. In the CDX file, the BarLabels and DataSet1 parameters should be replaced with the key shown in the first parameter of the put method - in this case, keyTHEDATA. keyTHEKEYNAME is my naming convention. */ Hashtable objchartParams = new Hashtable(); objchartParams.put("keyTHEDATA", strTheData); /* Create a NetCharts object and load the Hashtable into it. The 'request' parameter is a javax.servlet.http.HttpServletRequest object, and is apparently fixed. 'true' means, include the interactive Javascript for the chart image map. Setting this to 'false' always gives me errors. I've left off the last parameter, which is optional and is the URL to place in the HREF attribute of the imagemap tags if no URL has been configured. */ NSWebToolKit objtoolKit = new NSWebToolKit(strSolution + "\\" + strProject); String strChart = objtoolKit.getChartAsImageAndMap ( strCDX, objchartParams, request, true ); // We're done; display the chart out.println(strChart); } catch(Exception e) { e.printStackTrace(); out.println("
Query Failed: " + e.getMessage() + ""); } %>