Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Average Rails [duplicate]

I want to show the average of my array 'temp' (to be the average array) in my controller, but unsure on how to process this. Can I use a scope method a call in the controller? Or can I do it directly in the controller?

Controller

def show
    @soiltemp = Soiltemp.find(params[:id])
    @data = CSV.open(@soiltemp.csv.path, :headers => true, :encoding => 'ISO-8859-1')
    dates = []
    temp = []
    @data.each do |row|  
     dates << row[1]
     temp << row[2].to_i
    end
    average = ?
    @graph = LazyHighCharts::HighChart.new('graph') do |f|
      f.title({ :text => @soiltemp.site + " Soil Temperatures" })
      f.options[:xAxis][:categories] =  dates
      f.options[:plotOptions] = {pointInterval: 7.day * 7000}
      f.series(:type => 'area', :name => 'Temperature', :data => temp, :color => '#00463f') 
      f.series(:type => 'spline',:name => 'Average', :data => average) 
    end
like image 815
yellowskull Avatar asked Nov 17 '13 05:11

yellowskull


1 Answers

 average = temp.sum / temp.size.to_f
like image 162
Sully Avatar answered Oct 20 '22 13:10

Sully