Pie 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.
PieChartJSP
.JDBCDefaultExampleControllerServlet.java
file from the
Files Tab of the Project Navigator.
The JDBCTableViewExampleControllerServlet.java
file contains servlet
code to get a JDBC connection to SAS data and build a relational data model
with a JDBCToTableModelAdapter.
Just below the existing import statements at the top of the
JDBCTableViewExampleControllerServlet.java
file,
import the following classes:
import com.sas.graphics.components.piechart.PieChartTableDataModel; import com.sas.graphics.components.AnalysisVariable; import com.sas.graphics.components.ClassificationVariable;
Scroll down to the global Web application strings and add the following string:
private static final String PIE_DATA_MODEL = "pie_data_model";
jdbcQuery
string and change ENTER_QUERY_STRING_HERE
to "select * from sashelp.retail where year gt 1990".
jdbcQuery
string creates the
JDBCToTableModelAdapter. You must add similar code to create the PieChartTableDataModel.
You must then set the JDBCToTableModelAdapter as the model for the PieChartTableDataModel,
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.
//Set up 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); } } //Set up the PieChartTableDataModel PieChartTableDataModel dataModel = null; if (session != null){ dataModel = (PieChartTableDataModel)session.getAttribute(PIE_DATA_MODEL); } if (dataModel == null){ try{ // Create a data model for the chart dataModel= new PieChartTableDataModel(); if (session != null){ session.setAttribute(PIE_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("Year")); dataModel.setResponseVariable( new AnalysisVariable("Retail sales in millions of $")); } //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.
You will also insert the tagging structure needed to suppress the legend.<sas:PieChart id="pieChart1" model="pie_data_model" > <sas:PieChartModel> <sas:LegendModel visible="false" /> </sas:PieChartModel> </sas:PieChart>
For more information and options, see Web Application Example Templates.