Friday 2 August 2013

Using Fusion Charts in java Web Application

Jar Download : FusionChart Jar
To be kept parallel to Web-Inf :  FusionChart Folder

BasicChart.jsp
****************************Start********************************************

<html>
    <head>
        <title>My First FusionCharts</title>
        <SCRIPT LANGUAGE="Javascript" SRC="../FusionCharts/FusionCharts.js"></SCRIPT>       
    </head>
    <body bgcolor="#ffffff">
         
        First Way :
         
        <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
                codebase="<%=request.getContextPath()%>/FusionCharts/swflash.cab#version=8,0,0,0"
                width="900" height="300" id="Column3D" >
            <param name="movie" value="<%=request.getContextPath()%>/FusionCharts/FCF_Column3D.swf" />
            <param name="FlashVars" value="&dataURL=Data.xml&chartWidth=900&chartHeight=300">
            <param name="quality" value="high" />
            <embed src="<%=request.getContextPath()%>/FusionCharts/FCF_Column3D.swf"
                   flashVars="&dataURL=Data.xml&chartWidth=900&chartHeight=300"
                   quality="high" width="900" height="300"
                   name="Column3D" type="application/x-shockwave-flash"
                   pluginspage="http://www.macromedia.com/go/getflashplayer" />
        </object>
             <hr/>
              
        Second Way :
              
             <%
                String strXML="";
                strXML += "<graph caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' decimalPrecision='0' formatNumberScale='0'>";
                strXML += "<set name='Jan' value='445' color='AFD8F8'/>";
                strXML += "<set name='Feb' value='857' color='F6BD0F'/>";
                strXML += "<set name='Mar' value='671' color='8BBA00'/>";
                strXML += "<set name='Apr' value='494' color='FF8E46'/>";
                strXML += "<set name='May' value='761' color='008E8E'/>";
                strXML += "<set name='Jun' value='960' color='D64646'/>";
                strXML += "<set name='Jul' value='629' color='8E468E'/>";
                strXML += "<set name='Aug' value='622' color='588526'/>";
                strXML += "<set name='Sep' value='376' color='B3AA00'/>";
                strXML += "<set name='Oct' value='494' color='008ED6'/>";
                strXML += "<set name='Nov' value='761' color='9D080D'/>";
                strXML += "<set name='Dec' value='960' color='A186BE'/>";
                strXML += "</graph>";
             %>
             <jsp:include page="FusionChartsRenderer.jsp" flush="true">
                <jsp:param name="chartSWF" value="/FusionCharts/Pie3D.swf" />
                <jsp:param name="strURL" value="" />
                <jsp:param name="strXML" value="<%=strXML %>" />
                <jsp:param name="chartId" value="productSales" />
                <jsp:param name="chartWidth" value="600" />
                <jsp:param name="chartHeight" value="300" />
                <jsp:param name="debugMode" value="false" />
                <jsp:param name="registerWithJS" value="false" />
            </jsp:include>
        <h1>this is a sample</h1>
    </body>
</html>

*********************************************************************************

                                                         preview



Data.xml
****************************Start********************************************
<graph caption='Monthly Sales Summary' subcaption='For the year 2006'
xAxisName='Month' yAxisName='Sales' numberPrefix='$'>
    <set name='January' value='22222' />
    <set name='February' value='19800' />
    <set name='March' value='21800' />
    <set name='April' value='23800' />
    <set name='May' value='29600' />
    <set name='June' value='27600' />
    <set name='July' value='31800' />
    <set name='August' value='39700' />
    <set name='September' value='37800' />
    <set name='October' value='21900' />
    <set name='November' value='32900' />
    <set name='December' value='39800' />

</graph>
****************************************************************************************

FusionChartsRenderer.jsp
****************************Start********************************************

  String chartSWF = request.getContextPath() +request.getParameter("chartSWF");

            String strURL = request.getParameter("strURL");
            String strXML = request.getParameter("strXML");
            String chartId = request.getParameter("chartId");
            String chartWidthStr = request.getParameter("chartWidth");
            String chartHeightStr = request.getParameter("chartHeight");
            String debugModeStr = request.getParameter("debugMode");
            String registerWithJSStr = request.getParameter("registerWithJS");
            int chartWidth = 600;
            int chartHeight = 300;
            boolean debugMode = false;
            boolean registerWithJS = false;
            int debugModeInt = 0;
            int regWithJSInt = 0;
            if (null != chartWidthStr && !chartWidthStr.equals("")) {
                chartWidth = Integer.parseInt(chartWidthStr);
            }
            if (null != chartHeightStr && !chartHeightStr.equals("")) {
                chartHeight = Integer.parseInt(chartHeightStr);
            }
            if (null != debugModeStr && !debugModeStr.equals("")) {
                debugMode = new Boolean(debugModeStr).booleanValue();
                debugModeInt = boolToNum(new Boolean(debugMode));
            }
            if (null != registerWithJSStr && !registerWithJSStr.equals("")) {
                registerWithJS = new Boolean(registerWithJSStr).booleanValue();
                regWithJSInt = boolToNum(new Boolean(registerWithJS));
            }
%>
<div id='<%=chartId%>Div' align='center'>Chart</div>
<script type='text/javascript'>
    var chart_<%=chartId%> = new FusionCharts("<%=chartSWF%>", "<%=chartId%>", "<%=chartWidth%>", "<%= chartHeight%>", "<%= debugModeInt%>", "<%= regWithJSInt%>");
    <%
            if (strXML.equals("")) {
    %>
        //<!-- Set the dataURL of the chart-->
        chart_<%=chartId%>.setDataURL("<%=strURL%>");
    <%} else {%>
        // Provide entire XML data using dataXML method
        chart_<%=chartId%>.setDataXML("<%=strXML%>");
    <%}%>
        //<!-- Finally, render the chart.-->
        chart_<%=chartId%>.render("<%=chartId%>Div");
</script>
<%!
    /**
     * Converts a Boolean value to int value<br>
     *
     * @param bool Boolean value which needs to be converted to int value
     * @return int value correspoding to the boolean : 1 for true and 0 for false
     */
    public int boolToNum(Boolean bool) {
        int num = 0;
        if (bool.booleanValue()) {
            num = 1;
        }
        return num;
    }
%>

************************************************************************************

No comments:

Post a Comment