Rails 3:为 highcharts 创建紧凑数组包含 nil,但不应该

发布于 2024-12-17 08:02:37 字数 1515 浏览 0 评论 0原文

我使用 Rails 3 和 Highcharts JS 库来创建图表。而且设置效果很好。唯一的问题是我的数据数组还包含零数据。但让我先向您展示代码。以下代码片段定义了我的 Highchart 的数据。

data: <%= (30.days.ago.to_date..Date.today).map { |date| Health.total_on_weight(current_user.id, date).to_f}.compact %>

“Health”是模型,“total_on_weight”是该模型中定义的方法:

def self.total_on_weight(id, date)
  where("user_id = ?", id).where("date(date) = ?",date).average(:weight) unless self.nil?
end

如您所见,我已经尝试创建一个仅包含现有数据的数组。如果我的数据库中没有该日期的条目,则它不应该在数组中,但事实上,它是在数组中。

我已经尝试使用函数中的“compact”方法和“unless”方法删除这些条目。但似乎还有另一个问题我无法解决。

如果有人可以向我解释为什么零条目仍然在我的数组中或者有解决方案,我将非常感激。

好的,我缩小了问题范围:

如果我将“to_f”留在函数中,就像现在一样,compact 无法删除缺失值,因为它们不是 nil,而是 0。数组看起来像这样:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0, 100.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 90.0]

然后,如果我省略“to_f”,“compact”函数可以成功删除缺失的值,这给我一个像这样的数组:

[100.0, 100.0, 100.0, 90.0]

但这样做的缺点是,Highcharts 不再有我的值的正确日期。它按照我想要的方式正确显示它们,但日期没有任何意义。

更新:

我设法自己解决了这个问题。数据现在定义为:

data: [
  <% @weights = Health.where("user_id = ?", current_user.id) %>
  <% @weights.each do |weight| %>
    <%= "[Date.UTC(" + weight.date.year.to_s  + ", " + ( weight.date.month - 1 ).to_s  + ", " + weight.date.day.to_s  + ")" + ", " + weight.weight.to_f.round(2).to_s + " ],"%>
  <% end %>
]

这绝对有效。

I'm using Rails 3 and the Highcharts JS Library for creating charts. And the setup works just fine. The only problem is that my data array also includes nil-data. But let me show you the code first. The following snippet defines the data for my Highchart.

data: <%= (30.days.ago.to_date..Date.today).map { |date| Health.total_on_weight(current_user.id, date).to_f}.compact %>

"Health" is the model and "total_on_weight" is a method which is defined in that model:

def self.total_on_weight(id, date)
  where("user_id = ?", id).where("date(date) = ?",date).average(:weight) unless self.nil?
end

As you can see I already tried to create an array which contains only existing data. If there isn't an entry in my database for the date, it shouldn't be in the array, but as a matter of fact, it is.

I already tried do delete those entrys with the "compact" method and the "unless" method in my function. But there seems to be another problem which I can't wrap my head around.

If anybody could explain to me why the nil-entries are still in my array or would have an sollution I would be very thankful.

OK, I narrowed the problem down:

If i leave the "to_f" in the function, as it now is, compact can't erase the missing values because they are not nil, but 0. And the array looks like this:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0, 100.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 90.0]

Then, if I leave out the "to_f", the "compact" function can succesfully delete the missing values, which gives me an array like this:

[100.0, 100.0, 100.0, 90.0]

But the downside of this is, that Highcharts doesn't has the right dates for my values anymore. It displays them right as I wanted, but the dates just don't make any sense.

UPDATE:

I managed to fix the problem on my own. The data is now defined by:

data: [
  <% @weights = Health.where("user_id = ?", current_user.id) %>
  <% @weights.each do |weight| %>
    <%= "[Date.UTC(" + weight.date.year.to_s  + ", " + ( weight.date.month - 1 ).to_s  + ", " + weight.date.day.to_s  + ")" + ", " + weight.weight.to_f.round(2).to_s + " ],"%>
  <% end %>
]

This definitely does the trick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2024-12-24 08:02:37

我设法自己解决了这个问题。数据现在定义为:

data: [
  <% @weights = Health.where("user_id = ?", current_user.id) %>
  <% @weights.each do |weight| %>
    <%= "[Date.UTC(" + weight.date.year.to_s  + ", " + ( weight.date.month - 1 ).to_s  + ", " + weight.date.day.to_s  + ")" + ", " + weight.weight.to_f.round(2).to_s + " ],"%>
  <% end %>
]

I managed to fix the problem on my own. The data is now defined by:

data: [
  <% @weights = Health.where("user_id = ?", current_user.id) %>
  <% @weights.each do |weight| %>
    <%= "[Date.UTC(" + weight.date.year.to_s  + ", " + ( weight.date.month - 1 ).to_s  + ", " + weight.date.day.to_s  + ")" + ", " + weight.weight.to_f.round(2).to_s + " ],"%>
  <% end %>
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文