Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphics with Prawn

Tags:

ruby

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?

like image 502
Ricardo Acras Avatar asked Oct 25 '12 14:10

Ricardo Acras


3 Answers

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
like image 191
vvohra87 Avatar answered Nov 19 '22 00:11

vvohra87


@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)
like image 31
Marcin Bilski Avatar answered Nov 18 '22 22:11

Marcin Bilski


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.

like image 3
Powers Avatar answered Nov 19 '22 00:11

Powers