Friday, May 30, 2008

Displaying Gruff Graphs in RubyCocoa v2

After getting the last post to be as minimal as possible and popping a window using RubyCocoa I wanted something more complete...

There were lots of little goals here:
a) Add a real icon (I assumed this was drag-n-drop in IB or XCode, not so much, but it is simple) b) dynamically generate an interesting icon at run-time (this is actually really easy) c) generate "interesting random" graphs for gruff.

Solution a) Edit the
Info.plist and set the CFBundleIconFile entry to an appropriately created .icns file

Solution b) in some method (I chose awakeFromMib) simply fetch the currently running application thusly: @thisApp = OSX::NSApplication.sharedApplication and call the setApplicationIconImage method with and NSImage (BOOM!)

Solution c) trivial

I've done all of those things in the
attached code

Friday, May 16, 2008

Displaying Gruff Graphs in RubyCocoa

In the quest to do interesting things in RubyCocoa I decided I had display gruff graphs in native windows. Luckily it turns out this is very very easy, simply create a new Ruby-Cocoa project in XCode, add the following class to the project, instantiate it in InterfaceBuilder, connect it to an NSImageView called imageOutlet and Boom!

Done.

(Thanks to Jim Morris for his Ruby-HTMLization-fu)


#  GruffGraphController.rb
# GrufferGrapher
#
# Created by Dave on 5/16/08.
# Copyright (c) 2008. All rights reserved.
#

require 'osx/cocoa'
require 'rubygems'
require 'gruff'

class GruffGraphController < OSX::NSObject

 ib_outlet :imageOutlet

 def awakeFromNib
  g = Gruff::Pie.new
  g.data("ASN 1", 5)
  g.data("ASN 2", 3)

  b = g.to_blob

  d = OSX::NSData.alloc.initWithBytes_length(b, b.length)

  i = OSX::NSImage.alloc.initWithData(d)

  @imageOutlet.setImage(i)
 end
end
Powered By Blogger