Class Fusioncharts::DbExampleController
In: app/controllers/fusioncharts/db_example_controller.rb
Parent: ApplicationController

There are two examples in this controller. *Pie-chart for total ouput quantities of each factory by getting data from database and using dataXML method *Pie-chart for total ouput quantities of each factory and a link to another chart which gives detailed information for selected factory All the views related to this controller will use the "common" layout. As per Ruby On Rails conventions, we have the corresponding views with the same name as the function name in the controller.

Methods

Public Instance methods

This action retrieves the values from the database and constructs an array to hold, factory name and corresponding total output quantity. The view for this action basic_dbexample will use the array values to construct the xml for this chart. To build the xml, the view takes help from the builder file (basic_factories_quantity.builder)

[Source]

    # File app/controllers/fusioncharts/db_example_controller.rb, line 16
16:   def basic_dbexample
17:       headers["content-type"]="text/html";
18:       @factory_data = [] 
19:       #Get data from factory masters table
20:       
21:       factory_masters = Fusioncharts::FactoryMaster.find(:all)
22:       factory_masters.each do |factory_master| 
23:           total = 0.0
24:           factory_id = factory_master.id
25:           factory_name = factory_master.name
26:           factory_master.factory_output_quantities.each do |factory_output|
27:                   total = total + factory_output.quantity
28:           end
29:           # Push the hash of values into the array             
30:           @factory_data<<{:factory_name=>factory_name,:factory_output=>total}
31:       end    
32:   end

In this function, we obtain total output of quantities and name of each factory from the database and plot them on a pie-chart. It stores URL to the "detailed" function in a variable passing FactoryId as parameter to the function which returns quantity produced and date of production of the factory that are obtained from database and which are plotted in a chart. This action retrieves the values from the database and constructs an array to hold, factory name, corresponding total output quantity and URL to the action which will generate the detailed chart. The view for this action default.html.erb will use the array values to construct the xml for this chart. To build the xml, the view takes help from the builder file (default_factories_quantity.builder)

[Source]

    # File app/controllers/fusioncharts/db_example_controller.rb, line 41
41:   def default
42:      headers["content-type"]="text/html";
43:     str_data_url = '';
44:     @animate_chart = params[:animate]
45:     if @animate_chart==nil or @animate_chart.empty?
46:       @animate_chart = '1'
47:     end
48:     #Get data from factory masters table
49:     
50:     factory_masters = Fusioncharts::FactoryMaster.find(:all)
51:     
52:     @factory_data = []
53:     #Loop through each record
54:       factory_masters.each do |factory_master| 
55:           total = 0.0
56:           #Get factoryid and factoryname 
57:           factory_id = factory_master.id
58:           factory_name = factory_master.name
59:           factory_master.factory_output_quantities.each do |factory_output|
60:                   total = total + factory_output.quantity
61:           end
62:           # Escape the URL 
63:           str_data_url = "/Fusioncharts/db_example/detailed?"+CGI.escape("FactoryId")+"="+CGI.escape(factory_id.to_s)
64:           # Put the hash of values in the array
65:           @factory_data<<{:str_data_url=>str_data_url, :factory_name=>factory_name, :factory_output=>total}
66:       end
67:   end

This action retrieves the quantity and date of production of the factory identified by the request parameter expected "FactoryId" The view for this action is detailed.html.erb and it uses the builder file factory_details.builder to build the xml for the column chart.

[Source]

    # File app/controllers/fusioncharts/db_example_controller.rb, line 73
73:   def detailed
74:       headers["content-type"]="text/html";
75:       @factory_id = params[:FactoryId]
76:       @factory_data = []
77:       
78:       factory_master = Fusioncharts::FactoryMaster. find(@factory_id)
79:       factory_master.factory_output_quantities.each do |factory_output|
80:                       date_of_production = factory_output.date_pro
81:                       # Formats the date to dd/mm
82:                       #formatted_date = date_of_production.strftime('%d/%m')
83:                       # Formats the date to dd/mm without leading zeroes
84:                       formatted_date = format_date_remove_zeroes(date_of_production)
85:                       quantity_number = factory_output.quantity
86:                       @factory_data<<{:date_of_production=>formatted_date,:quantity_number=>quantity_number}
87:       end
88:   end

[Validate]