Select Scatter Chart > Reading data using JavaScript |
|||||||||||||||
In our previous section, we had seen how to submit the list of selected data points from chart to a server side script as XML. Here, we'll see how to read the updated data using JavaScript functions present on the same page. Code samples discussed in this section is present in Download Package > Code > Select_Data_Rtn folder. |
|||||||||||||||
| Enabling JavaScript access to the chart | |||||||||||||||
| To enable JavaScript access to your chart, you first need to set the registerWithJS flag of the chart to 1. | |||||||||||||||
| <div id="chartdiv" align="center">
FusionCharts. </div> <script type="text/javascript"> var chart = new FusionCharts("SelectScatter.swf", "SelectChart", "580", "350", "0", "1"); chart.setDataURL("Data.xml"); chart.render("chartdiv"); </script> |
|||||||||||||||
Notice the last parameter in chart constructor function - it's set to 1. The above code renders a chart in the page with data from Data.xml. We wouldn't actually focus on data here, as we're trying to link this data to JavaScript and not build it. XML data structrue for the chart can be seen in Chart XML Sheet section. Now that the chart is rendered, whenever we need access to chart's data as array, we can simple call the following function on the chart: |
|||||||||||||||
//Get a reference to our chart //Now get the data as array. |
|||||||||||||||
| We first need to get a reference to the chart object. We do so using getChartFromId method. Next, to get the data as array, we simply call the getData method of the chart. This method returns the data of chart in a 2-dimensional array. The structure of the array is as under: | |||||||||||||||
|
|||||||||||||||
| To make things simpler, let's consider the following XML: | |||||||||||||||
<chart palette='3' caption='Investment Analysis Tool' subcaption='Drag over the required investments to select them. You can have multiple selections.' yAxisName='Returns till date' xAxisName='Age (years)' showLegend='1' showNames='1' xAxisMaxValue='10' xAxisMinValue='0' showFormBtn='0' numberSuffix='%' > <categories verticalLineThickness='1'> <dataSet id='EQS' seriesName='Equities' color='0372AB' plotBorderThickness='0' showPlotBorder='1' anchorSides='3'> <dataSet id='MFS' seriesName='Mutual Funds' color='FF9900' showPlotBorder='1' anchorRadius='4'> </chart> |
|||||||||||||||
| Now, from this chart, if you select 3 data points from Equities dataset and 5 from Mutual Funds dataset and then return the data of chart as array using the getData() method of the chart object, the tabular mapping of returned array can be traced as under: | |||||||||||||||
|
|||||||||||||||
If you map this with XML, you'll find the following:
|
|||||||||||||||
| Reading XML Data from the chart | |||||||||||||||
The chart also provides a method to read the updated data in XML format. This method is named as getXMLData() and can be invoked as under: |
|||||||||||||||
//Get a reference to our chart |
|||||||||||||||
| Example Application in Code Download | |||||||||||||||
| Using all this information, we've built a simple example where we data from chart and render it dynamically in tabular format at client side using JavaScript. The code for this is contained in Chart.html in Download Package > Code > Select_Data_Rtn folder. |