Highcharts - Rails数组包含空的日期条目 [英] Highcharts - Rails array includes empty date entries

查看:109
本文介绍了Highcharts - Rails数组包含空的日期条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Highcharts在一个Rails 3.1.1应用程序和各种线图的仪表板上工作。我在Railcast 223(highcharts)中实现了示例代码 - 并且一切都按预期工作。但是在没有条目存在的日子里,数组绘制了一个0值 - 但我只想看线图的实际记录。

Railscast没有问题,因为它们每天都有多个Order条目。这似乎是由另一个用户(托马斯)发布的同样的问题...虽然他的代码没有在我的情况。

  Index view:

$(function(){
new Highcharts.Chart({
chart:{renderTo:'orders_chart'},
title:{ text:'Orders by Day'},
xAxis:{type:'datetime'},
yAxis:{
title:{text:'Dollars'}
},
tooltip:{
formatter:function(){
return Highcharts.dateFormat(%B%e%Y,this.x)+':'+
'$' + Highcharts.numberFormat(this.y,2);
}
},
series:[{
pointInterval:<%= 1.day * 1000%> ;,
pointStart:<%= 3.weeks.ago.at_midnight.to_i * 1000%>,
data:<%=(3.weeks.ago_to_date..Date.today)。 map {| date | Order.total_on(date).to_f} .inspect%>
}]
});
});


class Order< ActiveRecord :: Base
def self.total_on(date)
where(date(purchased_at)=?,date).sum(:total_price)
end
end

任何输入都将非常感谢 - 新的轨道,并已找到100种方法不起作用!如果你想什么都不是来绘制,但日期仍然显示出来,那么就可以使用 将任何零更改为javascript null 。如果你不想绘制那些日子,那么你应该在输出前从数组中拒绝它们。



所以:

 (3.weeks.ago.to_date..Date.today).MAP {|日期|总= Order.total_on(日期).to_f; total.zero? ? null:total} .inspect 

或者:

 (3.weeks.ago.to_date..Date.today).map {| date | Order.total_on(date).to_f} .reject(&:zero?)。检查


I'm working on a Rails 3.1.1 app with a dashboard of various line graphs using Highcharts. I implemented the sample code in Railcast 223 (highcharts) -- and everything works as expected. But on days when no entries exists, the array plots a 0 value -- but I'm only looking to line graph actual records.

Its not an issue with the Railscast because they sample has multiple Order entries each day. It appears to be the same issue posted by another user (Thomas)...though his code didn't work in my situation.

Index view:

    $(function () {
  new Highcharts.Chart({
    chart: { renderTo: 'orders_chart' },
    title: { text: 'Orders by Day' },
    xAxis: { type: 'datetime' },
    yAxis: {
      title: { text: 'Dollars' }
    },
    tooltip: {
      formatter: function () {
        return Highcharts.dateFormat("%B %e %Y", this.x) + ': ' +
          '$' + Highcharts.numberFormat(this.y, 2);
      }
    },    
    series: [{
      pointInterval: <%= 1.day * 1000 %>,
      pointStart: <%= 3.weeks.ago.at_midnight.to_i * 1000 %>,
      data: <%= (3.weeks.ago.to_date..Date.today).map { |date| Order.total_on(date).to_f}.inspect %>
    }]
  });
});


    class Order < ActiveRecord::Base
  def self.total_on(date)
    where("date(purchased_at) = ?",date).sum(:total_price)
  end
end

Any input would be greatly appreciate -- new to rails and have already found the 100 ways the don't work! Thanks!

解决方案

If you want nothing to be graphed but the date to still show up, then change any zeroes to a javascript null. If you want to not graph those days, then you should reject them from the array before output.

So:

(3.weeks.ago.to_date..Date.today).map{|date| total=Order.total_on(date).to_f; total.zero? ? "null" : total }.inspect

Or:

(3.weeks.ago.to_date..Date.today).map{|date| Order.total_on(date).to_f}.reject(&:zero?).inspect

这篇关于Highcharts - Rails数组包含空的日期条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆