0

我正在使用 LazyHighCharts 并尝试收集 json 数据以仅显示最后 24 小时,但它不显示数据,仅显示日期但仅显示一次(一天中的每个小时都应该有一个温度)。

数据结构

{"status": "ok", "data": [{"2014-06-16 16:00:00": 24.2},{"2014-06-17 12:00:00": 30.2},{"2014-06-18 17:00:00": 42.9}]} etc

控制器

@data = JSON.parse(open(@temperature.url).read)

dates = []
temps = []

@data['data'].each do |data|
 dates << data.keys
 temps << data.values
end 

@graph = LazyHighCharts::HighChart.new('graph') do |f|
 f.chart(:height => '400')
 f.yAxis [:title => {:text => "Temperature", :margin => 20, style: { color: '#333'}}]
 f.series(:pointInterval => 1.hour, :pointStart => 30.day.ago, :type => 'area', :name => '24hrs', :data => [[dates, temps]])
 f.options[:xAxis] = { :minTickInterval => 24 * 3600 * 1000, :type => "datetime", :dateTimeLabelFormats => { day: "%b %e"}, :title => { :text => nil }, :labels => { :enabled => true } }
end

图片

在此处输入图像描述

4

2 回答 2

1

您的日期应该是时间戳(以毫秒为单位的时间,例如 12323311000)而不是这样的形式:“2014-06-16 16:00:00”所以您需要转换它。

于 2014-07-29T09:22:26.837 回答
1

要将您的 datetime 对象转换为 JS 毫秒约定,只需调用

DateTime.now.to_i*1000 

或者在你的情况下

def index
  @data = {"status" => "ok", "data" => [{"2014-06-16 16:00:00" => 24.2},{"2014-06-17 12:00:00" => 30.2},{"2014-06-18 17:00:00" => 42.9}]}
  d = []
  @data['data'].each do |data|
    d << [DateTime.parse(data.keys.first).to_i*1000, data.values.first]
  end 

  @chart = LazyHighCharts::HighChart.new('chart') do |f|
    f.chart(:height => '400')
    f.yAxis [:title => {:text => "Temperature", :margin => 20, style: { color: '#333'}}]
    f.xAxis(type: 'datetime')
    f.series(:type => 'area', :name => '24hrs', :data => d)
  end
end
于 2014-09-26T10:30:11.193 回答