Current File : /home/caballoscriollos/public_html/espanol/encuesta/graphs/contents/ruby_form.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>FusionCharts Documentation</title>
<link rel="stylesheet" href="Style.css" type="text/css" />
</head>
<body>
<table width="98%" border="0" cellspacing="0" cellpadding="3" align="center">
<tr>
<td><h2 class="pageHeader">Using FusionCharts with RoR- Charting Data from Forms </h2></td>
</tr>
<tr>
<td valign="top" class="text"><p>In this section, we'll show you how to use FusionCharts with Ruby to plot data collected from form. </p>
<p>We'll build a simple restaurant sales example, where the user will enter the items sold by a restaurant in a given week. This data will be submitted in a form to the server. We'll acquire this data and plot it on a chart. For the sake of simplicity, we wouldn't do any processing on this data. However, your real life applications might process data before presenting it on the chart. </p>
<p><strong>Before you go further with this page, we recommend you to please see the previous section "Basic Examples" as we start off from concepts explained in that page. </strong></p>
<p class="highlightBlock">
All code discussed here is present in <br>
<span class="codeInline">Controller : Download Package > Code > RoR > SampleApp > app > controllers > fusioncharts > form_based_controller.rb</span>. <br>
<span class="codeInline">Rhtml : Download Package > Code > RoR > SampleApp > app > views > fusioncharts > form_based</span> folder.
</p>
<p class="header">Building the Form</p> <p class="text">The form is contained in default.html.erb and looks as under: </p> <p class="text"><img src="Images/Code_Form.gif" class="imageBorder" /></p>
<p class="text">To display this form, we have defined an action default in the controller <span class="codeInline">form_based_controller.rb. </span>There is no code in this action. It just renders the view. (the form) </p> <p class="text">Let us see the view.</p> <p class="codeBlock"><HTML><br>
<HEAD><br>
<TITLE>FusionCharts - Form Based Data Charting Example</TITLE><br>
<style type="text/css"><br>
<!--<br>
body {<br>
font-family: Arial, Helvetica, sans-serif;<br>
font-size: 12px;<br>
}<br>
.text{<br>
font-family: Arial, Helvetica, sans-serif;<br>
font-size: 12px;<br>
}<br>
--><br>
</style><br>
</HEAD><br>
<BODY><br>
<CENTER><br>
<h2>FusionCharts Form-Based Data Example</h2><br>
<p class='text'>Please enter how many items of each category you<br>
sold within this week. We'll plot this data on a Pie chart.</p><br>
<p class='text'>To keep things simple, we're not validating for<br>
non-numeric data here. So, please enter valid numeric values only. In<br>
your real-world applications, you can put your own validators.</p><br>
<strong> <% form_tag(:action=>'chart') do -%></strong><br>
<table width='50%' align='center' cellpadding='2' cellspacing='1'<br>
border='0' class='text'><br>
<tr><br>
<td width='50%' align='right'><B>Soups:</B> &nbsp;</td><br>
<td width='50%'><input type='text' size='5' name='Soups'<br>
value='108'> bowls</td><br>
</tr><br>
<tr><br>
<td width='50%' align='right'><B>Salads:</B> &nbsp;</td><br>
<td width='50%'><input type='text' size='5' name='Salads'<br>
value='162'> plates</td><br>
</tr><br>
<tr><br>
<td width='50%' align='right'><B>Sandwiches:</B> &nbsp;</td><br>
<td width='50%'><input type='text' size='5' name='Sandwiches'<br>
value='360'> pieces</td><br>
</tr><br>
<tr><br>
<td width='50%' align='right'><B>Beverages:</B> &nbsp;</td><br>
<td width='50%'><input type='text' size='5' name='Beverages'<br>
value='171'> cans</td><br>
</tr><br>
<tr><br>
<td width='50%' align='right'><B>Desserts:</B> &nbsp;</td><br>
<td width='50%'><input type='text' size='5' name='Desserts'<br>
value='99'> plates</td><br>
</tr><br>
<tr><br>
<td width='50%' align='right'>&nbsp;</td><br>
<td width='50%'><input type='submit' value='Chart it!'></td><br>
</tr><br>
<table><br>
<strong><% end -%> </strong><br>
</CENTER><br>
</BODY><br>
</HTML><br>
</p> <p class="text">Here, we have used <span class="codeInline">form_tag</span> ruby <span class="codeInline">form-helper</span> function to create the form and we have assigned the action <span class="codeInline">"chart"</span> to it. In this example, we have the form text fields hard-coded. In real-world, you would create the form based on some Model and use ruby form-helpers to create the form, the textfields etc. What happens when you click on <span class="codeInline">Chart It</span> button? </p> <p class="text"><span class="header">Requesting the data and Creating the Chart </span></p> <p class="text">The work of requesting the data from submitted form and creating the chart containing the following code: </p> <p class="codeBlock"> <b>Controller: Fusioncharts::FormBasedController<br>
Action: chart<br>
</b><span class="codeComment">#Data is obtained from the submitted form (present in the request)<br>
#In this example, we're directly showing this data back on chart.<br>
#In your apps, you can do the required processing and then show the <br>
#relevant data only.<br>
#The view for this action uses the "common" layout.</span><br>
def chart<br>
headers["content-type"]="text/html";<br>
<span class="codeComment"> # Get the values from the request using params[]</span><br>
@str_soups = params[:Soups]<br>
@str_salads = params[:Salads]<br>
@str_sandwiches = params[:Sandwiches]<br>
@str_beverages = params[:Beverages]<br>
@str_desserts = params[:Desserts]<br>
<br>
<span class="codeComment"> #The common layout is used only by this function in controller.</span><br>
render(:layout => "layouts/common")<br>
end <br>
<br>
<b>View:</b><br>
<% @page_title=" FusionCharts - Form Based Data Charting Example " %><br>
<% @page_heading="FusionCharts Form-Based Data Example" %><br>
<% @page_subheading="Restaurant Sales Chart below" %><br>
<p class='text'>Click on any pie slice to see slicing effect. Or,<br>
right click on chart and choose "Enable Rotation", and then drag and<br>
rotate the chart.</p><br>
<%<br>
<span class="codeComment"># The xml is obtained as a string from builder template.</span><br>
str_xml =render "fusioncharts/form_based/form_based_data",{:str_soups => @str_soups,:str_salads => @str_salads,:str_sandwiches => @str_sandwiches,:str_beverages => @str_beverages,:str_desserts => @str_desserts}<br>
<br>
<span class="codeComment">#Create the chart - Pie 3D Chart with data from str_xml</span><br>
render_chart '/FusionCharts/Pie3D.swf','',str_xml,'Sales', 500, 300, false, false do-%><br>
<% end -%><br>
<a href='javascript:history.go(-1);'>Enter data again</a> </p> <p class="text">The controller action does the following:</p> <ol class="text">
<li> Gets the data submitted by the form from the request using params[...]</li>
<li>Stores them in global variables accessible to the view </li>
</ol> <p class="text" >The view calls render function and gets the xml from the builder template <span class="codeInline">form_based_data</span> by passing the form data as parameters to it. Then it calls the render_chart function to create a Pie chart, by passing the xml as parameter. </p>
<p class="text" >What does the builder do? Let's see.</p> <p class="codeBlock" ><span class="codeComment"># Builds xml for sales of various product categories <br>
# of a Restaurant to be shown in form of a pie-chart.<br>
# The values required here are got as parameters<br>
# Expected parameters: str_soups,str_salads,str_sandwiches,str_beverages,str_desserts</span><br>
xml = Builder::XmlMarkup.new<br>
xml.graph(:caption=>'Sales by Product Category', :subCaption=>'For this week', :showPercentageInLabel=>'1',:pieSliceDepth=>'25',:decimalPrecision=>'0',:showNames=>'1') do<br>
xml.set(:label=>'Soups',:value=>str_soups) <br>
xml.set(:label=>'Salads',:value=>str_salads) <br>
xml.set(:label=>'Sandwiches',:value=>str_sandwiches)<br>
xml.set(:label=>'Beverages',:value=>str_beverages)<br>
xml.set(:label=>'Desserts',:value=>str_desserts)<br>
end</p>
<p class="text" >The builder builds the xml with outermost tag as <graph> element. Inside this, it puts the <set> element. The attributes of the set tag are label and value. The values are picked from the parameters received from the view. That's it. Now, when you click on "Chart it" button, this is what you will see:</p>
<p class="text" ><img src="Images/Code_FormChart.jpg" width="468" height="268" class="imageBorder" /></p></td>
</tr>
</table>
</body>
</html>
Mr. DellatioNx196 GaLers xh3LL Backd00r 1.0, Coded By Mr. DellatioNx196 - Bogor BlackHat