Using FusionCharts with RoR > AJAX Examples |
In this section, we'll use AJAX in Ruby on Rails and generate dynamic charts. We'll do the following:
Before you proceed with the contents in this page, we recommend you to go through the section "Basic Examples ". All code discussed here is present in Controller : Download Package > Code > RoR > SampleApp > app > fusioncharts > controllers > ajax_example_controller.rb. View : Download Package > Code > RoR > SampleApp > app > views > fusioncharts > ajax_example folder. View Helper Modules: Download Package > Code > RoR > SampleApp > lib > fusion_charts_helper.rb Creating the page with factory names drop-down and initial chart. We will create a controller called AjaxExampleController with the action default_factories_chart for this page. The code in the controller and view are given below. Controller: Fusioncharts::AjaxExampleController Action: default_factories_chart class Fusioncharts::AjaxExampleController < ApplicationController # The data for the first chart is obtained and this is used in the corresponding builder and view files def default_factories_chart response.content_type = Mime::HTML @factories = Fusioncharts::FactoryMaster.find(:all) if(@factories.size>0) factory = @factories[0] @ factory_id = factory.id # Default factory factory = Fusioncharts::FactoryMaster.find(@factory_id) @factory_name = factory.name @factory_output_quantities = factory.factory_output_quantities end end View: default_factories_chart.html.erb <% #In this page, we will render two charts. The data for the first chart will be factories data from database #The second chart will be shown when clicked on a pie slice in the first chart. #This chart will show the details of the clicked factory. The following steps are carried out in the controller action:
Let us next, dissect the view page and see it part by part. In order to show the chart, we will render a partial _factory_sum_chart passing it the factory id, name and output quantities. The partial renders the chart using FusionChartsHelper and the factory_details builder file. Here, we will not go into the details of the partial and the builder, since they are similar to those seen in Drill Down example and other examples.The resulting chart is shown below:
On clicking on a slice in the above pie chart, the chart with that factory's details should be shown below. For this, we will use AJAX. As seen in the above code, we have a div with id "detailedChart" where the factory details chart will be shown. Let us analyze the action and its output. We will come to the javascript code in a minute. The code for this action is: Controller: Fusioncharts::AjaxExampleController In this action, the expected parameter is factory_id. A look-up is performed and the factory details for this id is obtained. The factory_output_quantities of the factory is stored in @factory_output_quantities. This is passed to the builder factory_details via locals parameter by the corresponding view factory_chart.html.erb. Note that this action is invoked via ajax in updateChart javascript function. The view renders the chart using FusionChartsHelper's render_chart_html function and the factory_details builder file. The code is as follows: <%
#This page is invoked from default action in controller. When the user clicks on a pie
#slice rendered by default.html.erb, the factoryId is passed as a parameter to detailed function
#in the controller. We need to get that factory id, get the information from database and then show
#a detailed chart.
# The xml is obtained as a string from builder template.
str_xml = render :file=>"fusioncharts/db_example/factory_details",
:locals=>{:factory_id=> @factory_id,:factory_name=>@factory_name,:factory_output => @factory_output_quantities}
#Create the chart - Column 2D Chart with data from strXML
render_chart_html '/FusionCharts/Column2D.swf', '', str_xml, 'FactoryDetailed', 600, 300, false do -%>
<% end-%>
As part of ajax response the complete object tag to render the chart is obtained. Let us focus on this javascript function now. function updateChart(factoryIndex){ var strURL = "factory_chart?factory_id=" + factoryIndex; $.ajax({ type: "GET", url: strURL, success: function(msg){ $("#detailedChart").show().html(msg); } }); } In this function, the following steps are performed:
Note that the Ajax functionality is part of the FusionCharts JS files. Our job is done. The chart will now get refreshed with details of the selected factory. In this example, we have seen how to use AJAX to update a chart when clicked on a slice in the pie chart. This is just the beginning of what can be achieved with Ruby on Rails, AJAX and FusionCharts. Keep exploring! |