Class Fusioncharts::DbJsController
In: app/controllers/fusioncharts/db_js_controller.rb
Parent: ApplicationController

In this controller, we will plot a pie-chart showing total output of quantities and name of each factory in a pie-section. On clicking on a pie-section, we obtain detailed information on the quantity produced and date of production of the factory by using javascript.

Methods

default  

Public Instance methods

In this action, the total quantity produced and name of each factory is obtained from the database and plotted. On clicking on a pie-section, it links to another chart giving detailed information of each output produced and the date of production. For this dataURL method is used from the js. of that factory. An array is created to store the index, factory name and total output. This array is accessible to the view. Also, a js_var_string variable is constructed. This variable contains javascript code to create an array, to contain the date of production and the factory output quantity.

[Source]

    # File app/controllers/fusioncharts/db_js_controller.rb, line 15
15:   def default
16:     headers["content-type"]="text/html";
17:     @factory_data = [] 
18:     @js_var_string =""
19:     index_count = -1
20:     #Get data from factory masters table
21:     factory_masters = Fusioncharts::FactoryMaster.find(:all)
22:       factory_masters.each do |factory_master| 
23:         total=0.0
24:         index_count = index_count + 1
25:         factory_id = factory_master.id
26:         factory_name = factory_master.name
27:         # Construct the javascript variable to hold an array.
28:         @js_var_string =@js_var_string+ "data[" + index_count.to_s + "] = new Array();\n" ; 
29: 
30:         factory_master.factory_output_quantities.each do |factory_output|
31:                       date_of_production = factory_output.date_pro
32:                       # Formats the date to dd/mm without leading zeroes
33:                       formatted_date = format_date_remove_zeroes(date_of_production)
34:                       quantity_number = factory_output.quantity
35:                       # Calculate the total quantity for this factory
36:                       total = total + factory_output.quantity
37:                       # Append values to the javascript array
38:                       @js_var_string =@js_var_string+ "\t\t\t\tdata[" + index_count.to_s + "].push(new Array('" + formatted_date + "','" +quantity_number.to_s+"'));\n" 
39:                     end
40:         #Formatting the output html
41:         @js_var_string =@js_var_string+"\t\t\t";
42:         #Push hash of values into the array          
43:         @factory_data<<{:factory_index=>index_count,:factory_name=>factory_name,:factory_output=>total}
44:       end
45:   end

[Validate]