0

I'm using Highcharts to display other graphs from csv's in my application, but now I have to access JSON data from a url (test.com/data/data1,temp) and i'm not sure how to best tackle this scenario.

The JSON data is formatted as such:

{"status": "ok", "data": [{"2013-10-01 00:00:00": 19.6}, {"2013-10-01 01:00:00": 19.1}, {"2013-10-01 02:00:00": 18.4}, {"2013-10-01 03:00:00": 17.9}, {"2013-10-01 04:00:00": 17.5}, {"2013-10-01 05:00:00": 17.2}, {"2013-10-01 06:00:00": 17.2}, {"2013-10-01 07:00:00": 17.4}]} 

and i have set up a form to input the url (as there are 30 locations) to generate each chart. I'm also not sure on how best to separate the two results, e.g.. the first being 'date' the second 'temperature'.

@data = JSON.parse(open("test.com/data/data1,temp").read) @dates = Array.new @temperatures = Array.new @data['data'].each do |d| @dates.push(d.keys) @temperatures.push(d.values) end @graph = LazyHighCharts::HighChart.new('graph') do |f| f.chart(:height => '300') f.yAxis [:title => {:text => "Temperature", :margin => 20, style: { color: '#333'}}] f.series(:type => 'line', :name => 'Temperature', :data => @temperatures) f.xAxis(:categories => @dates, labels: {overflow: 'justify', :rotation => 90, :step => 10} ) end 

Any help would be appreciated.

    1 Answer 1

    2

    I don't know if it's the best practice, but you can store dates in an array and temperatures in an other one like this :

    # Considering you already stored your data, you need to parse it now data = JSON.parse(your_data) # Initialize your two arrays @dates = Array.new @temperatures = Array.new # Fill your two arrays data["data"].each do |d| @dates.push(d.keys) @temperatures.push(d.values.to_i) # Need to be a number to work with .series of HightCharts end 

    Now you have the @dates array which contains all your dates and @temperatures array which contains all your temperatures.

    Hope this help !

    2
    • Thanks Kilian, has helped immensely. Although I can not display the values data in the graph. Code updated.CommentedJun 11, 2014 at 3:17
    • I think it's because HighChart wants number for series (I forget that details). So try to force value of @temperatures as integer like that @temperatures.push(d.values.to_i)
      – Kilian
      CommentedJun 11, 2014 at 7:25

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.