Module ApplicationHelper
In: app/helpers/application_helper.rb

Methods added to this helper will be available to all templates in the application. These methods can be used to render the chart using FusionCharts

Methods

Public Instance methods

 The xml got from builder contains new line characters which need to be removed.

The builder also encloses attribute values in double quotes. We will replce them with single quotes.

[Source]

     # File app/helpers/application_helper.rb, line 115
115:     def escape_builder_xml(xml_to_escape)
116:     # Hash containing all the required conversions
117:       conversions = {
118:       %r{\r\n}=>'',
119:       %r{\n}=>'',
120:       %r{"}=>'\''
121:      }  
122:       escaped_xml = xml_to_escape
123:       conversions.each do |x,y|
124:           escaped_xml = escaped_xml.gsub(x,y)
125:         end  
126:         return escaped_xml
127:     end

This function creates the javascript code that renders a chart from the swf file passed as parameter either making use of setDataURL method or setDataXML method.

  • parameter chart_swf : Path to the swf file (includin the filename) that renders the chart.
  • parameter str_url : If you intend to use dataURL method for the chart, pass the URL to the XML as this parameter. Else, set it to ""
  • parameter str_xml : If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to ""
  • parameter chart_id : string for identifying chart. Unique Id for the chart, using which it will be recognized in the HTML page.
  • parameter chart_width : integer, Intended width for the chart (in pixels)
  • parameter chart_height : integer, Intended height for the chart (in pixels)
  • parameter debug_mode : boolean (true/false), Whether to start the chart in debug mode.
  • parameter register_with_js : pass value as true ( a boolean ) for view file to be registered to include javascript statements.

[Source]

    # File app/helpers/application_helper.rb, line 15
15:      def render_chart(chart_swf,str_url,str_xml,chart_id,chart_width,chart_height,debug_mode,register_with_js,&block)
16:        
17:        chart_width=chart_width.to_s
18:        chart_height=chart_height.to_s
19:        concat("\t\t<!-- START Script Block for Chart-->\n\t\t",block.binding) 
20:        concat(content_tag("div","\n\t\t\t\tChart.\n\t\t",{:id=>chart_id+"Div",:align=>"center"}),block.binding)
21:        concat("\n\t\t<script type='text/javascript'>\n",block.binding)
22:       
23:        debug_mode_num="0";
24:        register_with_js_num="0";
25:       
26:        if debug_mode==true
27:          debug_mode_num="1"
28:        end
29:        
30:        if register_with_js==true
31:          register_with_js_num="1"
32:        end
33:   
34:        concat("\t\t\t\tvar chart_"+chart_id+"=new FusionCharts('"+chart_swf+"','"+chart_id+"',"+chart_width+","+chart_height+","+debug_mode_num+","+register_with_js_num+");\n",block.binding)
35:                      
36:        if str_xml==""
37:          concat("\t\t\t\t<!-- Set the dataURL of the chart -->\n",block.binding)
38:          concat("\t\t\t\tchart_"+chart_id+".setDataURL(\""+str_url+"\");\n",block.binding)
39:          logger.info("The method used is setDataURL.The URL is " + str_url)
40:        else
41:          concat("\t\t\t\t<!-- Provide entire XML data using DataXML method -->\n",block.binding)
42:          concat("\t\t\t\tchart_"+chart_id+".setDataXML(\""+str_xml+"\");\n",block.binding)
43:          logger.info("The method used is setDataXML.The XML is " + str_xml)
44:        end
45:          
46:        concat("\t\t\t\t<!-- Finally render the chart. -->\n",block.binding)
47:        concat("\t\t\t\tchart_"+chart_id+".render('"+chart_id+"Div');\n",block.binding)
48:        concat("\t\t</script>\n",block.binding)
49:        concat("\t\t<!-- END Script Block for Chart. -->\n",block.binding)
50:    
51:      end

This function creates the javascript code that renders a chart from the swf file passed as parameter either making use of setDataURL method or setDataXML method. T

  • parameter chart_swf : Path to the swf file (includin the filename) that renders the chart.
  • parameter str_url : If you intend to use dataURL method for the chart, pass the URL to the XML as this parameter. Else, set it to ""
  • parameter str_xml : If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to ""
  • parameter chart_id : string for identifying chart. Unique Id for the chart, using which it will be recognized in the HTML page.
  • parameter chart_width : integer, Intended width of the chart (in pixels)
  • parameter chart_height : integer, Intended height of the chart (in pixels)
  • parameter debug_mode : boolean (true/false), Whether to start the chart in debug mode.

[Source]

     # File app/helpers/application_helper.rb, line 62
 62:      def render_chart_html(chart_swf,str_url,str_xml,chart_id,chart_width,chart_height,debug_mode,&block)
 63:        chart_width=chart_width.to_s
 64:        chart_height=chart_height.to_s
 65:        
 66:        debug_mode_num="0"
 67:        if debug_mode==true
 68:          debug_mode_num="1"
 69:        end 
 70:        str_flash_vars=""
 71:        if str_xml==""
 72:           str_flash_vars="chartWidth="+chart_width+"&chartHeight="+chart_height+"&debugmode="+debug_mode_num+"&dataURL="+str_url
 73:           logger.info("The method used is setDataURL.The URL is " + str_url)
 74:        else
 75:           str_flash_vars="chartWidth="+chart_width+"&chartHeight="+chart_height+"&debugmode="+debug_mode_num+"&dataXML="+str_xml
 76:           logger.info("The method used is setDataXML.The XML is " + str_xml)
 77:        end
 78:         concat("\t\t<!-- START Code Block for Chart -->\n\t\t",block.binding)
 79:         
 80:         object_attributes={:classid=>"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"}
 81:         object_attributes=object_attributes.merge(:codebase=>"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0")
 82:         object_attributes=object_attributes.merge(:width=>chart_width)
 83:         object_attributes=object_attributes.merge(:height=>chart_height)
 84:         object_attributes=object_attributes.merge(:id=>chart_id)
 85:        
 86:         param_attributes1={:name=>"allowscriptaccess",:value=>"always"}
 87:         param_tag1=content_tag("param","",param_attributes1)
 88:         
 89:         param_attributes2={:name=>"movie",:value=>chart_swf}
 90:         param_tag2=content_tag("param","",param_attributes2)
 91:         
 92:         param_attributes3={:name=>"FlashVars",:value=>str_flash_vars}
 93:         param_tag3=content_tag("param","",param_attributes3)
 94:         
 95:         param_attributes4={:name=>"quality",:value=>"high"}
 96:         param_tag4=content_tag("param","",param_attributes4)
 97:         
 98:         embed_attributes={:src=>chart_swf}
 99:         embed_attributes=embed_attributes.merge(:FlashVars=>str_flash_vars)
100:         embed_attributes=embed_attributes.merge(:quality=>"high")
101:         embed_attributes=embed_attributes.merge(:width=>chart_width)
102:         embed_attributes=embed_attributes.merge(:height=>chart_height).merge(:name=>chart_id)
103:         embed_attributes=embed_attributes.merge(:allowScriptAccess=>"always")
104:         embed_attributes=embed_attributes.merge(:type=>"application/x-shockwave-flash")
105:         embed_attributes=embed_attributes.merge(:pluginspage=>"http://www.macromedia.com/go/getflashplayer")
106:         
107:         embed_tag=content_tag("embed","",embed_attributes)
108:         
109:         concat(content_tag("object","\n\t\t\t\t"+param_tag1+"\n\t\t\t\t"+param_tag2+"\n\t\t\t\t"+param_tag3+"\n\t\t\t\t"+param_tag4+"\n\t\t\t\t"+embed_tag+"\n\t\t",object_attributes),block.binding)
110:         concat("\n\t\t<!-- END Code Block for Chart -->\n",block.binding)
111:         
112:       end

[Validate]