[wxruby-users] chart extension for wxRuby?

Ellis, Peter
Tue, 22 Apr 2008 00:18:52 -0000

I just wanted to report back to the list on one chart-making technique
that has been working out well for me so far.  I am using a popular open
source scientific and mathematical plotting package called Gnuplot.

http://www.gnuplot.info/

To use Gnuplot I started with a Ruby wrapper called rgplot (found on
RubyForge).

http://rubyforge.org/projects/rgplot

After playing with rgplot a while, I came to the conclusion that the
interface wasn''t really suited for my needs, so I studied the code,
learned a few key things, and ultimately came away with the one or two
lines of code that setup the pipe between Ruby and Gnuplot.  Then I
wrote my own proof-of-concept wrapper to plot a couple of charts:
clustered bar charts, stacked bar charts, line graphs, etc.

Unfortunately, my wrapper is not really good enough for public
consumption, but I can explain how it works and you might be able to use
the same idea for your own applications.  Basically, Gnuplot is run as a
subprocess from Ruby.  You pass whatever commands and data you want to
Gnuplot through a stream to that subprocess.  When it''s done plotting,
Gnuplot writes an image file to disk (JPG or other format).  Finally you
create a Wx::Image from the image file and draw it as Wx::Bitmap on a
dc.

You can get pretty nice looking charts out of Gnuplot but you might have
to work at it.  The Gnuplot syntax can be a pain, but it does come with
a lot of example files that help a lot.  It also has an interactive GUI
that is handy for testing command syntax.  One thing that Gnuplot cannot
do is pie charts.  However, if you really need pie charts, they would be
easy enough to implement yourself by drawing directly with wx.

To implement your own wrapper for Gnuplot, I definitely recommend
studying rgplot (or use it "as is", if it works for you).  One thing
to
look out for is that Gnuplot will be running asynchronously from
whatever else you are doing in Ruby or wx.  So I use the on_timer event
to watch for the image file to be completed.  After a lot of
experimentation, I found that (File.exists?(path) and File.size(path) >
0) is the best test to see if the image file is actually ready to be
loaded into wx.  Also, if you need to create multiple charts at the same
time, you will need to write a manager that sequentially feeds jobs to
Gnuplot and then collects the resulting images.  I was able to do that
without too much trouble, again using the on_timer event.

Maybe someday I''ll be able to put together a wrapper that I would be
willing to share.  At the moment I''m not working on this actively, but
I
hope to get back to it in a few months.

Best of luck,
P

-----Original Message-----
From: wxruby-users-bounces at rubyforge.org
[mailto:wxruby-users-bounces at rubyforge.org] On Behalf Of Andreas Warberg
Sent: Monday, April 21, 2008 5:53 AM
To: wxruby-users at rubyforge.org
Subject: Re: [wxruby-users] chart extension for wxRuby?

Alex Fenton wrote:
> I think you''ve misunderstood the purpose of the example. It was
just
to
> show a set up where a scruffy graph would render using a wxRuby DC
> (device context) rather than to an SVG object.
>
> What remains for someone with motivation to do is to convert or
> translate the calls that scruffy makes to an svg object inside the
> library (eg svg.line, svg.circle) into the equivalent DC calls
> (dc.draw_line, dc.draw_circle). The relevant parts of scruffy to look
at
> are in the components and layers directory.

You''re right I did misunderstand. I might pursue your first suggestion
to create the svg and the load it as an image, though it is suboptimal.

I understand you can divert the output procedure to the screen but since

it is not a part of the "standard package" - and I thus need to write
it

myself - I don''t think it is a good route to take, considering time and
reliability.

Thanks!

Andreas
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
wxruby-users mailing list
wxruby-users at rubyforge.org