vinod827

How to put data from database into xml file which can be embedd with swf file to draw fusion chart dynamically?

Recommended Posts

Hi, iam looking to draw the fusion chart on jsp. I worked in Lotus domino technology earlier. There i placed data from view into xml file and then integrate the xml file with swf file.

Same concept i want to use in the j2ee also. iam using struts, hibernate. I want to put the data coming from the database into XML file. Once the xml file has the dynamic data from database, so that i can integrate xml with the swf(fusion chart) to draw the data.

Following is the piece of code on my jsp:-

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

 
<html:html>
<head>
<script type="text/javascript" src="javascript/validation.js"></script>
<script type="text/javascript" src="javascript/dynamicFormElements.js"></script>
<script type="text/javascript" src="javascript/FusionCharts.js"></script>
<!-- <link rel="stylesheet" type="text/css" href="css/color.css" /> -->
<link rel="stylesheet" type="text/css" href="css/common.css" />
<link rel="stylesheet" type="text/css" href="css/standard.css" />
<link rel="stylesheet" type="text/css" href="css/styles.css" />
</head>
<body onload="alternate('thetable')"> 
<html:form action="/reginfo" method="post" enctype="multipart/form-data" focus="ein">

Powered by   <img align="left" src="<%=request.getContextPath()%>/images/java.jpg"></img>
 
 
<html:errors/>
<center>
<table width="400" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<table border="0" cellspacing="2" cellpadding="1" width="100%" >
<tr bgcolor="#488AC7">
<td align="center" colspan="2"><font size="5" color="white"><bean:message key="label.registeration"/></font></td>
</tr> 

<tr><td colspan="2"><p> </p></td></tr>

<tr align="center">
<td align="right"><bean:message key="label.ein"/>:</td> 
<td><html:text property="ein" size="30" maxlength="30" disabled="false"/></td>

</tr> 

<tr align="center">
<td align="right"><bean:message key="label.name"/>:</td> 
<td><html:text property="name" size="30" maxlength="30"/></td>
</tr> 

<tr align="center">
<td align="right"><bean:message key="label.password"/>:</td> 
<td><html:password property="password" size="30" maxlength="30"/></td>
</tr> 

<tr align="center">
<td align="right"><bean:message key="label.age"/>:</td> 
<td><html:text property="age" size="30" maxlength="30" value=""/></td>
</tr> 

<tr align="center">
<td align="right"><bean:message key="label.gender"/>:</td> 
<td><html:radio property="gender" value="Male">Male</html:radio>
<html:radio property="gender" value="Female">Female</html:radio>
</td>
</tr> 

<tr align="center">
<td align="right"><bean:message key="label.city"/>:</td> 
<td><html:text property="city" size="30" maxlength="30" value=""/></td>
</tr> 

<tr><td colspan="2"><p> </p></td></tr>
<tr bgcolor="#488AC7">
<td align="center" colspan="2">
<input type="button" value="Insert" onclick="onButtonClick('insert')"/>
<input type="button" value="Update" onclick="onButtonClick('update')"/>
<input type="button" value="Delete" onclick="onButtonClick('delete')"/>
<input type="button" value="Select" onclick="onButtonClick('select')"/>
<input type="button" value="POP UP" onclick="openPopUp();"/>
<html:reset property="Reset"><bean:message key="button.reset"/></html:reset></td>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="98%" border="0" cellspacing="0" cellpadding="3" align="center">

</table>
<table width="98%" border="0" cellspacing="0" cellpadding="3" align="center">
<tr> 



</tr>
</table>
<table width="80%" border="1" cellspacing="0" cellpadding="0" id="thetable">
<tr>

<td class="bold"> EIN</td>
<td class="bold"> Name</td>
<td class="bold"> Gender</td>
<td class="bold"> City</td>
<td class="bold"> Age</td>
</tr>
<c:forEach var="emp" items="${reg.employeeList}">
<tr>
<td> ${emp.ein}</td> 
<td> ${emp.name}</td> 
<td> ${emp.gender}</td> 
<td> ${emp.city}</td> 
<td> ${emp.age}</td>
</tr>
</c:forEach>

</table>
<table>
<tr>
<td valign="top" class="text" align="center"> 
<div id="chartdiv" align="center"> 
FusionCharts. </div>
<script type="text/javascript">
var chart = new FusionCharts("FusionChart/Pie3D.swf", "ChartId", "350", "250", "0", "0");
chart.setDataURL("xml/Pie3D.xml"); 
chart.render("chartdiv");
</script>
</td>
</tr>
</table>
</center>
</html:form>
</body>
</html:html>

Bolded code above will give you the idea that am i trying to integrate the xml with swf file.

Right the Pie3D.xml contains the hard coded values only.

Pls help me with a sample code explaining how to bring the data from the database into this Pie3D.xml xml file ? So that i can easily integrate it with swf file on jsp.

Thanks in advance.

Share this post


Link to post
Share on other sites

Hi

Please see this sample code. The following function creates xml from the data present in the database. This xml can be provided to the chart.

 
/**
  * The data to be displayed as a flash chart is retrieved from the 
  * factory_master table and the required contents are formatted as a XML
  * string.  The factory_master table is present in the FactoryDB database.
  * The connection to this database is provided by the DBConn class.
  * @param request
  * @param response
  * @return xmlString
  */
 public String createChartElement(HttpServletRequest request,
  HttpServletResponse response) {
  Connection oConn = null;
 Statement st1 = null, st2 = null;
 ResultSet rs1 = null, rs2 = null;
 String strQuery = "";
 String xmlString = "";
 String animateChart = null;
 String factoryId = null;
 String factoryName = null;
 String totalOutput = "";
 String strDataURL = "";
  oConn = new DBConn().getConnection();
 animateChart = request.getParameter("animate");
 if (null == animateChart || animateChart.equals("")) {
  animateChart = "1";
 }
 xmlString = "<chart caption='Factory Output report' subCaption='By Quantity' pieSliceDepth='30' showBorder='1' formatNumberScale='0' numberSuffix=' Units' animation='"
+ animateChart + "'>";
 strQuery = "select * from Factory_Master";
 try {
  st1 = oConn.createStatement();
 } catch (SQLException e1) {
  e1.printStackTrace();
 }
 try {
  rs1 = st1.executeQuery(strQuery);
 } catch (SQLException e1) {
  e1.printStackTrace();
 }
 try {
  while (rs1.next()) {
factoryId = rs1.getString("FactoryId");
factoryName = rs1.getString("FactoryName");
strQuery = "select sum(Quantity) as TotOutput from Factory_Output where FactoryId="
  + factoryId;
st2 = oConn.createStatement();
rs2 = st2.executeQuery(strQuery);
if (rs2.next()) {
 totalOutput = rs2.getString("TotOutput");
}
strDataURL = encodeDataURL("Detailed.action?FactoryId="
  + factoryId, "false", response);
xmlString += "<set label='" + factoryName + "' value='"
  + totalOutput + "' link='" + strDataURL + "' />";
  }
  rs2 = null;
  st2 = null;
 } catch (SQLException e1) {
  e1.printStackTrace();
 }
 xmlString += "</chart>";
 try {
  if (null != rs1) {
rs1.close();
rs1 = null;
  }
 } catch (java.sql.SQLException e) {
  e.printStackTrace();
 }
 try {
  if (null != st1) {
st1.close();
st1 = null;
  }
 } catch (java.sql.SQLException e) {
  e.printStackTrace();
 }
 try {
  if (null != oConn) {
oConn.close();
oConn = null;
  }
 } catch (java.sql.SQLException e) {
  e.printStackTrace();
 }
 return xmlString;
 }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now