Using FusionCharts with JSP > Basic Examples |
|||||||||||||||||||||||||||
|
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:
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: |
|||||||||||||||||||||||||||
| <chart caption='Monthly Unit Sales'
xAxisName='Month' yAxisName='Units' showValues='0' formatNumberScale='0'
showBorder='1'> <set label='Jan' value='462' /> <set label='Feb' value='857' /> <set label='Mar' value='671' /> <set label='Apr' value='494' /> <set label='May' value='761' /> <set label='Jun' value='960' /> <set label='Jul' value='629' /> <set label='Aug' value='622' /> <set label='Sep' value='376' /> <set label='Oct' value='494' /> <set label='Nov' value='761' /> <set label='Dec' value='960' /> </chart> |
|||||||||||||||||||||||||||
|
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 function named as createChartHTML(). This function is contained in Download Package > Code > JSP > Includes > FusionCharts.jsp file. So, whenever you need to work with FusionCharts in JSP, just include this file in your page and then you can work with FusionCharts very easily. The same functionality is also present in org.infosoftglobal.fusioncharts.FusionChartsCreator.java and it can be imported and used. We will see how to do this later. Let's see it in example. BasicChart.jsp contains the following code to render the chart: |
|||||||||||||||||||||||||||
| <%@ include file="../Includes/FusionCharts.jsp"%> <HTML> <HEAD> <TITLE>FusionCharts - Simple Column 3D Chart</TITLE> </HEAD> <BODY> <% //Create the chart - Column 3D Chart with data from Data/Data.xml String chartHTMLCode=createChartHTML("../../FusionCharts/Column3D.swf", "Data/Data.xml", "", "myFirst", 600, 300, false) ; %> <%=chartHTMLCode%> </BODY> </HTML> |
|||||||||||||||||||||||||||
|
As you can see above, we've first included FusionCharts.jsp to help us easily create charts. After that, we've simply invoked the createChartHTML function to render the chart. To this function, you need to pass the following parameters (in same order): |
|||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||
|
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. |
|||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||
Next, we will see how to use FusionChartsCreator.java class instead of functions from FusionCharts.jsp. Using this Java Class is a better better way of creating charts in your applications. However, for the sake for simplicity, in our examples, we'll stick to include function. BasicChartUsingFusionChartsClass.jsp contains the following code to render the chart: |
|||||||||||||||||||||||||||
|
<% |
|||||||||||||||||||||||||||
So, you just saw how simple it is to create
a chart using JSP and FusionCharts. For further information about the 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: | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
|
As you can see above, we:
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 technical issue on behalf of Microsoft. 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: |
|||||||||||||||||||||||||||
| <%@ include file="../Includes/FusionCharts.jsp"%> <HTML> <HEAD> <TITLE>FusionCharts - Simple Column 3D Chart</TITLE> <SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT> </HEAD> <BODY> <% //Create the chart - Column 3D Chart with data from Data/Data.xml String chartCode= createChart("../../FusionCharts/Column3D.swf", "Data/Data.xml", "", "myFirst", 600, 300, false, false); %> <%=chartCode%> </BODY> </HTML> |
|||||||||||||||||||||||||||
|
As you can see above, we've:
The createChart() method takes in the following parameters: |
|||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||
| When you now view the chart, you'll see that no activation is required even in Internet Explorer. |