Gruff – good looking charts in Rails

Gruff is plugin for creating pretty decent charts in Rails. It has nice straight forward API and couple of built-in themes.

Gruff in action

To render it I’ve used(Haml)

 %img{:src =>'/izvjestaji/ucestalost_po_rasvjetnom_mjestu_graf' ,
:style =>'border:1px solid #aabcca;'}

and in controller:

  def ucestalost_po_rasvjetnom_mjestu_graf
    g = create_pie
    ucestalost_po_rasvjetnom_mjestu.each do |item|
      g.data item.oznaka.to_s, item.broj_kvarova.to_i
    end
    send_data(g.to_blob(), :disposition => 'inline', :type => 'image/png',
:filename => "ucestalost_po_rasvjetnom_mjestu")
  end
  def create_pie
    g = Gruff::Pie.new('948x450')
    g.theme_pastel
    g.font = "#{Prawn::BASEDIR}/data/fonts/times.ttf"
    g.legend_font_size = 10
    g.hide_title
    g.title_margin = 0
    g.title_font_size = 0
    return g
  end

My biggest issue with Gruff was having to install RMagick. Please read RMagick FAQ. You’ll have to compile it, and make sure you have all the libraries it needs.

Major grief were Croatian characters(ŠĐŽĆČ) but it applies to all utf-8. Worst part was that compile went OK, but result was all but OK :)

To save some time make sure you:

1. Use true type font that has chars that you want
2. Install RMagick with all the -dev libraries that it needs (otherwise special characters are screwed up)
3. FreeType library is a must – make sure you have it before compilation

When I look at the result I wish it had 3D pie, but never the less it’s worth to keep an eye on.