Posts Tagged ‘ziya’

28th November
2008
written by simplelight
  1. Upgrade to latest version of RubyGems (‘sudo gem update –system’)
  2. Add GitHub repository (‘gem sources -a http://gems.github.com’)
  3. Upgrade to latest version of Ziya (‘sudo gem install derailed-ziya’)
  4. Install Ziya in project directory (‘Ziyafy –charts’ in project home, note the double dash)
  5. Add ziya.rb to config/initializers directory
  6. 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.new

04:    # Pull portfolios out of database
05:   @query = sessions[:period].to_i

06:    @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.