3

Ruby 'NooB' question.

I'm using the gem 'keen', to run a query that returns a (JSON style) multi line string:

query

-@a = Keen.median

returns (array)

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

I'd like to be able to chart this data (e.g. using 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

What's the most 'ruby' way to use (or format) this array data (to a hash?). So it can be easily used for other functions (i.e. charts).

Thank you in advance.

  • Each element it is a Hash actually( `@a[0].class #=> Hash`), so what is your question. You can just iterate through this array and get Hash values with `@a['value']`, `@a['timeframe]` etc. – Rustam Gasanov Mar 02 '15 at 03:47
  • @RustamA.Gasanov - Firstly, thank you for your comment. When I iterate through this array with `@a.map{|a| a[:value]}` the values are all 'nil'... '[[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]]'. Is it possible that the Array / Hash - 'Key' and 'Value' are not mapped correctly i.e "key" => "value", ( JSON currently returns "key"=>value, )? – Mark_The_NooB Mar 02 '15 at 05:33
  • Sorry, @MarkTheNooB, I looked at your hash again and realized that they key is a string ("value"), not a symbol (:value). Simply change it to @a.map{|a| a["value"]} and it should work. I edited my example below accordingly. – J Plato Mar 02 '15 at 06:02

1 Answers1

5

You want to pass Gchart.line an array of values extracted from an array of hashes. You can do that like this:

g.data = @a.map{|a| a["value"]}

example:

@a=[{"value" => 1, b:2},{"value" => 3, b:4}]
@a.map{|a| a["value"]}

returns:

[1, 3]

J Plato
  • 888
  • 1
  • 8
  • 17