Creating your First Chart > Creating your First Chart |
||||||||||||
Here, we'll guide you through the process of creating your first chart. For a head start, we'll create a simple angular gauge (speedometer chart) to visually depict "Customer Satisfaction %" for a fictional company. Like FusionCharts, to create any chart using FusionWidgets, you need to assemble four things:
Before we get on to assemble the above items, let's create a new folder that will serve as our playground for trials and researches. Create a new folder named as c:\FusionWidgets on your computer. We'll keep all our examples within this folder. For our first chart example, we'll create another folder within the above folder. Name the new folder as MyFirstChart so that the structure now looks like c:\FusionWidgets\MyFirstChart. |
||||||||||||
All examples discussed in the documentation are present in Download Package under Code folder. | ||||||||||||
Setting the Chart SWF & JavaScript class | ||||||||||||
Now, to build the chart, we need to copy chart SWF files within this folder. Before that, create another folder Charts under c:\FusionWidgets, so that the new folder looks like c:\FusionWidgets\Charts. This folder would be our container for all the chart SWF files. Copy all the chart SWF files (from Download Package > Charts folder) into this newly created folder (c:\FusionWidgets\Charts\). Also copy FusionCharts.js file (from Download Package > JSClass) into this folder. This JavaScript file contains the class to help us easily embed the charts in our HTML page. This folder serves as the central location for charts. All our examples would use the charts contained in this single folder. |
||||||||||||
Even when you're using FusionWidgets on your website, it's a good idea to keep all the chart SWF files and FusionCharts JavaScript class file in a root level folder named as FusionCharts or Charts. This makes sure that you do not have multiple copies of the same chart SWF. So, updates of charts become easier, as you just have to copy-paste the new charts at one location. | ||||||||||||
Now that the SWF files are set up, we move on to see how to create the XML data for our chart. |
||||||||||||
Creating the XML data document | ||||||||||||
Before we build the chart, we first need to have the data that we'll present on the chart. Since we're plotting customer satisfaction index for a fictional company, let us first define the scales for measuring this index. The scales would look as under in a tabular form: |
||||||||||||
|
||||||||||||
Let us assume that, as a result of a survey, the company found that 92% of its customers are satisfied with it. So, we need to depict the above scales and this value in our angular gauge. Now, as mentioned earlier, FusionWidgets necessarily needs its data in pre-defined XML format. It cannot read any other format (including Excel, CSV or text data) apart from XML. So, we need to convert this data into XML. The converted XML data would look as under: |
||||||||||||
<chart lowerLimit='0' upperLimit='100' lowerLimitDisplay='Bad' upperLimitDisplay='Good' gaugeStartAngle='180' gaugeEndAngle='0' palette='1' numberSuffix='%' tickValueDistance='20' showValue='1'> <colorRange> <color minValue='0' maxValue='75' code='FF654F'/> <color minValue='75' maxValue='90' code='F6BD0F'/> <color minValue='90' maxValue='100' code='8BBA00'/> </colorRange> <dials> <dial value='92' rearExtension='10'/> </dials> </chart> |
||||||||||||
Punch the above code in a text editor (e.g., Notepad) and save it as Data.xml under c:\FusionWidgets\MyFirstChart folder. And yeah - don't worry about whatever spaghetti stuff you have just written - we'll soon cover them. Basically, what we've done above can be listed in the following points:
Just as a measure to check if the XML document is structurally valid, open the file in your browser. You should be able to see the XML data document in a formatted way, without any errors. And now, if you're running out of your patience to see this data in chart
format, let's quickly build the HTML container for this chart. |
||||||||||||
Creating the HTML container for chart | ||||||||||||
Each chart needs to be embedded in an HTML file before it can be viewed. So, to view our chart, we'll have to create an HTML wrapper for this one too. Pull up your text editor again and throw in the following code: | ||||||||||||
<html> |
||||||||||||
Save this file as MyFirstChart.html under MyFirstChart folder.
The code in bold above is the actual code that you need to place in your page to embed a FusionCharts chart. Here, we first include the FusionCharts JavaScript class file using: |
||||||||||||
<script language="JavaScript" src="Charts/FusionCharts.js"></script> | ||||||||||||
Create a DIV in the page body with a unique id (chartdiv in example above). |
||||||||||||
<div id="chartdiv" align="center">...</div> | ||||||||||||
Instantiate a chart using the following code with width as 400 and height as 200. Also, we've specified the chart's ID as myChartId. |
||||||||||||
var myChart = new FusionCharts("Charts/AngularGauge.swf", "myChartId", "400", "200", "0", "0"); | ||||||||||||
Here, myChart is the name of the JavaScript object that contains reference to our chart. As parameters, we pass:
|
||||||||||||
Convey the XML data path to the chart using: |
||||||||||||
myChart.setDataURL("Data.xml"); | ||||||||||||
Finally, render the chart by calling render() method of the class and specifying the Id of the DIV which we want the chart to occupy. | ||||||||||||
myChart.render("chartdiv"); | ||||||||||||
And, it's now time to fructify the efforts that you have put into creating your first chart. Open MyFirstChart.html in your preferred browser. You should now see a chart like this: | ||||||||||||
And we just proved how easy it is to create a chart using FusionWidgets. | ||||||||||||
Troubleshooting | ||||||||||||
If for some reasons, you do not see a chart similar to one shown above, run through the following checks: If you see an endless loading progress bar in your browser, or if the right click menu (right click at the place where the chart is supposed to be) shows "Movie not loaded", check the following:
If you get a screen showing "The chart will appear within this DIV. This text will be replaced by the chart.", make sure that:
If you get an "Error in Loading Data" message, check the following:
If you get an "Invalid XML Data" message, it means that the XML data document is malformed. Check it again for common errors like:
To check whether your final XML is ok, open it in your browser and you'll see the error. |