Class Fusioncharts::DbDataUrlController
In: app/controllers/fusioncharts/db_data_url_controller.rb
Parent: ApplicationController

This controller class shows how to render a pie-chart by retrieving factory name and total output quantity dynamically at run-time, from the database. setDataURL method is used here. As per Ruby On Rails conventions, we have the corresponding views with the same name as the function name in the controller.

Methods

default   pie_data  

Public Instance methods

Set DataURL with animation property to 1. NOTE: It‘s necessary to encode the dataURL if you‘ve added parameters to it. In this example, we show how to connect FusionCharts to a database using dataURL method. In our other examples, we‘ve used dataXML method where the XML is generated in the same page as chart. Here, the XML data for the chart would be generated in pie_data function. To illustrate how to pass additional data as querystring to dataURL, we‘ve added an animate property, which will be passed to pie_data action. pie_data action would handle this animate property and then generate the XML accordingly.

[Source]

    # File app/controllers/fusioncharts/db_data_url_controller.rb, line 18
18:   def default
19:     # Escape each parameter and its value before appending it to the URL
20:     # This is done, so that any special characters like ? etc in the parameter name or value is escaped
21:     # Here, the parameter name is "animate" and value is "1", which do not need escaping as such.
22:     @str_data_url = "/Fusioncharts/db_data_url/pie_data?"+CGI.escape("animater")+"="+CGI.escape("1");
23:     
24:     #The common layout for this view
25:     render(:layout => "layouts/common")
26:   end

Generates the xml with each factory‘s name and total output quantity. Content-type for its view is text/xml

[Source]

    # File app/controllers/fusioncharts/db_data_url_controller.rb, line 30
30:   def pie_data
31:     
32:       headers["content-type"]="text/xml";
33:       @animate_chart = params[:animate]
34:             
35:       if @animate_chart==nil or @animate_chart.empty?
36:          @animate_chart = '1'
37:       end  
38:       
39:       @factory_data = []
40:       
41:       # Find all the factories
42:       factory_masters = Fusioncharts::FactoryMaster.find(:all)
43:      
44:       # For each factory, find the factory output details.
45:         factory_masters.each do |factory_master|
46:           factory_name = factory_master.name  
47:           total = 0.0
48:           factory_master.factory_output_quantities.each do |factory_output|
49:             # Total the output quantity for a particular factory
50:             total = total + factory_output.quantity
51:           end
52:           # Append the array of factory name and total output quantity to the existing array @factory_data
53:           @factory_data<<[factory_name,total]
54:       end
55:   end

[Validate]