Generate pie chart with Gruff gem in Ruby

1.3k Views Asked by At

I am attempting to create a pie chart using the gruff gem, but my chart is a black abyss regardless of what I do. This is my code:

association_disposition_pie_chart = Gruff::Pie.new
association_disposition_pie_chart.title = "Visual Pie Graph Test"
association_disposition_pie_chart.data 'Solved', 10
association_disposition_pie_chart.data 'Action Required', 50
    association_disposition_pie_chart.theme = {
      :colors => ['#A5D8D8', '#EFAD1C'],
      :font_color => 'black',
      :background_colors => 'white'
    }
association_disposition_pie_chart.write("association_disposition_pie_chart.jpg")

Why is this creating a black pie chart? The background is white, the font_color is black, but so is the entire chart. I want the chart pieces to be the colors specified in :colors.

EDIT

Screenshot:

http://i39.tinypic.com/33ne1r6.jpg

2

There are 2 best solutions below

7
dax On

This is mentioned in the documentation:

You can set a theme manually. Assign a hash to this method before you send your data.

graph.theme = {
  :colors => %w(orange purple green white red),
  :marker_color => 'blue',
  :background_colors => %w(black grey)
}
:background_image => 'squirrel.png' is also possible.

(Or hopefully something better looking than that.)

Although the source is more helpful:

# File 'lib/gruff/base.rb', line 300

def theme=(options)
  reset_themes()

  defaults = {
    :colors => ['black', 'white'],
    :additional_line_colors => [],
    :marker_color => 'white',
    :font_color => 'black',
    :background_colors => nil,
    :background_image => nil
  }
  @theme_options = defaults.merge options

  @colors = @theme_options[:colors]
  @marker_color = @theme_options[:marker_color]
  @font_color = @theme_options[:font_color] || @marker_color
  @additional_line_colors = @theme_options[:additional_line_colors]

  render_background
end

I think maybe the problem is your colors attribute - :colors => ['#A5D8D8', '#EFAD1C'] - as Shaun Frost Duke Jackson mentioned, it looks like you need to use add_color('#c0e9d3') to do that, but the documentation isn't clear where you do that if you're defining the theme in line. It might be easier to add your own theme in the THEMES module:

LUIGIS_THEME = {
      :colors => [
        '#A5D8D8',
        '#EFAD1C'
      ],
      :marker_color => '#55ae36', 
      :font_color => 'black',
      :background_colors => 'white'
    }

which is then called with g.theme = Gruff::Themes::LUIGIS_THEME

1
b00stfr3ak On

While using imagemagick-no-hdri and the default rmagick gem the pie graphs would become black and white. I was able to fix this issue by doing the following

Install imagemagick
git clone [email protected]:rmagick/rmagick.git
gem build rmagick.gemspec
gem install ./rmagick-2.13.2.gem