| Creating your First Chart | ||||||||||||
To get you started with FusionCharts, we'll show you how to build a simple 3D column chart showing "Weekly Sales Summary" for an arbitrary month. Once completed, the chart would look as under (with animation and interactivity, which is not reflected by the image below):
Following steps are involved in creating the above chart:
And that completes the recipe! Open the file weekly-sales.html in a Web Browser » Internet Explorer, Firefox, Opera or Safari (Mac/iPhone/iPad/Windows) and you will see an animated Column 3D chart similar to the one below:
See it live! Code examples discussed in this section are present in Download Package > Code > MyFirstChart folder. |
||||||||||||
| How it works? | ||||||||||||
|
Now that you've already rendered a chart, let's get behind the scenes and understand how FusionCharts renders charts in a web-page. Essentially, to create a chart, you need the following 4 items:
What happens if Flash player is not available? |
||||||||||||
| Explanation of chart data | ||||||||||||
Let's take a closer look at the data and its XML form: |
||||||||||||
|
||||||||||||
Basically, what we've done above can be listed in the following points:
For creating manual XML data, you can use the visual XML Generator Utility, which converts tabular data into XML. For instructions on using the XML Generator Utility refer to "Guide for General Users" section. Note that you can also provide chart data in JSON format. View an example of JSON data here. Or, to learn more about FusionCharts JSON data format, please go through FusionCharts data formats > JSON section. |
||||||||||||
| Explanation of HTML & JavaScript code used to embed the chart | ||||||||||||
In the HTML wrapper file (or your web page), the shots are called by the included JavaScript class file FusionCharts.js. Let's take a second look at the HTML code to gain an understanding of how it works. <html>
<head>
<title>My First chart using FusionCharts</title>
<script type="text/javascript" src="FusionCharts/FusionCharts.js">
</script>
</head>
<body>
<div id="chartContainer">FusionCharts will load here!</div>
<script type="text/javascript"><!--
var myChart = new FusionCharts( "FusionCharts/Column3D.swf",
"myChartId", "400", "300", "0", "1" );
myChart.setXMLUrl("Data.xml");
myChart.render("chartContainer");
// -->
</script>
</body>
</html>
|
||||||||||||
In the very beginning, the JavaScript class file FusionCharts.js is included into the HTML using the code shown below. FusionCharts.js is smart enough to automatically load the other required JavaScript files - jquery.min.js and highcharts.js on-demand. |
||||||||||||
<script type="text/javascript" src="FusionCharts/FusionCharts.js"></script> |
||||||||||||
Next, a HTML DIV with id chartContainer is created in the body of the page. |
||||||||||||
<div id="chartContainer">FusionCharts will load here!</div> |
||||||||||||
Now comes the essential part, which instantiates the chart. This is called the chart constructor. |
||||||||||||
var myChart = new FusionCharts( "FusionCharts/Column3D.swf", "myChartId",
"900", "300", "0", "1" ); |
||||||||||||
Here, myChart is the name of the JavaScript object (variable) that contains reference to the chart. The following parameters are passed on to the MyChart object:
The code below provides the reference (relative URL) of chart data file (XML in this case). The path of the XML file is set using setXMLUrl() function as shown below: |
||||||||||||
myChart.setXMLUrl( "Data.xml" ); |
||||||||||||
The code sample above uses Url of static XML file. Ideally, you would NOT use a physical data file. Instead you'll have 'your own server side scripts to use dynamic data and build XML. You can always provide the Url of the script to virtually relay the dynamically built data. Finally, the render() method is called and the ID of HTML DIV, where the chart is meant to be rendered, is provided. This line of code renders the desired chart inside the DIV. |
||||||||||||
myChart.render( "chartContainer" ); |
||||||||||||
|
Existing users : You might be wondering what happened to the functions like setDataURL()which you have already been using in your application. Yes - although deprecated, it will continue to work without any problem. |
||||||||||||
| Compact rendering method | ||||||||||||
Since v3.2, FusionCharts also provides alternate methods of declaring and rendering a chart using JavaScript. You can use a compact single-line of JavaScript (instead of the three lines we saw in the above sample) to render a chart as shown below: |
||||||||||||
<html>
<head>
<title>My First chart using FusionCharts - Compact Rendering Method</title>
<script type="text/javascript" src="FusionCharts/FusionCharts.js"></script>
</head>
<body>
<div id="chartContainer">FusionCharts will load here!</div>
<script type="text/javascript"><!--
var myChart = FusionCharts.render( "FusionCharts/Column3D.swf",
"myChartId", "400", "300", "chartContainer", "Data.xml" );
// -->
</script>
</body>
</html>
See it live! |
||||||||||||
| The above code uses the static render function of the global FusionCharts object. This function takes all required parameters to render a chart like the chart SWF, chart ID, width & height of chart, div ID where chart will be rendered and reference to chart data.
There are additional ways of rendering a chart as well, which have been explained in Creating charts page in "FusionCharts and JavaScript" section. |
||||||||||||
| Troubleshooting | ||||||||||||
If for any reason, you do not see a chart, 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 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. If you only get "FusionCharts will load here!" text shown, check the following:
|