Time for action — powering a chart using JSON data embedded in the page
- Create a copy of
DataStringMethod.html
in theFirstChart
folder and name it asDataStringMethodJSON.html
. - 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>
- 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.
- TensorFlow Lite移動端深度學(xué)習(xí)
- C語言程序設(shè)計基礎(chǔ)與實驗指導(dǎo)
- 我的第一本算法書
- OpenCV for Secret Agents
- Visual Basic程序設(shè)計(第3版):學(xué)習(xí)指導(dǎo)與練習(xí)
- ASP.NET動態(tài)網(wǎng)頁設(shè)計教程(第三版)
- Java程序設(shè)計與計算思維
- Visual Basic程序設(shè)計實驗指導(dǎo)(第4版)
- Microsoft Azure Storage Essentials
- 從零開始學(xué)Android開發(fā)
- 小程序從0到1:微信全棧工程師一本通
- 深入實踐DDD:以DSL驅(qū)動復(fù)雜軟件開發(fā)
- H5+移動營銷設(shè)計寶典
- WCF技術(shù)剖析(卷1)
- Java設(shè)計模式深入研究