【发布时间】:2013-05-05 15:39:49
【问题描述】:
我对 Rails 还是很陌生,但我会说进展相当顺利。因此,为了练习,我正在开发一个简单的应用程序,用户可以在其中输入他们的体重,并使用 Lazy Highcharts gem 通过 Highcharts 折线图向他们显示 30 天内的信息。我跟着 Ryan Bates 的 Railscast #223 开始。
我的问题:
好的,所以输入显示,除了在用户不输入值的那一天,它在图表上显示为“0”(线下降到图表底部),而不是连接到下一个点在任何一天。不知道这一切是否有意义,所以这里有一个截图:
我找到了这个解决方案: Highcharts - Rails Array Includes Empty Date Entries
但是,当我实现在该页面上找到的第一个选项时(将 0 转换为 null):
(30.days.ago.to_date..Date.today).map { |date| wt = Weight.pounds_on(date).to_f; wt.zero? ? "null" : wt }
点显示,但线没有,工具提示也没有......这让我认为有些东西正在破坏 js。不过,控制台中显然没有任何问题..?从这里我认为这可能是使用 Highchart 的 'connectNulls' 选项的问题,但这也不起作用。实现第二个选项(拒绝方法)时:
(30.days.ago.to_date..Date.today).map { |date| Weight.pounds_on(date).to_f}.reject(&:zero?)
它从图表中完全删除所有为空的日期,这完全搞乱了结构,因为这些值应该根据它们的 created_at 日期显示。
所以回到第一个方格,这就是我剩下的(图表在没有输入的日子里绘制零)。
型号:
class Weight < ActiveRecord::Base
attr_accessible :notes, :weight_input
validates :weight_input, presence: true
validates :user_id, presence: true
belongs_to :user
def self.pounds_on(date)
where("date(created_at) = ?", date).pluck(:weight_input).last
end
end
控制器:
def index
@weights = current_user.weights.all
@startdate = 30.days.ago.to_date
@pounds = (30.days.ago.to_date..Date.today).map { |date| Weight.pounds_on(date).to_f }
@h = LazyHighCharts::HighChart.new('graph') do |f|
f.options[:title][:text] = " "
f.options[:chart][:defaultSeriesType] = "area"
f.options[:chart][:inverted] = false
f.options[:chart][:zoomType] = 'x'
f.options[:legend][:layout] = "horizontal"
f.options[:legend][:borderWidth] = "0"
f.series(:pointInterval => 1.day, :pointStart => @startdate, :name => 'Weight (lbs)', :color => "#2cc9c5", :data => @pounds )
f.options[:xAxis] = {:minTickInterval => 1, :type => "datetime", :dateTimeLabelFormats => { day: "%b %e"}, :title => { :text => nil }, :labels => { :enabled => true } }
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @weights }
end
end
有人对此有解决方案吗?我想我可能做错了,所以非常感谢任何帮助。
【问题讨论】:
标签: ruby-on-rails highcharts lazy-high-charts