| Using FusionCharts with RoR - Creating Drill-down charts |
In our previous example, we had used FusionCharts to plot a chart using data stored in database. We'll now extend that example itself to create a drill-down chart which can show more information. If you recall from previous example, we were showing the sum of factory output in a pie chart as below:
In this example, we'll extend the previous example, so that when a user clicks on a pie slice for a factory, he can drill down to see date wise production for that factory. Setting up the pie chart for Link To set up the pie chart to enable links for drill-down involves just minor tweaking of our previous example. We basically need to add the link attribute for each <set> element. Controller: Fusioncharts::DbExampleController This action expects a parameter called "animate" from the request. This is assigned to the variable @animate_chart. The controller finds the data and stores it in @factories. The view is similar to the basic_dbexample.html.erb seen in the previous example, except here it passes the parameter animate_chart also in the locals hash. Here the builder template used is default_factories_quantity.builder and we pass the factories' data and animate_chart parameters to the builder. The builder template used is as follows: #Creates xml with values for Factory Output Iterate through each factory and use the name and total quantity values. We add an attribute called link to the <set> tag, with value as str_data_url. This link str_data_url is the path to the detailed action with id of the factory. On clicking a pie slice, what happens? It goes to the detailed action of the Controller. Let us now generate the chart that will be shown on clicking a pie slice. Creating the detailed data chart page Controller: Fusioncharts::DbExampleController The detailed action does the following:
The view detailed.html.erb calls the render function with the path to the builder factory_details, factory_output and factory id as parameter. The resultant xml is assigned to the variable str_xml. Finally, it calls the render_chart function to chart a Column2D chart and passes the xml to it as dataXML parameter. What does the builder template factory_details do? Here is the code: #Creates xml with values for date of production and quantity for a particular factory This is a simple xml with the outer <chart> element and <set> elements within it. The <set> element has label and value attributes. Date of production is set as the label and quantity is set as the value. These values are obtained from the array of hashes factory_data received as parameter. Following is the code for formatting date to dd/mm and removing the leading zeroes. application_helper.rb Now, when you click on a pie slice, the page opens the following chart with details of the selected factory:
|