Using FusionCharts with JSP > Charting Data from an Array |
In this section, we'll show you how to use FusionCharts and JSP to plot charts from data contained in JSP arrays. We'll cover the following examples here:
Before you go further with this page, we recommend you to please see the previous section "Basic Examples" as we start off from concepts explained in that page. The code examples contained in this page are present in Download Package > Code > JSP > ArrayExample folder. |
Creating a single series chart from data contained in arrays |
The code to create a single series chart is contained in SingleSeries.jsp and can be listed as under: |
<HTML> //Store sales data //Now, we need to convert this data into XML. We convert using string concatenation. //Create the chart - Column 3D Chart with data contained in strXML |
In the above example, we first include FusionCharts.js file to enable us embed the chart using JavaScript. We also include FusionChartsRenderer.jsp wherever we want to render the chart. Thereafter, we define an JSP array arrData to store sales data for 6 different products. The array has two dimensions - first one for data label and the next one for data values. We define a variable strXML to store the entire XML data. To build the XML, we iterate through the array and use string concatenation. Finally, we render the chart by including FusionChartsRenderer.jsp and passing strXML and other parameters. When you view the chart, you'll see a chart as under: |
![]() |
Creating a multi-series chart from data contained in arrays |
Let us now create a multi-series chart from data contained in arrays. We create a file MultiSeries.jsp with the following code: |
<HTML> <BODY> <CENTER> Let's store the sales data for 6 products in our array. We also store //Store sales data //Store sales data for previous year String strXML = "<chart caption='Sales by Product' numberPrefix='$' formatNumberScale='1' rotateValues='1' placeValuesInside='1' decimals='0' >"; //Initialize <categories> element - necessary to generate a multi-series chart //Initiate <dataset> elements //Iterate through the data //Close <categories> element //Close <dataset> elements //Assemble the entire XML now //Create the chart - MS Column 3D Chart with data contained in strXML |
In the above code, we first include FusionCharts.js file to enable us embed the chart using JavaScript. We also include FusionChartsRenderer.jsp wherever we want to render the chart. Thereafter, we define a JSP array arrData to store sales data for 6 different products. The array has three dimensions - first one for data label (product) and the next two for data values. The first data value column would store sales information for current year and the second one for previous year. We define a variable strXML to store the entire XML data. We also define strCategories, strDataCurr and strDataPrev variables to store XML data for categories elements, current year's dataset and previous year's dataset respectively. To build the XML, we iterate through the array and use string concatenation. We concatenate the entire XML finally in strXML. Finally, we render the chart using FusionChartsRenderer.jsp and passing strXML as parameter. When you view the chart, you'll see a chart as under: |
![]() |
In Download Package > Code > JSP > ArrayExample, we've more example codes to create Stacked and Combination Charts too, which have not been explained here, as they're similar in concept. You can directly see the code and understand them easily. |