score:1

Accepted answer

this is a highcharts specfic. you need to pass in the timestamp into your data array vs. defining it for the dataset.

for each data point, i set [time,value] as a tuple.
[ "date.utc(#{date.year}, #{date.month}, #{date.day})" , weight.pounds_on(date).to_f ]

you will still need to remove the zero's in w/e fashion you like, but the data will stay with the proper day value.

i do think you need to remove :pointinterval => 1.day

general tips you should also, look at optimizing you query for pound_on, as you are doing a db call per each point on the chart. do a weight.where(:created_at => date_start..date_end ).group("date(created_at)").sum(:weight_input) which will give you an array of the created_at dates with the sum for each day.

additions

improved sql query

this leans on sql to do what it does best. first, use where to par down the query to the records you want (in this case past 30 days). select the fields you need (created_at and weight_input). then start an inner join that runs a sub_query to group the records by day, selecting the max value of created_at. when they match, it kicks back the greatest (aka last entered) weight input for that given day.

@last_weight_per_day = weight.where(:created_at => 30.days.ago.beginning_of_day..time.now.end_of_day)
select("weights.created_at , weights.weight_input").
joins(" 
  inner join (  
      select weights.weight_input, max(weights.created_at) as max_date 
      from weights 
      group by weights.weight_input , date(weights.created_at) 
  ) weights_dates on weights.created_at =  weights_dates.max_date
")

with this you should be able @last_weight_per_day like so. this should not have 0 / nil values assuming you have validated them in your db. and should be pretty quick at it too.

  @pounds = @last_weight_per_day.map{|date| date.weight_input.to_f}

Related Query

More Query from same tag