Pascal
Pascal

Reputation: 4127

Drawing Library for Ruby

I am trying to code a flowchart generator for a language using Ruby.

I wanted to know if there were any libraries that I could use to draw various shapes for the various flowchart elements and write out text to those shapes.

I would really prefer not having to write code for drawing basic shapes, if I can help it.

Can someone could point me to some reference documentation with examples of using that library?

Upvotes: 15

Views: 11871

Answers (4)

Nathan Clark
Nathan Clark

Reputation: 121

It sounds like you're going to be limited mainly by the capabilities of whatever user agent you're building for; if this is a web project, drawing capabilities are going to be dependent on the browser. Since Ruby is running server-side, you would at minimum need some JavaScript to allow dragging/zooming, etc. There's plenty of examples of JavaScript being used for vector drawing (just google "javascript graphics library"), but all require coding, and I haven't seen any library that abstracts this elegantly.

ImageMagick has a Ruby binding called RMagick (sometimes known by other names, depending on the repository). (Link) I haven't used it myself, but I believe it will do what you're looking for. You will need to do some coding, but it's along the lines of

draw.rectangle(x1, y1, x2, y2)
draw.polygon(x1, y1,...,xN, yN)

Upvotes: 3

Mike Woodhouse
Mike Woodhouse

Reputation: 52316

The simple answer is that (at time of writing) what you want almost certainly didn't exist. Sorry!

If you're Windows-based then I'd look for a product in the .NET space, where I expect you'd find something. You're probably going to have to pay real money though. I suppose, if you're brave, you may be able to talk to it using IronRuby.

From a non-MS environment I would be hoping for a Java-based solution. As already mentioned, within the Web world, you're going to be hoping some something JavaScript-based, or probably more likely, something from the Flash (or maybe even Silverlight?) world.

Actually, if you want to stay in Ruby and don't mind some risk, Silverlight might be a way to go - assuming the DLR stuff actually works (no experience here). Ruby - much as I love it - hardly has the most mature GUI structure around it.

Update: Since writing this (approaching 5 years ago) the situation has improved - a little. While I'm still not aware of any specific Ruby graph-drawing libraries, there is improved support for Graphviz here (Github) or by gem install ruby-graphviz. I've found that for simple graphs, it's quite possible to write the script directly from Ruby.

Upvotes: 2

rampion
rampion

Reputation: 89053

Write up your flowchart as a directed or undirected graph in Graphviz. Graphviz has a language, dot that makes it easy to generate graphs. Just generate the dot file, run it through Graphiviz, and you get your image.

graph {
  A -- B -- C;
  B -- D;
  C -- D [constraint=false];
}

renders as
undirected

digraph {
  A [label="start"];
  B [label="eat"];
  C [label="drink"];
  D [label="be merry"];

  A -> B -> C;
  C -> D [constraint=false];
  B -> D [ arrowhead=none, arrowtail=normal]; // reverse this edge
}

renders as
directed

You can control node shapes and much more in Graphviz.

Upvotes: 17

Reputation:

I am totally sure, but check out cairo bindings for ruby. Pango for text. I am investigating then currently and came across this page.

Upvotes: 0

Related Questions