2014-06-10 3 views
0

내 응용 프로그램에서 csv의 다른 그래프를 표시하기 위해 Highcharts를 사용하고 있지만 이제 URL (test.com/data/data1,temp)에서 JSON 데이터에 액세스해야하며 확실하지 않습니다. 이 시나리오에 최선의 대처 방법.URL의 JSON 데이터

JSON 데이터는 같은 포맷 :

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

난 각 차트를 생성하기 위해 (30 곳이 있기 때문에) 입력 폼에 URL을 설정했다. 또한 두 결과를 구분하는 최선의 방법이 확실하지 않습니다. 예를 들어, 첫 번째 '날짜'는 두 번째 '온도'입니다.

@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 

어떤 도움을 주시면 감사하겠습니다.

답변

2

것이 가장 좋은 방법인지 잘 모르겠지만,이 같은 다른 하나의 배열에서 날짜와 온도를 저장할 수

# 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 

지금 모든 날짜를 포함 @dates 배열을 모든 온도를 포함하는 @temperatures 어레이.

희망이 도움이됩니다.

+0

감사합니다. 킬리안은 엄청난 도움을주었습니다. 그래프에는 값 데이터를 표시 할 수 없지만 코드가 업데이트되었습니다. – DollarChills

+0

HighChart가 시리즈 번호를 원하기 때문에 (그 세부 정보는 잊어 버렸습니다.) 그래서 @ temp.push (d.values.to_i)와 같은 정수로 @temperatures 값을 넣으십시오. – Kilian

+0

도움을 주셔서 감사합니다 Kilian. – DollarChills

관련 문제