Using FusionCharts with RoR - Charting Data from an Array |
In this section, we'll show you how to use FusionCharts and Ruby to plot charts from data contained in Ruby arrays. We'll cover the following examples here:
Before you go further with this page, we recommend you to please see the previous section "Basic Examples" as we start off from concepts explained in that page. |
| All code discussed here is present in Controller : Download Package > Code > RoR > app > controllers > array_example_controller.rb. Rhtml : Download Package > Code > RoR > app > views > array_example folder. |
| Creating a single-series chart from data contained in arrays |
| Let us now create a single-series chart from data contained in arrays. |
|
Controller:array_example_controller.rb
|
In the above example, we first include FusionCharts.js file to enable us embed the chart using JavaScript. Thereafter, we define an Ruby array arrData to store sales data for 6 different products. The array has two columns - first one for data label and the next one for data values. We define a variable strXML to store the entire XML data. To build the XML, we iterate through the array and using string concatenation. Finally, we render the chart using renderChart() function and pass strXML as dataXML. When you view the chart, you'll see a chart as under: |
![]() |
| Creating a multi-series chart from data contained in arrays |
| The code to create a multi series chart can be listed as under: |
|
Controller: array_example_controller.rb
|
In the above example, we first include FusionCharts.js file to enable us embed the chart using JavaScript. Thereafter, we define an Ruby array arrData to store sales data for 6 different products. The array has three columns - first one for data label (product) and the next two for data values. The first data value column would store sales information We define a variable strXML to store the entire XML data. We also define strCategories, strDataCurr and strDataPrev variables to store XML data for categories elements, current year's dataset and previous year's dataset respectively. To build the XML, we iterate through the array and using string concatenation. We concatenate the entire XML finally in strXML. Finally, we render the chart using renderChart() function and pass strXML as dataXML. When you view the chart, you'll see a chart as under: |
![]() |
| In Download Package > Code > RoR > app > controllers > array_example_controller.rb, we've more example codes to create Stacked and Combination Charts too, which have not been explained here, as they're similar in concept. You can directly see the code if you want to. |