官术网_书友最值得收藏!

Time for action — powering a chart using JSON data embedded in the page

  1. Create a copy of DataStringMethod.html in the FirstChart folder and name it as DataStringMethodJSON.html.
  2. Change the following lines of code, as highlighted:
    <html>
    <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.setJSONData('{\"chart": {\ "caption": "Harry\'s SuperMart",\ "subcaption": "Revenue by Year",\ "xaxisname": "Year",\ "yaxisname": "Amount",\ "numberprefix": "$"\ },\"data": [{\ "label": "2009",\ "value": "1487500"\ },{\ "label": "2010",\ "value": "2100600"\ },{\ "label": "2011",\ "value": "2445400"\ }]}'); myChart.render("chartContainer");// -->
    </script>
    </body>
    </html>
    
  3. View the page in the browser. You should see the same chart as the previous one.

What just happened?

You changed the setXMLData() function to the setJSONData() function and provided JSON data instead of XML data. Also, note how the apostrophe in Harry's SuperMart was escaped in JavaScript so as to form Harry\'s SuperMart. Otherwise, there would have been a conflict of quotes leading to invalid JavaScript syntax.

You can also provide the JSON data to the setJSONData() method as an object, instead of a string, as shown in the following code:

<html>
<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.setJSONData({ "chart": { "caption": "Harry\'s SuperMart", "subcaption": "Revenue by Year", "xaxisname": "Year", "yaxisname": "Amount", "numberprefix": "$" }, "data": [{ "label": "2009", "value": "1487500" },{ "label": "2010", "value": "2100600" },{ "label": "2011", "value": "2445400" }]});
myChart.render("chartContainer");// -->
</script>
</body>
</html>

Here, we have converted the JSON string to a JavaScript object by removing the enclosing string quotation marks and even the \ character that was used for concatenating the string distributed across multiple lines. And that does it all!

Bingo! You are now adept with the basics of FusionCharts. You have learned how to create a FusionCharts, provide XML or JSON data as either URL or string, and even render the chart using pure JavaScript. Now, we are all set to explore additional charts in FusionCharts. First, we will create a chart with more than one series of data, called a multi-series chart in FusionCharts parlance.

主站蜘蛛池模板: 邹城市| 资中县| 鹤峰县| 泰州市| 扶沟县| 盐边县| 南漳县| 东至县| 安陆市| 报价| 广河县| 天水市| 江门市| 许昌县| 壶关县| 鲁甸县| 衢州市| 盐池县| 冕宁县| 即墨市| 尉犁县| 宜春市| 莒南县| 仙游县| 晋州市| 龙里县| 杭州市| 高要市| 灵丘县| 丰顺县| 正镶白旗| 叙永县| 大关县| 乌拉特后旗| 墨竹工卡县| 湖南省| 中江县| 新巴尔虎右旗| 沙洋县| 广安市| 武宣县|