FusionCharts and JavaScript > Example Application |
In this section, we're going to create a simple application to demonstrate the integration of FusionCharts and JavaScript. Our application would be completely built in HTML using HTML, JavaScript and FusionCharts. We recommend that you please go through the previous topics in this section, if you've not already gone through them. This example uses a lot of concepts explained in previous topics. Our application would look as under once we're done: |
The code for this application is present in Download Package > Code > JavaScript > ClientSideData folder. |
Application Description |
As you can see in the image above:
Before we get to the code of the application, let's first see the process flow. |
Process Flow |
The process flow for this application can be enlisted as under:
Let's now see the code for this application. |
Code |
The code for the above application is present in Chart.html and can be listed as under: |
<HTML> /** |
As you can see above, we're first rendering the FORM. This form allows the user to select the products which they want to plot on the chart. We also present some chart configuration options in the same form: |
<FORM NAME='productSelector' Id='productSelector' action='Chart.html' method='POST'> <INPUT TYPE='Checkbox' name='ProductA' onClick="JavaScript:updateChart('chart1Id');" checked> Product A <INPUT TYPE='Checkbox' name='ProductB' onClick="JavaScript:updateChart('chart1Id');" checked> Product B <INPUT TYPE='Checkbox' name='ProductC' onClick="JavaScript:updateChart('chart1Id');" checked> Product C <INPUT TYPE='Checkbox' name='ProductD' onClick="JavaScript:updateChart('chart1Id');" checked> Product D <B>Chart Configuration:</B> <INPUT TYPE='Checkbox' name='AnimateChart'>Animate chart while changing data? <INPUT TYPE='Checkbox' name='ShowValues' onClick="JavaScript:updateChart('chart1Id');" checked>Show Data Values? </FORM> |
We've defined the onClick event for each checkbox, so that when they change, they invoke the updateChart JavaScript function. After the form, we've created a 3D Column chart with DOM Id as chart1Id. |
<div id="chart1div"> FusionCharts </div> <script language="JavaScript"> var chart1 = new FusionCharts("../../FusionCharts/FCF_MSColumn3D.swf", "chart1Id", "600", "400"); //loading XML data into variable strXML using generateXML() method var strXML=generateXML(this.document.productSelector.AnimateChart.checked); chart1.setDataXML(strXML); chart1.render("chart1div"); </script> |
This is all about the HTML part of the application. Now, let's get to the JavaScript side of story. We begin with including FusionCharts.js file, which provides all the wrapper functions to deal with FusionCharts. Thereafter, we define our data for this application in JavaScript arrays: |
//We store all our data in an array data. It contains data for three Products |
We also keep another array to store color values for each column representing each product : |
//the array of colors contains 4 unique Hex coded colours (without #) for the 4 products |
Before moving to updateChart() function, let's first see the other two functions: generateXML() and getProductXML(). getProductXML function basically takes in the numeric index of a product and returns the XML data document for data pertinent to that product. The data is returned in multi-series XML format, as we're using a multi-series 3D Column Chart. |
function getProductXML(productIndex){ var productXML; //Create <dataset> element //and color vaules from 'colors' array defined above productXML = "<dataset seriesName='" + data[productIndex][0] + "' color='"+ colors[productIndex] +"' >"; //Create set elements for (var i=1; i<=4; i++){ productXML = productXML + "<set value='" + data[productIndex][i] + "' />"; } //Close <dataset> element productXML = productXML + "</dataset>"; //Return return productXML; } |
generateXML function generates the full XML data document for the selected products and returns it. It also reads the chart configuration parameters from FORM elements and then puts it in XML Data document. |
function generateXML(animate){ //Variable to store XML var strXML; //<graph> element //Added animation parameter based on animate parameter //Added value related attributes if show value is selected by the user strXML = "<graph numberPrefix='$' decimalPrecision='0' animation='" + ((animate==true)?"1":"0") + "' " + ((this.document.productSelector.ShowValues.checked==true) ? ("showValues='1' rotateValues='1'"):(" showValues='0' ")) + "yaxismaxvalue='800000'>"; //Store <categories> and child <category> elements strXML = strXML + "<categories><category name='Quarter 1' /><category name='Quarter 2' /><category name='Quarter 3' /><category name='Quarter 4' /></categories>"; //Based on the products for which we've to generate data, generate XML strXML = (this.document.productSelector.ProductA.checked==true)?(strXML + getProductXML(0)):(strXML); strXML = (this.document.productSelector.ProductB.checked==true)?(strXML + getProductXML(1)):(strXML); strXML = (this.document.productSelector.ProductC.checked==true)?(strXML + getProductXML(2)):(strXML); strXML = (this.document.productSelector.ProductD.checked==true)?(strXML + getProductXML(3)):(strXML); //Close <graph> element; strXML = strXML + "</graph>"; //Return data return strXML; } |
Finally we've the updateChart() function, which is the main function responsible for updating the chart. This function is invoked when the user changes the state of any checkbox in the form. In this function:
|
function updateChart(domId){ //Update it's XML - set animate Flag from AnimateChart checkbox in form //using updateChartXML method defined in FusionCharts JavaScript class updateChartXML(domId,generateXML(this.document.productSelector.AnimateChart.checked)); } |
And that's it - this marks the end of code required for this application. When you now view this application, you'll get exactly what you were looking for. |