15

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?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Pascal
  • 4,127
  • 8
  • 33
  • 29
  • I had the same question, Let me know id there is anything comparable to System.Drawing namespace in .net – Zasz Aug 28 '11 at 11:11

4 Answers4

17

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.

Community
  • 1
  • 1
rampion
  • 87,131
  • 49
  • 199
  • 315
3

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)
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Nathan Clark
  • 121
  • 2
2

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Mike Woodhouse
  • 51,832
  • 12
  • 88
  • 127
  • 2
    Some libraries do exist for ruby. Although you would have to work only with basic elements. As Nathan pointed out, there's https://rubygems.org/gems/rmagick, but there's also https://rubygems.org/gems/cairo. Here's a simple tutorial http://www.hokstad.com/simple-drawing-in-ruby-with-cairo.html. And here's another that uses GTK http://zetcode.com/tutorials/rubygtktutorial/cairo/. I would have to agree that it's not the best API, but it's a start. – Gaston Feb 01 '12 at 18:14
  • 4
    "I need something with Ruby", "Try .NET", WTF. – Dorian Feb 17 '13 at 19:11
  • @Gaston too bad you didn't make an answer out of that. – Daniel Jun 12 '17 at 19:13
0

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