FusionCharts can effectively be used with JSP to plot dynamic data-driven charts. In this example, we'll show a few basic examples to help you get started.

We'll cover the following examples here:

  1. We'll use FusionCharts in JSP with a pre-built Data.xml (which contains data to plot)
  2. We'll then change the above chart into a single page chart using dataXML method.
  3. Finally, we'll use FusionCharts JavaScript class to embed the chart.

Let's quickly see each of them. Before you proceed with the contents in this page, we strictly recommend you to please go through the section "How FusionCharts works?".

All code discussed here is present in Download Package > Code > JSP > BasicExample folder.

Setting up the charts for use

In our code, we've used the charts contained in Download Package > Code > FusionCharts folder. When you run your samples, you need to make sure that the SWF files are in proper location.

Plotting a chart from data contained in Data.xml

Let's now get to building our first example. In this example, we'll create a "Monthly Unit Sales" chart using dataURL method. For a start, we'll hard code our XML data in a physical XML document Data.xml and then utilize it in our chart contained in an JSP Page (BasicChart.jsp).

Let's first have a look at the XML Data document:

<graph caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' decimalPrecision='0' formatNumberScale='0'>
    <set name='Jan' value='462' color='AFD8F8' />
    <set name='Feb' value='857' color='F6BD0F' />
    <set name='Mar' value='671' color='8BBA00' />
    <set name='Apr' value='494' color='FF8E46' />
    <set name='May' value='761' color='008E8E' />
    <set name='Jun' value='960' color='D64646' />
    <set name='Jul' value='629' color='8E468E' />
    <set name='Aug' value='622' color='588526' />
    <set name='Sep' value='376' color='B3AA00' />
    <set name='Oct' value='494' color='008ED6' />
    <set name='Nov' value='761' color='9D080D' />
    <set name='Dec' value='960' color='A186BE' />
</graph>

This XML is stored as Data.xml in Data Folder under BasicExample folder. It basically contains the data to create a single series chart to show "Monthly Unit Sales". We'll plot this on a Column 3D Chart. Let's see how to do that.

To plot a Chart that consumes this data, you need to include the HTML code to embed a Flash object and then provide the requisite parameters. To make things simpler for you, we've put all this functionality in an JSP named FusionChartsHTMLRenderer.jsp. This jsp is present in Download Package > Code > JSP > Includes. So, whenever you need to work with FusionCharts in JSP, just include this file in your page, where you want to render the chart and then you can work with FusionCharts very easily. Let us see how to use the FusionChartsHTMLRenderer.jsp.

Let's see it in example. BasicChart.jsp contains the following code:

<HTML>
    <HEAD>
        <TITLE>FusionCharts Free - Simple Column 3D Chart</TITLE>
        <style type="text/css">
        <!--
        body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;
        }
        -->
        </style>
    </HEAD>
    <BODY>
        <CENTER>
            <h2><a href="http://www.fusioncharts.com" target="_blank">FusionCharts Free</a> Examples</h2>
            <h4>Basic example using pre-built Data.xml</h4>
            <%
        
            /*
            This page demonstrates the ease of generating charts using FusionCharts.
            For this chart, we've used a pre-defined Data.xml (contained in /Data/ folder)
            Ideally, you would NOT use a physical data file. Instead you'll have
            your own JSP to create the XML data document. Such examples are also present.
            For a head-start, we've kept this example very simple.
            */


            //Create the chart - Column 3D Chart with data from Data/Data.xml
            %>
            <jsp:include page="../Includes/FusionChartsHTMLRenderer.jsp" flush="true">
                <jsp:param name="chartSWF" value="../../FusionCharts/FCF_Column3D.swf" />
                <jsp:param name="strURL" value="Data/Data.xml" />
                <jsp:param name="strXML" value="" />
                <jsp:param name="chartId" value="myFirst" />
                <jsp:param name="chartWidth" value="600" />
                <jsp:param name="chartHeight" value="300" />
                <jsp:param name="debugMode" value="false" />
            </jsp:include>


            <BR>
            <BR>
            <a href='../NoChart.html' target="_blank">Unable to see the chart
            above?</a>
            <BR><H5 ><a href='../default.htm'>&laquo; Back to list of examples</a></h5>
        </CENTER>
    </BODY>
</HTML>

As you can see above, we've included FusionChartsHTMLRenderer.jsp to help us render the chart. To this include, you need to pass the following parameters :

Parameter Type Description
chartSWF String SWF File Name (and Path) of the chart which you intend to plot. Here, we are plotting a Column 3D chart. So, we've specified it as ../../FusionCharts/FCF_Column3D.swf
strURL String If you intend to use dataURL method for the chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method). In this case, we're using Data.xml file, so we specify Data/Data.xml
strXML String If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method). Since we're using dataURL method, we specify this parameter as "".
chartId String Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id.
chartWidth int value Intended width for the chart (in pixels)
chartHeight int value Intended height for the chart (in pixels)
debugMode true/false String Whether to start the chart in debug mode. This feature is not available in Free version.

When you now run this page, you'll see a chart like the one below.

If you do not see a chart like the one below, please follow the steps listed in Debugging your Charts > Basic Troubleshooting section of this documentation.

We will see in our next example, how to use FusionChartsCreator.java instead of FusionCharts.jsp for creating charts easily. Note that this is the preferred way of creating charts.

So, you just saw how simple it is to create a chart using JSP and FusionCharts. Let's now convert the above chart to use dataXML method.

Changing the above chart into a single page chart using dataXML method

To convert this chart to use dataXML method, we create another page BasicDataXML.jsp in the same folder with following code:

<HTML>
    <HEAD>
        <TITLE>FusionCharts Free - Simple Column 3D Chart using dataXML method</TITLE>
        <style type="text/css">
        <!--
        body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;
        }
        -->
        </style>
    </HEAD>
    <BODY>
        <CENTER>
            <h2><a href="http://www.fusioncharts.com" target="_blank">FusionCharts Free</a> Examples</h2>
            <h4>Basic example using dataXML method (with XML data hard-coded in
            JSP page itself)</h4>
            <p>If you view the source of this page, you'll see that the XML data
            is present in this same page (inside HTML code). We're not calling any
            external XML files to serve XML data. dataXML method is
            ideal when you've to plot small amounts of data.</p>
            <%

            /*This page demonstrates the ease of generating charts using FusionCharts.
            For this chart, we've used a string variable to contain our entire XML data.
            
            Ideally, you would generate XML data documents at run-time, after interfacing with
            forms or databases etc.Such examples are also present.
            Here, we've kept this example very simple.
            */
            //Create an XML data document in a string variable

            String strXML="";

            strXML += "<graph caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' decimalPrecision='0' formatNumberScale='0'>";
            strXML += "<set name='Jan' value='462' 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>";


            //Create the chart - Column 3D Chart with data from strXML variable using dataXML method

            %>
            <jsp:include page="../Includes/FusionChartsHTMLRenderer.jsp" flush="true">
                <jsp:param name="chartSWF" value="../../FusionCharts/FCF_Column3D.swf" />
                <jsp:param name="strURL" value="" />
                <jsp:param name="strXML" value="<%=strXML%>" />
                <jsp:param name="chartId" value="myNext" />
                <jsp:param name="chartWidth" value="600" />
                <jsp:param name="chartHeight" value="300" />
                <jsp:param name="debugMode" value="false" />                 
            </jsp:include>

            <BR>
            <BR>
            <a href='../NoChart.html' target="_blank">Unable to see the chart above?</a><BR><H5 ><a href='../default.htm'>&laquo; Back to list of             examples</a></h5>
        </CENTER>
    </BODY>
</HTML>

As you can see above, we:

  1. Create the XML data document in an JSP variable strXML using string concatenation. Here, we're hard-coding the data. In your applications, you can build this data dynamically after interacting with databases or external sources of data.
  2. Include FusionChartsHTMLRenderer.jsp file wherever the chart needs to be rendered.
  3. Pass appropriate parameters to this page. The parameter "strXML" is set with the value present in strXML variable created in step 1.

When you see this chart, you'll get the same results as before.

Using FusionCharts JavaScript class to embed the chart.

If you see the charts from previous examples in the latest versions of Internet Explorer, you'll see a screen as below:

Internet Explorer asks you to "Click and activate..." to use the chart. This is happening because of a patent that Microsoft lost to Eolas. As such, all Flash movies need to be clicked once before you can start interacting with them.

However, the good news is that there's a solution to it. This thing happens only when you directly embed the HTML code of the chart. It would NOT happen when you use JavaScript to embed the chart. To see how to embed using JavaScript at code level, please see Creating Your First Chart > JavaScript Embedding Section.

Again, to make things simpler for you, we've provided an JSP function called createChart() which helps you wrap this JavaScript function, so that you don't have to get your hands dirty with JavaScript, Flash and HTML. This function is contained in the previously used FusionCharts.jsp or FusionChartsCreator.java file.

Let's now quickly put up a sample to show the use of this function. We create another JSP page SimpleChart.jsp to use this function to plot a chart from data contained in our previously created Data.xml file. It contains the following code:

<HTML>
    <HEAD>
        <TITLE>FusionCharts Free - Simple Column 3D Chart</TITLE>
        <%
        /*You need to include the following JS file, if you intend to embed the chart using JavaScript.
        Embedding using JavaScripts avoids the "Click to Activate..." issue in Internet Explorer
        When you make your own charts, make sure that the path to this JS file is correct. Else, you would get JavaScript errors.
        */

        %>
        <SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT>
        <style type="text/css">
        <!--
        body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;
        }
        -->
        </style>
    </HEAD>
    <BODY>
        <CENTER>
            <h2><a href="http://www.fusioncharts.com" target="_blank">FusionCharts Free</a> Examples</h2>
            <h4>Embedding chart using FusionCharts JavaScript class and using dataURL method.</h4>
            <%

            /*This page demonstrates the ease of generating charts using FusionCharts.
            For this chart, we've used a pre-defined Data.xml (contained in /Data/ folder)
            Ideally, you would NOT use a physical data file. Instead you'll have
            your own code virtually relay the XML data document. Such examples are also present.
            For a head-start, we've kept this example very simple.
            */

            //Create the chart - Column 3D Chart with data from Data/Data.xml

            %>
            <jsp:include page="../Includes/FusionChartsRenderer.jsp" flush="true">
                <jsp:param name="chartSWF" value="../../FusionCharts/FCF_Column3D.swf" />
                <jsp:param name="strURL" value="Data/Data.xml" />
                <jsp:param name="strXML" value="" />
                <jsp:param name="chartId" value="myFirst" />
                <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>
            <BR>
            <BR>
            <a href='../NoChart.html' target="_blank">Unable to see the chart above?</a><BR><H5 ><a href='../default.htm'>&laquo; Back to list of             examples</a></h5>
        </CENTER>
    </BODY>
</HTML>

As you can see above, we've:

  1. Included FusionCharts.js file, which is required when using the JavaScript method.
  2. Included FusionChartsRenderer.jsp file wherever the chart needs to be rendered.
  3. Passed appropriate parameters to this include.

The included page FusionChartsRenderer.jsp is present in the Includes folder. It expects the following parameters:

Parameter Type Description
chartSWF String SWF File Name (and Path) of the chart which you intend to plot. Here, we are plotting a Column 3D chart. So, we've specified it as ../../FusionCharts/FCF_Column3D.swf
strURL String If you intend to use dataURL method for the chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method). In this case, we're using Data.xml file, so we specify Data/Data.xml
strXML String If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method). Since we're using dataURL method, we specify this parameter as "".
chartId String Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id.
chartWidth int Intended width for the chart (in pixels)
chartHeight int Intended height for the chart (in pixels)
debugMode true/false String Whether to start the chart in debug mode. This feature is not available in Free version.
registerWithJS boolean Whether to register the chart with JavaScript. This feature is not available in Free version

These parameters are similar to the parameters expected by FusionChartsHTMLRenderer.jsp. debugMode and registerWithJS are not used in Free version. The only difference is FusionChartsRenderer.jsp uses javascript to render the chart. When you now view the chart, you'll see that no activation is required even in Internet Explorer.