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
average = temp.sum / temp.size.to_f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With