Search code examples
ruby-on-railsjsonhighchartsruby-on-rails-3.2lazy-high-charts

Parsing JSON Data for Lazy Highcharts


I'm having some issues with parsing some JSON data into high charts using the Lazy Highcharts gem. I'm trying to select only the data from the last 7 days, or 1 week ago. At this stage my application just hangs and doesn't load with the code below.

I am loading the JSON data from a link.I have tried the pointStart option, but it doesn't seem to work.

Any help would be appreciated.

JSON

{"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

Controller

@data =  Oj.load(open(@temperature.url).read)

results = []

@data['data'].each do |data|
 results << ((7.day.ago.to_i * 1000)..(Date.today.to_i * 1000)).map { |date| [DateTime.parse(data.keys.first).to_i * 1000 == date, data.values.first] }
end 

@graph = LazyHighCharts::HighChart.new('graph') do |f|
  f.chart(:height => '400', width: '860')
  f.yAxis [:title => {:text => "Temperature, :margin => 20, style: { color: '#333'}}]
  f.series(:type => 'line', :name => 'Temperature', pointStart: 7.day.ago.to_i * 1000, data: results, marker: {enabled: false}, :color => '#00463f' )
  f.xAxis(:type => 'datetime', tickInterval: 1.day.to_i * 1000, :tickmarkPlacement => 'on', :startOnTick => true )
  f.legend({:align => 'center', :verticalAlign => 'top', :y => 0, :borderWidth => 0, style: {color: "#333"}})
end

Solution

  • I have solved this. If any one is interested I added;

    min: 1.weeks.ago.at_midnight.to_i * 1000
    

    To the xAxis.