Graphics with Prawn
Asked Answered
C

3

5

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?

Celebrant answered 25/10, 2012 at 14:54 Comment(0)
H
9

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
Hoebart answered 19/11, 2012 at 9:14 Comment(2)
is there a way to pass prawn the image bytes instead of writing it to disk and passing to to prawn?Viscera
@Viscera not possible imo.Hoebart
S
5

@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)
Stamina answered 23/4, 2014 at 22:56 Comment(0)
R
3

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.

Rooker answered 17/12, 2013 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.