Ricardo Acras
Ricardo Acras

Reputation: 36244

Graphics with Prawn

Looking to a gem that adds gtraphing capabilities to prawn, I found this one but it seems a litle outdated. Is there any more active gem for that?

Upvotes: 5

Views: 2312

Answers (3)

Marcin Bilski
Marcin Bilski

Reputation: 611

@eggie5 Regarding using gruff with prawn to insert an image without saving it to disk, it's pretty simple:

image StringIO.new(g.to_blob)

Upvotes: 5

Powers
Powers

Reputation: 19328

I created a Prawn Graphing library called PrawnCharts that only depends on Prawn and does not rely on rMagick and ImageMagick. rMagick and ImageMagick are annoying dependencies (big files, painful to install, etc.) and create larger files compared to a native solution like PrawnCharts.

Here is an example of a graph I generated with PrawnCharts:

enter image description here

Feel free to submit pull requests - I will merge them.

Upvotes: 3

vvohra87
vvohra87

Reputation: 5664

There is nothing very active for graphing inside Prawn directly, but Gruff is an active gem which is highly configurable and will allow you to make all kinds of graphs.

In fact prawn-graph is basically a wrapper around gruff!

My advise is to use gruff to generate the required charts and graphs then embed them as images in Prawn document.

So the code would look something like this:

g = Gruff::Line.new(400)
g.title = "Transparent Background"
g.theme = {
  :colors => ['black', 'grey'],
  :marker_color => 'grey',
  :font_color => 'black',
  :background_colors => 'transparent'
}
g.labels = {
  0 => '5/6',
  1 => '5/15',
  2 => '5/24',
  3 => '5/30',
}
g.data(:apples, [-1, 0, 4, -4])
g.data(:peaches, [10, 8, 6, 3])
g.write(path_to_save)

Prawn::Document.generate("graphed-pdf.pdf") do
    text "The image will go right below this line of text."
    image "#{path_to_save}"
end

Upvotes: 9

Related Questions