Posts Tagged ‘graphs’
- Upgrade to latest version of RubyGems (‘sudo gem update –system’)
- Add GitHub repository (‘gem sources -a http://gems.github.com’)
- Upgrade to latest version of Ziya (‘sudo gem install derailed-ziya’)
- Install Ziya in project directory (‘Ziyafy –charts’ in project home, note the double dash)
- Add ziya.rb to config/initializers directory
- Copy your themes into ../public/themes/
Some other thoughts: If you’re frustrated by the almost non-existent documentation and the fact that the gem is in constant flux, don’t despair. The best way to understand how to customize your graph is to look through the example themes (for some reason they didn’t install with my gem but I downloaded them from GitHub).
Note: You can not instantiate the Ziya object in the controller corresponding to the chart’s view. It needs to be in a separate controller. Otherwise, an XML file (instead of a chart) will be returned when that controller is invoked.
Also, the reference material at XML/SWF charts is very useful and Ziya seems to adhere quite closely to the naming conventions.
I also found that I needed user-defined functions fairly quickly to customize axes etc.
Here is an example of how to use Ziya to create a scatter chart:
== Chart Controller ==
01: def load_ef
02: Â Â # Create graph data object
03:   chart_data  = Array.new04:   # Pull portfolios out of database
05: Â @query = sessions[:period].to_i06: Â Â @portfolios = Portfolio.find(:all, :conditions => [“period = ?”, @query])
07: Â Â # Strip out risk and return
08: Â Â @portfolios.each { |x|
09:    chart_data  << x.std_dev
10:    chart_data  << x.port_ret
11: Â Â }12: Â Â title = “User-entered portfolios”
13: Â Â chart = Ziya::Charts::Scatter.new(‘LICENSE-KEY’)
14: Â Â chart.add( :axis_category_text, %w[x y]*(chart_data.length/2) )
15: Â Â chart.add( :series, title, chart_data )16: Â Â chart.add( :theme , “assetcorrelation” )
17: Â Â respond_to do |fmt|
18: Â Â Â fmt.xml { render : xml => chart.to_xml }
19: Â Â end
20: end== View ==
1: <div>
2: Â <%= ziya_chart load_ef_url, :size => “1200×800” – %>
3: </div>== Routes.rb ==
1: map.load_ef  ‘/chart/load_ef’,  :controller => ‘chart’, :action => ‘load_ef’
As far as I understand, Rails will first look for a method in your controller that matches the view, then, once it starts rendering the view, it hits the callback (line 2 in the view snippet above) and at that point calls the method in the chart_controller to render the chart data, using the routing information in Routes.rb.
Note that in this case, I am pulling the data for the chart out of my database.