Bar Line Chart |
Data Source: JDBC
Please install the latest webAF template updates prior to building this example. For more information about the server-side example templates used by this example, see Web Application Example Templates and Built-in Web Application Templates and Options.
The following example is not meant to be a complete Web application, rather it is to show how to use a particular component(s). The example does not address the issue of immediately freeing up resources when the user navigates off the Web application or closes the Web browser. Any necessary resources created in the example will stay around until the associated HTTPSession times out. If this example is used in a multi-user environment, it is possible to exhaust the available resources until HTTPSessions time out and free up their associated resources.
BarLineChartJSP
.JDBCTableViewExampleControllerServlet.java
file from the Files Tab of the Project Navigator.
File JDBCTableViewExampleControllerServlet.java
contains servlet
code to get a JDBC connection to SAS data and build a relational data model
with a JDBCToTableModelAdapter. You need to import the appropriate graphics
classes to create and define a BarLineChartTableDataModel that uses the
data that is available through the JDBCToTableModelAdapter.
JDBCTableViewExampleControllerServlet.java
file,
import the following classes:
import com.sas.graphics.components.barlinechart.BarLineChartTableDataModel; import com.sas.graphics.components.AnalysisVariable; import com.sas.graphics.components.ClassificationVariable; import com.sas.graphics.components.GraphConstants;
private static final String BARLINE_DATA_MODEL = "barline_data_model";
jdbcQuery
string and change ENTER_QUERY_STRING_HERE
to "select * from sashelp.class".
jdbcQuery
string creates the
JDBCToTableModelAdapter. You must add similar code to create the BarLineChartTableDataModel.
You must then set the JDBCToTableModelAdapter as the model for the BarLineChartTableDataModel,
and finally assign variable roles to the chart data model.
In the following code fragment, the code you must add is shown in bold. The code that is not bold provides a context that shows exactly where you should add your new code.
//Setup the JDBC model adapter JDBCToTableModelAdapter adapter = null; if (session != null){ adapter = (JDBCToTableModelAdapter)session.getAttribute(SAS_MODEL); } if (adapter == null){ try{ //Create the model adapter and set it on the session adapter = new JDBCToTableModelAdapter(sas_JDBCConnection, jdbcQuery); if (session != null){ session.setAttribute(SAS_MODEL, adapter); } } catch(Exception e){ throw new RuntimeException(e); } } //Setup the BarLineChartTableDataModel BarLineChartTableDataModel dataModel = null; if (session != null){ dataModel = (BarLineChartTableDataModel)session .getAttribute(BARLINE_DATA_MODEL); } if (dataModel == null){ try{ // Create a data model for the chart dataModel= new BarLineChartTableDataModel(); if (session != null){ session.setAttribute(BARLINE_DATA_MODEL, dataModel); } } catch(Exception e){ throw new RuntimeException(e); } } // Set the table model adapter on the data model dataModel.setModel(adapter); // Assign variable roles to the data model dataModel.setCategoryVariable( new ClassificationVariable("Age", GraphConstants.SORT_ASCENDING )); dataModel.setBarResponseVariable( new AnalysisVariable("Weight", GraphConstants.STATISTIC_MEAN)); dataModel.setLineResponseVariable( new AnalysisVariable("Height", GraphConstants.STATISTIC_MEAN)); } //Forward the request to the JSP for display RequestDispatcher rd = getServletContext().getRequestDispatcher("/JDBCTableViewExampleViewer.jsp"); rd.forward(request, response);
When you used the WebApp Project Wizard to create this project, you selected
JDBC TableView servlet as the type of initial content for the project.
In response, the Wizard generated servlet code to build a relational data
model, and it generated an JDBCTableViewExampleViewer.jsp
file
containing a <sas:TableViewComposite> element as the viewer for the
data model.
JDBCTableViewExampleControllerServlet.java
file.Replace the existing <sas:TableViewComposite> tag structure in JDBCTableViewExampleViewer.jsp with the following tag structure:
<sas:BarLineChart id="barlineChart1" model="barline_data_model" > <sas:BarLineChartModel> <sas:CategoryAxisModel label="Age in Years" /> <sas:ResponseAxisModel label=" " /> </sas:BarLineChartModel> </sas:BarLineChart>
For more information and options, see the Web Application Example Templates paper.