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

No comments:

Powered By Blogger