0

我试图绘制一个 highcharts 图表。我想要 x 轴上的数据时间和 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 %>

生成:

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

我怎样才能以 highcharts 期望的方式格式化它,我相信这是[datetime, count]

4

1 回答 1

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"]] } %>
于 2013-08-05T05:35:10.087 回答