## Using FusionCharts with RoR - Basic Examples
FusionCharts can effectively be used with Ruby to plot dynamic data-driven charts. In this example, we'll show a few basic examples to help you get started. We'll cover the following examples here: We'll use FusionCharts in Ruby with a pre-built Data.xml (which contains data to plot) We'll then change the above chart into a single page chart using dataXML method. Finally, we'll use FusionCharts JavaScript class to embed the chart. Let's quickly see each of them. Before you proceed with the contents in this page, we strictly recommend you to please go through the section "How FusionCharts works?".
All code discussed here is present in Controller : Download Package > Code > RoR > app > controllers > basic_example_controller.rb. Rhtml : Download Package > Code > RoR > app > views > basic_example folder.
Setting up the charts for use
In our code, we've used the charts contained in Download Package >> RoR >> public >>FusionCharts folder. When you run your samples, you need to make sure that the SWF files are in proper location.
Plotting a chart from data contained in Data.xml
Let's now get to building our first example. In this example, we'll create a "Monthly Unit Sales" chart using dataURL method. For a start, we'll hard code our XML data in a physical XML document Data.xml and then utilize it in our chart contained in Ruby Let's first have a look at the XML Data document:
| <chart caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' showValues='0' formatNumberScale='0' showBorder='1'> <set label='Jan' value='462' /> <set label='Feb' value='857' /> <set label='Mar' value='671' /> <set label='Apr' value='494' /> <set label='May' value='761' /> <set label='Jun' value='960' /> <set label='Jul' value='629' /> <set label='Aug' value='622' /> <set label='Sep' value='376' /> <set label='Oct' value='494' /> <set label='Nov' value='761' /> <set label='Dec' value='960' /> </chart> This XML is stored as Data.xml in Data Folder under basic_example folder. It basically contains the data to create a single series chart to show "Monthly Unit Sales". We'll plot this on a Column 3D Chart. Let's see how to do that. To plot a Chart that consumes this data, you need to include the HTML code to embed a Flash object and then provide the requisite parameters. To make things simpler for you, we've put all this functionality in a function named as renderChartHTML(). Let's see it in example.
Controller:basic_example_controller.rb Action: basicchart class BasicExampleController < ApplicationController def basicchart #Create the chart - Column 3D Chart with data from Data/Data.xml @chart = renderChartHTML("/FusionCharts/Column3D.swf", "/Data/Data.xml", "", "myFirst", 600, 300, false) end end View: <HTML> <BODY> <%= @chart%> </BODY> </HTML>
We've simply invoked the renderChartHTML function to render the chart. To this function, you can pass the following parameters (in same order):
Parameter | Description
chartSWF | SWF File Name (and Path) of the chart which you intend to plot. Here, we are plotting a Column 3D chart. So, we've specified it as /FusionCharts/Column3D.swf
strURL | If you intend to use dataURL method for the chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method). In this case, we're using Data.xml file, so we specify Data/Data.xml
strXML | If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method). Since we're using dataURL method, we specify this parameter as "".
chartId | Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id.
chartWidth | Intended width for the chart (in pixels)
chartHeight | Intended height for the chart (in pixels)
debugMode | Whether to start the chart in debug mode. Please see Debugging your Charts section for more details on Debug Mode.
When you now run this page, you'll see a chart like the one below. If you do not see a chart like the one below, please follow the steps listed in Debugging your Charts > Basic Troubleshooting section of this documentation.
So, you just saw how simple it is to create a chart using Ruby and FusionCharts. Let's now convert the above chart to use dataXML method.
Changing the above chart into a single page chart using dataXML method
To convert this chart to use dataXML method, we create another page
Controller: basic_example_controller.rb Action: basicdataxml class BasicExampleController < ApplicationController def basicdataxml #Create an XML data document in a string variable strXML = "" strXML = strXML + "<chart caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' showValues='0' formatNumberScale='0' showBorder='1'>" strXML = strXML + "<set label='Jan' value='462' />" strXML = strXML + "<set label='Feb' value='857' />" strXML = strXML + "<set label='Mar' value='671' />" strXML = strXML + "<set label='Apr' value='494' />" strXML = strXML + "<set label='May' value='761' />" strXML = strXML + "<set label='Jun' value='960' />" strXML = strXML + "<set label='Jul' value='629' />" strXML = strXML + "<set label='Aug' value='622' />" strXML = strXML + "<set label='Sep' value='376' />" strXML = strXML + "<set label='Oct' value='494' />" strXML = strXML + "<set label='Nov' value='761' />" strXML = strXML + "<set label='Dec' value='960' />" strXML = strXML + "</chart>" #Create the chart - Column 3D Chart with data from strXML variable using dataXML method @chart=renderChartHTML("/FusionCharts/Column3D.swf", "", strXML, "myNext", 600, 300, false) end end
View: <HTML> <BODY> <%= @chart%> </BODY> </HTML>
As you can see above, we: Create the XML data document in an Ruby variable strXML using string concatenation. Here, we're hard-coding the data. In your applications, you can build this data dynamically after interacting with databases or external sources of data. Finally, create the chart and set the dataXML parameter as strXML. We leave dataURL parameter blank. When you see this chart, you'll get the same results as before.
Using FusionCharts JavaScript class to embed the chart.
If you see the charts from previous examples in the latest versions of Internet Explorer, you'll see a screen as below:
Internet Explorer asks you to "Click and activate..." to use the chart. This is happening because of a technical issue on behalf of Microsoft. As such, all Flash movies need to be clicked once before you can start interacting with them. However, the good news is that there's a solution to it. This thing happens only when you directly embed the HTML code of the chart. It would NOT happen when you use JavaScript to embed the chart. To see how to embed using JavaScript at code level, please see Creating Your First Chart > JavaScript Embedding Section. Again, to make things simpler for you, we've provided a function called renderChart() which helps you wrap this JavaScript function in Ruby function, so that you don't have to get your hands dirty with JavaScript, Flash and HTML. Let's now quickly put up a sample to show the use of this function.
Controller: basic_example_controller.rb Action: simplechart class BasicExampleController < ApplicationController def simplechart #Create the chart - Column 3D Chart with data from Data/Data.xml @chart=renderChart("/FusionCharts/Column3D.swf", "/Data/Data.xml", "", "myFirst", 600, 300, false, false) end end View: <HTML> <HEAD> <TITLE> FusionCharts - Simple Column 3D Chart </TITLE> <% =begin 'You need to include the following JS file, if you intend to embed the chart using JavaScript. 'Embedding using JavaScripts avoids the "Click to Activate..." issue in Internet Explorer 'When you make your own charts, make sure that the path to this JS file is correct. Else, you would get JavaScript errors. =end %> <SCRIPT LANGUAGE="Javascript" SRC="/FusionCharts/FusionCharts.js"></SCRIPT> </HEAD> <BODY> <%= @chart%> </BODY> </HTML>
The renderChart() method takes in the following parameters:
Parameter | Description
chartSWF | SWF File Name (and Path) of the chart which you intend to plot. Here, we are plotting a Column 3D chart. So, we've specified it as /FusionCharts/Column3D.swf
strURL | If you intend to use dataURL method for the chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method). In this case, we're using Data.xml file, so we specify Data/Data.xml
strXML | If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method). Since we're using dataURL method, we specify this parameter as "".
chartId | Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id.
chartWidth | Intended width for the chart (in pixels)
chartHeight | Intended height for the chart (in pixels)
debugMode | Whether to start the chart in debug mode. Please see Debugging your Chart Section for more details on Debug Mode.
registerWithJS | Whether to register the chart with JavaScript. Please see FusionCharts and JavaScript section for more details on this.
When you now view the chart, you'll see that no activation is required even in Internet Explorer.