DollarChills
DollarChills

Reputation: 1086

each_with_index only showing last option

I'm using Gruff with Prawn to insert an image graph, I have the bar graph displaying properly, but the labels is only showing the last label. See image here for example.

Using prawn (0.15.0) and gruff (0.5.1)

Prawn

def initialize(result)
 super()
 @result = result
 show_graph
end

def show_graph
lint = @result.map {|v| v.lint/227 } 
g = Gruff::Bar.new('540x200')
g.data(:lint, lint, '#00463f')
@result.each_with_index do |v, i|
g.labels = {i => v.variety.variety_name}
end
g.y_axis_label = 'Yield (bales/ha)'
g.marker_font_size = 16
g.marker_count = 5
g.theme = {:marker_color => '#333333', :font_color => '#333333', :background_colors => %w(#ffffff #ffffff)}
g.minimum_value = 0
g.hide_legend = true

g.write("#{Rails.root}/app/assets/images/chart.png")

image "#{Rails.root}/app/assets/images/chart.png"

end

Controller

@result = Result.where('trial_id' => params[:trial_id]).order('lint DESC')

Upvotes: 0

Views: 81

Answers (1)

Uelb
Uelb

Reputation: 4093

The error you make is that you are redifining the labels hash at each iteration. Instead of doing

g.labels = {i => v.variety.variety_name}

Try :

g.labels[i] = v.variety.variety_name

And before the each, you can put :

g.labels = {}

Upvotes: 1

Related Questions