Rails - 为Highcharts Chart数据创建数组

时间:2013-08-05 04:55:04

标签: ruby-on-rails json ruby-on-rails-3 highcharts

我试图绘制一张highcharts图表。我想在x轴上使用datatime并在y轴上计数。到目前为止我所拥有的:

series: [{
name: 'Success',
pointInterval: <%= 1.day * 1000 %>,
pointStart: <%= DateTime.strptime(params[:start_date], '%m/%d/%Y').at_midnight.to_i * 1000 %>,
data: <%= ServerStatus.success.find(:all, :select => 'generated_at, count', :conditions => {:generated_at => DateTime.strptime("07/25/2013", '%m/%d/%Y').beginning_of_day..DateTime.strptime("07/25/2013", '%m/%d/%Y').end_of_day}, :order => 'generated_at ASC').to_json %>

生成:

  

数据:   [{ “计数”:2222, “generated_at”: “2013-07-25T00:00:00Z”},{ “计数”:65, “generated_at”: “2013-07-25T00:00:00Z”},{ “计数”:335, “generated_at”: “2013-07-25T00:00:00Z”},{ “计数”:320, “generated_at”: “2013-07-25T00:00:00Z”},{“计数“:156,” generated_at “:” 2013-07-25T00:00:00Z“}]

我如何按照highcharts期望的方式格式化,我相信[datetime, count]

1 个答案:

答案 0 :(得分:0)

在系列定义之前初始化变量:

array_of_records = ServerStatus.success.find(:all, :select => 'generated_at, count', :conditions => {:generated_at => DateTime.strptime("07/25/2013", '%m/%d/%Y').beginning_of_day..DateTime.strptime("07/25/2013", '%m/%d/%Y').end_of_day}, :order => 'generated_at ASC').to_json

然后,在系列定义中,定义数据如下:

data: <%= array_of_records.map { |record| [record["generated_at"], record["count"]] } %>