Ruby on Rails - 使用来自 JSON(样式)数组的数据
Ruby on Rails - Using the Data from JSON (style) Array
Ruby'NooB'问题。
我正在使用 gem 'keen',以 运行 查询 returns 一个(JSON 样式)多行字符串:
查询
-@a = Keen.median
returns(数组)
[{"value"=>3, "timeframe"=>{"start"=>"2015-03-01T02:00:00.000Z", "end"=>"2015-03-01T03:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T03:00:00.000Z", "end"=>"2015-03-01T04:00:00.000Z"}}, {"value"=>2, "timeframe"=>{"start"=>"2015-03-01T04:00:00.000Z", "end"=>"2015-03-01T05:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T05:00:00.000Z", "end"=>"2015-03-01T06:00:00.000Z"}}, {"value"=>-1, "timeframe"=>{"start"=>"2015-03-01T06:00:00.000Z", "end"=>"2015-03-01T07:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T07:00:00.000Z", "end"=>"2015-03-01T08:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T08:00:00.000Z", "end"=>"2015-03-01T09:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T09:00:00.000Z", "end"=>"2015-03-01T10:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T10:00:00.000Z", "end"=>"2015-03-01T11:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T11:00:00.000Z", "end"=>"2015-03-01T12:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T12:00:00.000Z", "end"=>"2015-03-01T13:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T13:00:00.000Z", "end"=>"2015-03-01T14:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T14:00:00.000Z", "end"=>"2015-03-01T15:00:00.000Z"}}, {"value"=>-2, "timeframe"=>{"start"=>"2015-03-01T15:00:00.000Z", "end"=>"2015-03-01T16:00:00.000Z"}}, {"value"=>-1, "timeframe"=>{"start"=>"2015-03-01T16:00:00.000Z", "end"=>"2015-03-01T17:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T17:00:00.000Z", "end"=>"2015-03-01T18:00:00.000Z"}}, {"value"=>3, "timeframe"=>{"start"=>"2015-03-01T18:00:00.000Z", "end"=>"2015-03-01T19:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T19:00:00.000Z", "end"=>"2015-03-01T20:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T20:00:00.000Z", "end"=>"2015-03-01T21:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T21:00:00.000Z", "end"=>"2015-03-01T22:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T22:00:00.000Z", "end"=>"2015-03-01T23:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T23:00:00.000Z", "end"=>"2015-03-02T00:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-02T00:00:00.000Z", "end"=>"2015-03-02T01:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-02T01:00:00.000Z", "end"=>"2015-03-02T02:00:00.000Z"}}]
我希望能够绘制此数据图表(例如使用 gem 'Gchart')。
-@chart = GChart.line do |g|
g.data = [@a[:value]]
g.colors = [:red, :yellow]
g.legend = ["Line", "Wonkiness"]
g.width = 600
g.height = 150
g.entire_background = "f4f4f4"
g.axis(:left) { |a| a.range = 0..6 }
g.axis :bottom do |a|
a.labels = ["@a[:timeframe]"]
a.label_positions = [0, 50, 100]
a.text_color = :black
end
g.axis :bottom do |a|
a.labels = ["Foo"]
a.label_positions = [50]
end
end
最'ruby' 使用(或格式化)此数组数据(到散列?)的方法是什么。因此它可以很容易地用于其他功能(即图表)。
提前致谢。
您想传递 Gchart.line 从哈希数组中提取的值数组。你可以这样做:
g.data = @a.map{|a| a["value"]}
示例:
@a=[{"value" => 1, b:2},{"value" => 3, b:4}]
@a.map{|a| a["value"]}
returns:
[1, 3]
Ruby'NooB'问题。
我正在使用 gem 'keen',以 运行 查询 returns 一个(JSON 样式)多行字符串:
查询
-@a = Keen.median
returns(数组)
[{"value"=>3, "timeframe"=>{"start"=>"2015-03-01T02:00:00.000Z", "end"=>"2015-03-01T03:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T03:00:00.000Z", "end"=>"2015-03-01T04:00:00.000Z"}}, {"value"=>2, "timeframe"=>{"start"=>"2015-03-01T04:00:00.000Z", "end"=>"2015-03-01T05:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T05:00:00.000Z", "end"=>"2015-03-01T06:00:00.000Z"}}, {"value"=>-1, "timeframe"=>{"start"=>"2015-03-01T06:00:00.000Z", "end"=>"2015-03-01T07:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T07:00:00.000Z", "end"=>"2015-03-01T08:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T08:00:00.000Z", "end"=>"2015-03-01T09:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T09:00:00.000Z", "end"=>"2015-03-01T10:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T10:00:00.000Z", "end"=>"2015-03-01T11:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T11:00:00.000Z", "end"=>"2015-03-01T12:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T12:00:00.000Z", "end"=>"2015-03-01T13:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T13:00:00.000Z", "end"=>"2015-03-01T14:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T14:00:00.000Z", "end"=>"2015-03-01T15:00:00.000Z"}}, {"value"=>-2, "timeframe"=>{"start"=>"2015-03-01T15:00:00.000Z", "end"=>"2015-03-01T16:00:00.000Z"}}, {"value"=>-1, "timeframe"=>{"start"=>"2015-03-01T16:00:00.000Z", "end"=>"2015-03-01T17:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T17:00:00.000Z", "end"=>"2015-03-01T18:00:00.000Z"}}, {"value"=>3, "timeframe"=>{"start"=>"2015-03-01T18:00:00.000Z", "end"=>"2015-03-01T19:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T19:00:00.000Z", "end"=>"2015-03-01T20:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T20:00:00.000Z", "end"=>"2015-03-01T21:00:00.000Z"}}, {"value"=>1, "timeframe"=>{"start"=>"2015-03-01T21:00:00.000Z", "end"=>"2015-03-01T22:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T22:00:00.000Z", "end"=>"2015-03-01T23:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-01T23:00:00.000Z", "end"=>"2015-03-02T00:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-02T00:00:00.000Z", "end"=>"2015-03-02T01:00:00.000Z"}}, {"value"=>0, "timeframe"=>{"start"=>"2015-03-02T01:00:00.000Z", "end"=>"2015-03-02T02:00:00.000Z"}}]
我希望能够绘制此数据图表(例如使用 gem 'Gchart')。
-@chart = GChart.line do |g|
g.data = [@a[:value]]
g.colors = [:red, :yellow]
g.legend = ["Line", "Wonkiness"]
g.width = 600
g.height = 150
g.entire_background = "f4f4f4"
g.axis(:left) { |a| a.range = 0..6 }
g.axis :bottom do |a|
a.labels = ["@a[:timeframe]"]
a.label_positions = [0, 50, 100]
a.text_color = :black
end
g.axis :bottom do |a|
a.labels = ["Foo"]
a.label_positions = [50]
end
end
最'ruby' 使用(或格式化)此数组数据(到散列?)的方法是什么。因此它可以很容易地用于其他功能(即图表)。
提前致谢。
您想传递 Gchart.line 从哈希数组中提取的值数组。你可以这样做:
g.data = @a.map{|a| a["value"]}
示例:
@a=[{"value" => 1, b:2},{"value" => 3, b:4}]
@a.map{|a| a["value"]}
returns:
[1, 3]