views:

599

answers:

1

Hi All,

I'm having a bit of an issue wrapping my head around something. I'm currently using a hacked version of Gruff in order to accommodate "Scatter Plots". That said, the data is entered in the form of:

 g.data("Person1",[12,32,34,55,23],[323,43,23,43,22])

...where the first item is the ENTITY, the second item is X-COORDs, and the third item is Y-COORDs.

I currently have a recordset of items from a table with the columns: POINT, VALUE, TIMESTAMP. Due to the "complex" calculations involved I must grab everything using a single query or risk way too much DB activity. That said, I have a list of items for which I need to dynamically collect all data from the recordset into a hash (or array of arrays) for the creation of the data items. I was thinking something like the following:

 @h={}

 e = Events.find_by_sql(my_query)
 e.each do |event|
      @h["#{event.Point}"][x] = event.timestamp
      @h["#{event.Point}"][y] = event.value
 end

Obviously that's not the correct syntax, but that's where my brain is going. Could someone clean this up for me or suggest a more appropriate mechanism by which to accomplish this? Basically the main goal is to keep data for each pointname grouped (but remember the recordset has them all).

Much appreciated.

EDIT 1

g = Gruff::Scatter.new("600x350")
g.title = self.name

e = Event.find_by_sql(@sql)

h ={}

e.each do |event|
  h[event.Point.to_s] ||= {}
  h[event.Point.to_s].merge!({event.Timestamp.to_i,event.Value})
end

h.each do |p|

  logger.info p[1].values.inspect

  g.data(p[0],p[1].keys,p[1].values)
end

g.write(@chart_file)
A: 

First off, don't use string interpolation when a simple o.to_s will do.

@h[event.Point.to_s]

The structure of your data is not clear from the description. I think you mean that there 1) there are events with Point names 2) each event has a time and a value 3) you want to manipulate a collection of events.

A hash is a fine choice for the problem.

mcandre
Good call in the interpolation. That said, I'm still not sure as to how to structure the hash. I'll need both X and Y forks for each, correct? Or would it be a hash of a hash? Would you mind providing an example? My main issue is that all points are in the returned recordset and I need an extra level of "awareness" in order to determine what POINTNAME I'm currently using -- unless, of course hashes support some sort of "push" mechanism, in which I case I could simply reference it by name and throw on two more values.
humble_coder
You need to figure out how you want to represent and organize the data. The code is secondary. If you want to manipulate, but not store, the points in a hash (keyed by event name) that's fine. It's okay to use another hash (keys: "x", "y") or a list (0th = x, 1th = y) which refers to the time and value.`@[event.Point.to_s]=[event.timestamp, event.value]`
mcandre
Please see my "Edit 1". That is what I have as of this comment. I keep receiving errors regarding conversions from Arrays to Strings and vice versa depending on how I format the data. I'm not sure why I can't simply plug in the array values that way. Perhaps it's returning one of the arrays as a string?
humble_coder
Paste the error messages. Which lines cause errors?
mcandre