You are viewing documentation for an older version. For current documentation - click here.

Apart from XML, FusionCharts XT can also accept JSON data format. It can use a JSON URL/feed, a JSON Object (in JavaScript) or JSON String. This functionality is provided by FusionCharts.js JavaScript class.

In this page we will see how to pass JSON data to chart in various forms. We will again convert our first sample weekly-sales.html to implement this.

Code examples and data files discussed in this section are present in Download Package > Code > MyFirstChart folder.

Providing JSON data using URL method

Create a copy of weekly-sales.html and rename it to weekly-sales-json-url.html. Then punch in the following code:

<html>
  <head> 	
    <title>My First chart using FusionCharts XT - JSON data URL</title> 	
    <script type="text/javascript" src="FusionCharts/FusionCharts.js"></script>
  </head>   
  <body>     
    <div id="chartContainer">FusionCharts XT will load here!</div>          
    <script type="text/javascript"><!-- 	

      var myChart = new FusionCharts( "FusionCharts/Column3D.swf", 
		   "myChartId", "400", "300", "0" );
      myChart.setJSONUrl("Data.json");
      myChart.render("chartContainer");
      
    // -->     
    </script> 	   
  </body> 
</html>

See it live!

Many Web servers like IIS6, does not supply .json (no wildcard MIME type) files by default. You will need to setup your Web server to supply json files. In IIS6, you can do this using these steps:
  • Open the properties for the server in IIS Manager and click MIME Types
  • Click "New". Enter "JSON" for the extension and "application/json" for the MIME type
Many browsers restrict JavaScript from accessing local file system owing to security reasons. The JavaScript charts, when running locally, will not be able to access data provided as a URL. If you run the files from a server, it will run absolutely fine, though. When running locally, however, if you provide the data as string (using the Data String method), it works fine.
Click here to see the code using the Data String method »
<html>
  <head> 	
    <title>My First chart using FusionCharts - Using JavaScript</title> 	
    <script type="text/javascript" src="FusionCharts/FusionCharts.js"></script>
  </head>   
  <body>     
    <div id="chartContainer">FusionCharts XT will load here!</div>          
    <script type="text/javascript"><!--

      var myChart = new FusionCharts( "FusionCharts/Column3D.swf", 
		    "myChartId", "400", "300", "0" );

      myChart.setXMLData("<chart caption='Weekly Sales Summary' xAxisName='Week' " +
        "yAxisName='Sales' numberPrefix='$'>" +
          "<set label='Week 1' value='14400' />" +
          "<set label='Week 2' value='19600' />" +
          "<set label='Week 3' value='24000' />" +
          "<set label='Week 4' value='15700' />" +
        "</chart>");

      myChart.render("chartContainer");      
    // -->  
    </script> 	   
  </body> 
</html>

See it live in your iPad or iPhone or any browser where Flash Player is not supported.

In the above code, we have provided a JSON data (compatible with Column Chart) to the chart using the setJSONUrl() function. The data stored in the JSON file is as follows:

{ 
    "chart": { 
          "caption" : "Weekly Sales Summary" ,
          "xAxisName" : "Week",
          "yAxisName" : "Sales",
          "numberPrefix" : "$"
    },
	
    "data" : 
     [
          { "label" : "Week 1", "value" : "14400" },
          { "label" : "Week 2", "value" : "19600" },
          { "label" : "Week 3", "value" : "24000" },
          { "label" : "Week 4", "value" : "15700" }
     ]
}

To know more on FusionCharts XT JSON data format, please go through FusionCharts XT data formats > JSON section. Like the Visual XML Generator, you can take help of FusionCharts Data Converter tool to convert chart XML to JSON or vice-versa and use it.

Providing JSON Object embedded in HTML

You can also provide JSON embedded in the HTML itself. Since JSON format is derived from JavaScript, you can also pass a JavaScript Object as data for the chart. This is possible using setJSONData() function as shown below:

...   
myChart.setJSONData( { 
	"chart": 
	{ 
		"caption" : "Weekly Sales Summary" ,	
		"xAxisName" : "Week", 
		"yAxisName" : "Sales",	
		"numberPrefix" : "$" 
	},

	"data" : 
	[ 
		{ "label" : "Week 1", "value" : "14400" },
		{ "label" : "Week 2", "value" : "19600" }, 
		{ "label" : "Week 3", "value" : "24000" }, 
		{ "label" : "Week 4", "value" : "15700" } 
	]
} );
...

See it live!

Providing JSON String embedded in HTML

Additionally, you can also pass a JSON, stored as a String. In many cases, AJAX feed comes as JSON String, so you do not have to convert the string to JSON. FusionCharts XT takes care of that as shown below:

...
   
myChart.setJSONData ( 
	'{ "chart": { "caption" : "Weekly Sales Summary" , ' +
	'"xAxisName" : "Week", "yAxisName" : "Sales", "numberPrefix" : "$" }, ' +
	'"data" : [ { "label" : "Week 1", "value" : "14400" },  ' +
	'{ "label" : "Week 2", "value" : "19600" }, ' +
	'{ "label" : "Week 3", "value" : "24000" }, ' +
	'{ "label" : "Week 4", "value" : "15700" } ]	} '
);
...

See it live!