Combining FusionCharts, JSP & JavaScript (dataURL) method |
FusionCharts v3 offers tremendous integration capabilities with JavaScript. You can easily use FusionCharts and JavaScript to create client side dynamic charts. Here, we'll see the JavaScript + URL method - where we ask the chart to fetch new data from server and update itself, without incurring any page refreshes. The entire application resides in a single page which makes it a seamless experience for your end viewers. Before you proceed with the contents in this page, we strictly recommend you to please go through the sections "How FusionCharts works?" and "Basic Examples", as we'll directly use a lot of concepts defined in those sections. The code discussed in this example is present in Download Package > Code > JSP > DB_JS_dataURL folder. |
Mission for this example |
Let us first define what we want to achieve in this example. We'll carry on from our previous drill-down example and convert it into a single page example. In our previous example, we were showing the Production Summary of all the factories in a pie chart. When the user clicked on a pie slice, he was taken to another page, where a detailed date-wise chart was shown for the required factory. In this example, we'll assimilate both the charts in a single page and make them interact with each other using JavaScript, thereby making the end-user experience smooth. Effectively, we will do the following:
|
Creating the charts container page |
Both the charts and JavaScript functions to manipulate the charts is contained in Default.jsp. It has the following code: |
<%@ include file="../Includes/DBConn.jsp"%> <%@ page import="java.sql.Statement"%> <% The entire app (page) can be summarized as under. This app shows the break-down The XML data for the pie chart is fully created in JSP at run-time. JSP interacts <SCRIPT LANGUAGE="JavaScript"> /** //Sometimes, the above URL and XML data gets cached by the browser. //URLEncode it - NECESSARY. //Get reference to chart object using Dom ID "FactoryDetailed" String strQuery = ""; //strXML will be used to store the entire XML document generated //Generate the chart element //Iterate through each factory String factoryId = null; while (rs1.next()) { //Create the chart - Pie 3D Chart with data from strXML |
Before we get to the JavaScript functions, let's first see what we're doing in our JSP Code. We first create the XML data document for Pie chart - summary of factory output. For each <set>, we provide a JavaScript link to the updateChart() function and pass the factory ID to it as shown in the line below: strXML += "<set label='" + factoryName + "' value='" +totalOutput+ "' We now create the Pie 3D chart using dataXML way and render it. The Pie 3D chart has its DOM Id as FactorySum: <jsp:include page="../Includes/FusionChartsRenderer.jsp" flush="true"> Now, we render an empty Column 2D chart with <chart></chart> data initially. We also change the "No data to display." error to a friendly and intuitive "Please select a factory from pie chart above to view detailed data." This chart has its DOM Id as FactoryDetailed. <jsp:include page="../Includes/FusionChartsRenderer.jsp" flush="true"> Note that the registerWithJS parameter for this chart should be set to true. Effectively, our page is now set to show two charts. The pie chart shows the summary data provided to it using dataXML way. The column chart shows the above "friendly" error message. Now, when each pie slice is clicked, the updateChart() JavaScript function is called and the factoryID of the pie is passed to it. This function is responsible for updating the column chart and contains the following code: |
function updateChart(factoryIndex){ //DataURL for the chart var strURL = "FactoryData.jsp?factoryId=" + factoryIndex; //Sometimes, the above URL and XML data gets cached by the browser. //If you want your charts to get new XML data on each request, //you can add the following line: //strURL = strURL + "&currTime=" + getTimeForURL(); //getTimeForURL method is defined below and needs to be included //This basically adds a ever-changing parameter which bluffs //the browser and forces it to re-load the XML data every time. //URLEncode it - NECESSARY. strURL = escape(strURL); //Get reference to chart object using Dom ID "FactoryDetailed" var chartObj = getChartFromId("FactoryDetailed"); //Send request for XML chartObj.setDataURL(strURL); } |
Here,
This completes our front-end for the app. We now just need to build FactoryData.jsp page, which is responsible to provide detailed data to column chart. It contains the following code: |
<%@ include file="../Includes/DBConn.jsp"%> <%@ page import="java.sql.Statement"%> <%@ page import="java.sql.ResultSet"%> <%@ page import="java.text.SimpleDateFormat"%> <% /* This page is invoked from Default.jsp. When the user clicks on a pie slice in Default.jsp, the factory Id is passed to this page. We need to get that factory id, get information from database and then write XML. */ //First, get the factory Id String factoryId=null; //Request the factory Id from Querystring factoryId = request.getParameter("factoryId"); String strXML=""; if(null!=factoryId){ ResultSet rs; String strQuery; Statement st; java.sql.Date date=null; java.util.Date uDate=null; String uDateStr=""; String quantity=""; //Generate the chart element string strXML = "<chart palette='2' caption='Factory " +factoryId+" Output ' subcaption='(In Units)' xAxisName='Date' showValues='1' labelStep='2' >"; //Now, we get the data for that factory strQuery = "select * from Factory_Output where FactoryId=" + factoryId; st=oConn.createStatement(); rs = st.executeQuery(strQuery); while(rs.next()){ date=rs.getDate("DatePro"); quantity=rs.getString("Quantity"); if(date!=null) { uDate=new java.util.Date(date.getTime()); SimpleDateFormat sdf=new SimpleDateFormat("dd/MM"); uDateStr=sdf.format(uDate); } strXML += "<set label='" +uDateStr+"' value='" +quantity+"'/>"; } //Close <chart> element strXML +="</chart>"; try { if(null!=rs){ rs.close(); rs=null; } }catch(java.sql.SQLException e){ System.out.println("Could not close the resultset"); } try { if(null!=st) { st.close(); st=null; } }catch(java.sql.SQLException e){ System.out.println("Could not close the statement"); } try { if(null!=oConn) { oConn.close(); oConn=null; } }catch(java.sql.SQLException e){ System.out.println("Could not close the connection"); } //Just write out the XML data //NOTE THAT THIS PAGE DOESN'T CONTAIN ANY HTML TAG, WHATSOEVER response.setContentType("text/xml"); } %> <%=strXML%> |
In this page, we basically request the factory Id passed to it as querystring, query the database for required data, build XML document out of it and finally write it to output stream. When you now see the application, the initial state would look as under: |
![]() |
And when you click on a pie slice, the following would appear on the same page (without involving any browser refreshes): |
![]() |
This example demonstrated a very basic sample of the integration capabilities possible with FusionCharts v3. For advanced demos, you can see and download our FusionCharts Blueprint/Demo Applications. |