Class Fusioncharts::DbJsDataurlController
In: app/controllers/fusioncharts/db_js_dataurl_controller.rb
Parent: ApplicationController

This controller shows how to generate a pie-chart by retrieving values of the total output quantities produced in factories with their names On clicking on a pie-section, the detailed date-wise manufacture of each quantity produced by that factory is displayed as a chart on the same page. In this example, we show a combination of database + JavaScript (dataURL method) rendering using FusionCharts. The entire example can be summarized as under. This example shows the break-down of factory wise output generated. In a pie chart, we first show the sum of quantity generated by each factory. These pie slices, when clicked would show detailed date-wise output of that factory. The detailed data would be dynamically pulled by the column chart from another page. There are no page refreshes required. Everything is done on one single page. The XML data for the pie chart is fully created at run-time. Controller interacts with the database and creates the XML for this using Builder template. Now, for the column chart (date-wise output report), each time we need the data we dynamically submit request to the server with the appropriate factoryId. The server responds with an XML document, which we accept and update chart at client side.

Methods

Public Instance methods

Default action. The total output quantity for each factory is calculated using values from the database. This data of factory id, name and total output is stored in an array for each factory. This array of arrays is visible to its view.

[Source]

    # File app/controllers/fusioncharts/db_js_dataurl_controller.rb, line 23
23:     def default
24:     
25:       headers["content-type"]="text/html";
26:       # Expects a parameter called animate
27:       @animate_chart = params[:animate]
28:       if @animate_chart==nil or @animate_chart.empty?
29:          # Assigns default values as '1'
30:          @animate_chart = '1'
31:       end  
32:       #Initialize the array
33:       @factory_data = []
34:       # Get the data in all the columns of FactoryMaster
35:       factory_masters = Fusioncharts::FactoryMaster. find(:all)
36:       # Loop through each factory obtained
37:       factory_masters.each do |factory_master|
38:           factory_id = factory_master.id
39:           factory_name = factory_master.name  
40:           total = 0.0
41:           factory_master.factory_output_quantities.each do |factory_output|
42:             # Calculate the total for this factory
43:             total = total + factory_output.quantity
44:           end
45:           @factory_data<<{:factory_id=>factory_id,:factory_name=>factory_name,:factory_output=>total}
46:       end  
47:     end

This action will generate a chart to show the detailed information of the selected factory. Expects factoryId as parameter in the request

[Source]

    # File app/controllers/fusioncharts/db_js_dataurl_controller.rb, line 51
51:     def factory_details
52:         headers["content-type"]="text/xml";
53:         # Get the factoryId from request using params[]
54:         @factory_id = params[:factoryId]
55:         #Initialize the array
56:         @factory_data = []
57:         
58:         factory_master = Fusioncharts::FactoryMaster. find(@factory_id)
59:                 factory_master.factory_output_quantities.each do |factory_output|
60:                       date_of_production = factory_output.date_pro
61:                       # Formats the date to dd/mm without leading zeroes
62:                       formatted_date = format_date_remove_zeroes(date_of_production)
63:                       quantity_number = factory_output.quantity
64:                       @factory_data<<{:date_of_production=>formatted_date,:quantity_number=>quantity_number}
65:                 end
66:     end

[Validate]