Home arrow Ruby-on-Rails arrow Page 3 - GUIs and More for Desktop Reports
RUBY-ON-RAILS

GUIs and More for Desktop Reports


In this second part to a two-part series on creating reports on the desktop with Ruby-on-Rails, you'll learn how to create GUIs with Ruby and take a look at a sample report. It is excerpted from chapter four of the book Practical Reporting with Ruby and Rails, written by David Berube (Apress; ISBN: 1590599330).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
March 24, 2010
TABLE OF CONTENTS:
  1. · GUIs and More for Desktop Reports
  2. · Using FXRuby
  3. · Graphing Team Performance on the Desktop
  4. · Dissecting the Code for the Program

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
GUIs and More for Desktop Reports - Graphing Team Performance on the Desktop
(Page 3 of 4 )

Let's say that Transmegtech Studios is reworking the artificial intelligence in its new strategy game using the graphs created in Chapter 3. However, the graphs were created as PNG files, and the chief executive officer (CEO) of the company does not know how to view PNGs. He wants to be able to click an icon on his desktop and launch an application that lets him view charts by clicking a player in a list. Fortunately, this is reasonably easy to do with FXRuby.

Before you can run this example, you'll need the database from Chapter 3 (shown partially in Listing 3-3). You can download the SQL from http://rubyreporting.com/examples/player_4.sql or from the Source/Downloads area of the Apress web site (http://.www.apress.com), and then import the data using the command
mysql -u my_mysql_user -p < player_4.sql.

The code to create a simple desktop application for viewing graphs is shown in Listing 4-3.

Listing 4-3. Desktop Team Performance Grapher (desktop_team_performance_graph.rb)

require 'fox16'
require 'active_record'
require 'optparse'
require 'rubygems'
require 'gruff'
require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter  => 'mysql',
  :host     => 'localhost',
  :username => 'insert_your_mysql_username_here',
  :password => 'insert_your_mysql_password_here',
  :database => 'players_4')

class Player < ActiveRecord::Base
  has_many :plays
end
class Game < ActiveRecord::Base

  has_many :plays
end
class Play < ActiveRecord::Base
  belongs_to :game
 
belongs_to :player
end
class Event < ActiveRecord::Base

  belongs_to :play
end

include Fox

class TransmegtechGraphWindow
    def initialize

        @main_window=FXMainWindow.new(get_app,
                            "Transmegtech Studios Player Reporting Software",
                            nil, nil, DECOR_ALL )
       
@main_window.width=640; @main_window.height=480

        control_matrix=FXMatrix.new(@main_window,4, MATRIX_BY_COLUMNS)

        FXLabel.new(control_matrix, 'Game:')
        @game_combobox = FXComboBox.new(control_matrix, 30, nil,
                    COMBOBOX_STATIC | FRAME_SUNKEN )
        @game_combobox.numVisible = 5
        @game_combobox.editable = false

        Game.find(:all).each do |game|
          @game_combobox.appendItem(game.name , game.id)
        end
        @game_combobox.connect(SEL_COMMAND) do
          update_display
        end

        FXLabel.new(control_matrix, 'Player:')

        @player_combobox = FXComboBox.new(control_matrix, 35, nil,
                           COMBOBOX_STATIC | FRAME_SUNKEN )
        @player_combobox.numVisible = 5
        @player_combobox.editable = false

        Player.find(:all).each do |player|
          @player_combobox.appendItem(player.name , player.id)
        end

        @player_combobox.connect(SEL_COMMAND) do
          update_display
        end

        @graph_picture_viewer = FXImageView.new(@main_window , nil, nil, 0,
              LAYOUT_FILL_X | LAYOUT_FILL_Y)

        @graph_picture_viewer.connect( SEL_CONFIGURE ) do
          update_display
        end

        @main_window.show( PLACEMENT_SCREEN )
    end
    def update_display
      game_id_to_analyze = @game_combobox.getItemData(@game_combobox.currentItem)
      player = Player.find(@player_combobox.getItemData(
              @player_combobox.currentItem))
      bar_chart = Gruff::Bar.new("#{@graph_picture_viewer.width}x" << 
          
#{@graph_picture_viewer.height})
      bar_chart.legend_font_size = 12
      total_games = Play.count(:conditions=>['game_id = ? AND ' << 
                        
'player_id = ?', 
                         game_id_to_analyze, player.id]
             ).to_f || 0
      total_wins = Play.count(:conditions=>['game_id = ? AND ' <<
                         'player_id = ? AND won=1',
                         game_id_to_analyze, player.id]
             ).to_f || 0

      bar_chart.title = "#{player.name} (#{'%i' %
                   (total_games==0 ? '0' :
                     (total_wins/total_games * 100))
                   }% won)"
      bar_chart.minimum_value = 0
      bar_chart.maximum_value = 110

      sql = "SELECT event, AVG(time) as average_time

           FROM events AS e
              INNER JOIN
              plays AS p
               
ON e.play_id=p.id 
          
WHERE p.game_id='#{game_id_to_analyze}'
              AND
              p.player_id='#{player.id}'
          GROUP BY e.event DESC;"

        data = []
        Event.find_by_sql(sql).each do |row|
          bar_chart.data row.event, (row.average_time.to_i/1000)
        end
        bar_chart.labels = {0=>'Time'}
        chart_png_filename = "./player_#{player.id}.png"
        bar_chart.write(chart_png_filename)

        pic = FXPNGImage.new(FXApp.instance())

        FXFileStream.open(chart_png_filename,
                    FXStreamLoad) { |stream| pic.loadPixels(stream) }

        pic.create
        @graph_picture_viewer.image = pic
        File.unlink(chart_png_filename)

      end

end

fox_application=FXApp.new

TransmegtechGraphWindow.new

FXApp.instance().create # Note that getApp returns the same FXApp instance
                # as fox_application references.

FXApp.instance().run

Save this script as desktop_team_performance_graph.rb You can run the script using the following command:

ruby desktop_team_performance_graph.rb

When you run this command, you will see a screen with the text "no data." You can then use the drop-down menus to select a game and a player. The sample database has data only for the Tech Website Baron game. Select that game to see a screen similar to Figure 4-3.


Figure 4-3.  Player graph displayed by the GUI

Let's take a look at the important parts of the script.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials