# How to break this down for use in a graph?

**URL:** <https://www.ruby-forum.com/t/how-to-break-this-down-for-use-in-a-graph/167367>\
**Category:** Ruby\
**Created:** [May 7, 2009, 3:15am UTC](https://www.ruby-forum.com/t/how-to-break-this-down-for-use-in-a-graph/167367 "2009-05-07T03:15:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [May 7, 2009, 3:15am UTC](https://www.ruby-forum.com/t/how-to-break-this-down-for-use-in-a-graph/167367/1 "2009-05-07T03:15:53Z")

</div>

Hi,

I have been trying for many hours to figure out how to do this, and have  
yet to come up with a solution. It’s my lack of experience I’m sure and  
I hope someone can point me in the right direction.

I have an ActiveRecord resultset that contains rows from a query with  
group by clauses. I’m trying to generate a stacked column graph that  
will show the number of orders for a period of time stacked by the rep  
that generated the orders broken down by date.

The AR result set has the following attributes:

sale\_date (date the sale was made - for a single date, there will be a  
row for each each rep that had a recorded sale on that date)  
sales\_rep (the rep - the name of the rep that made the sale on the sale  
date)  
order\_count (the count of orders made by the above rep for the sale  
date)

The graph expects an array of dates for the axis. I did:

sale\_days = @results.map{ |item | item.sales\_day }.uniq

to get a unique set of dates (as they repeat for each rep, but I only  
want the unique dates).

I don’t have a clue how to get something that allows me to do this:

sales\_graph.add( :series, “Sales Rep Name”, [a, b, c, d, e] )

where the sales rep name is found in my result set, and the a,b,c… is  
the actual count of orders for that rep for each day. I need to do the  
sales\_graph.add… for each rep found in the resultset. I can handle  
that part, if I can figure out how to get the counts for a rep into it’s  
own array/hash and tied back to the rep.

Please help! ☹ If I need to structure my query differently, I can.  
Ruby is so beautiful and I know I am missing some easy way to do this  
and for the life of me I can’t figure it out on my own.

Thanks for any pointers in advance,

M.

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [May 7, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/how-to-break-this-down-for-use-in-a-graph/167367/2 "2009-05-07T13:18:31Z")

</div>

> Please help! ☹ If I need to structure my query differently, I can.  
> Ruby is so beautiful and I know I am missing some easy way to do this  
> and for the life of me I can’t figure it out on my own.

It would help if you show your model or at least ‘p @results’. Then  
show what you want as output. For me at least, code will be easier to  
decipher than what you mean by things like “tied back to the rep”.

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [May 7, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/how-to-break-this-down-for-use-in-a-graph/167367/3 "2009-05-07T16:00:53Z")

</div>

Mark T. wrote:

> > Please help! ï¿½:-( ï¿½If I need to structure my query differently, I can.  
> > Ruby is so beautiful and I know I am missing some easy way to do this  
> > and for the life of me I can’t figure it out on my own.
> 
> It would help if you show your model or at least ‘p @results’. Then  
> show what you want as output. For me at least, code will be easier to  
> decipher than what you mean by things like “tied back to the rep”.

Sorry about that. Here’s a sample of the results after calling  
.inspect. Should help clarify the structure of my resultset.

#\<Client:0x4920884 @attributes={“order\_count”=\>“4”,  
“sales\_day”=\>“2009-04-13”, “sales\_rep”=\>“Michael”}\>,  
#\<Client:0x4920848 @attributes={“order\_count”=\>“2”,  
“sales\_day”=\>“2009-04-13”, “sales\_rep”=\>“John”}\>,  
#\<Client:0x492080c @attributes={“order\_count”=\>“10”,  
“sales\_day”=\>“2009-04-13”, “sales\_rep”=\>“Jane”}\>,  
#\<Client:0x49204c4 @attributes={“order\_count”=\>“1”,  
“sales\_day”=\>“2009-04-14”, “sales\_rep”=\>“Michael”}\>,  
#\<Client:0x4920488 @attributes={“order\_count”=\>“9”,  
“sales\_day”=\>“2009-04-14”, “sales\_rep”=\>“John”}\>,  
#\<Client:0x4920438 @attributes={“order\_count”=\>“7”,  
“sales\_day”=\>“2009-04-14”, “sales\_rep”=\>“David”}\>,  
#\<Client:0x49203e8 @attributes={“order\_count”=\>“2”,  
“sales\_day”=\>“2009-04-14”, “sales\_rep”=\>“Albert”}\>]"

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [May 7, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/how-to-break-this-down-for-use-in-a-graph/167367/4 "2009-05-07T16:52:59Z")

</div>

> #\<Client:0x4920884 @attributes={“order\_count”=\>“4”,  
> “sales\_day”=\>“2009-04-13”, “sales\_rep”=\>“Michael”}\>,  
> #\<Client:0x4920848 @attributes={“order\_count”=\>“2”,  
> “sales\_day”=\>“2009-04-13”, “sales\_rep”=\>“John”}\>,  
> #\<Client:0x492080c @attributes={“order\_count”=\>“10”,  
> “sales\_day”=\>“2009-04-13”, “sales\_rep”=\>“Jane”}\>,  
> #\<Client:0x49204c4 @attributes={“order\_count”=\>“1”,  
> “sales\_day”=\>“2009-04-14”, “sales\_rep”=\>“Michael”}\>,  
> #\<Client:0x4920488 @attributes={“order\_count”=\>“9”,  
> “sales\_day”=\>“2009-04-14”, “sales\_rep”=\>“John”}\>,  
> #\<Client:0x4920438 @attributes={“order\_count”=\>“7”,  
> “sales\_day”=\>“2009-04-14”, “sales\_rep”=\>“David”}\>,  
> #\<Client:0x49203e8 @attributes={“order\_count”=\>“2”,  
> “sales\_day”=\>“2009-04-14”, “sales\_rep”=\>“Albert”}\>]"

I forgot to put what I wanted for output. With the results above as a  
sample, I want:

sales\_dates = [2009-04-13, 2009-04-14]

Then…

Michael, [4, 1]  
John, [2, 9]  
Jane, [10, 0]  
David, [0, 7]  
Albert, [0, 2]

or whatever it would take for me to iterate over an array/hash of  
sales\_reps and call something like the following:

for rep in sales\_reps  
sales\_graph.add( :series, rep, [a, b] )  
end

so that I would end up with:  
sales\_graph.add( :series, “Michael”, [4, 1] )  
sales\_graph.add( :series, “John”, [2, 9] )  
sales\_graph.add( :series, “Jane”, [10, 0] )  
sales\_graph.add( :series, “David”, [0, 7] )  
sales\_graph.add( :series, “Albert”, [0, 2] )

I hope this helps. Again, any advice is greatly appreciated.

Regards,

Michael

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [May 8, 2009, 12:39am UTC](https://www.ruby-forum.com/t/how-to-break-this-down-for-use-in-a-graph/167367/5 "2009-05-08T00:39:14Z")

</div>

On May 7, 2009, at 10:53 AM, Michael Modic wrote:

> > #\<Client:0x4920438 @attributes={"order\_count"=\>"7",  
> > "sales\_day"=\>"2009-04-14", "sales\_rep"=\>"David"}\>,  
> > #\<Client:0x49203e8 @attributes={"order\_count"=\>"2",  
> > "sales\_day"=\>"2009-04-14", "sales\_rep"=\>"Albert"}\>]"
> 
> I forgot to put what I wanted for output. With the results above as a  
> sample, I want:

OK, so I’ll start with @results having AR model instances like the  
above.

# First, let’s make it into a simpler data structure:

# a hash with keys of the sales\_rep with values as an array of

# [sales\_day, order\_count] pairs.

sales\_data = Hash.new {|h,k| h[k] = []}  
@results.each do |client|  
sales\_data[client.sales\_rep] \<\< [client.sales\_day,  
client.order\_count]  
end

> sales\_dates = [2009-04-13, 2009-04-14]

# get the unique, sorted set of dates

sales\_dates = sales\_data.values.map{|pairs|pairs.map{|pair|  
pair[0]}}.flatten.uniq.sort

# Perhaps you want to eliminate “holes” in the array of sales\_dates

# Make sure each sales\_rep has the same set of entries

sales\_data.keys.each do |rep|  
sales\_data[rep] = sales\_dates.map do |d|  
if pair = sales\_data[rep].assoc(d)  
pair[1].to\_i  
else  
0  
end  
end  
end

> Then…
> 
> Michael, [4, 1]  
> John, [2, 9]  
> Jane, [10, 0]  
> David, [0, 7]  
> Albert, [0, 2]

irb\> pp sales\_data  
{“Michael”=\>[4, 1],  
“David”=\>[0, 7],  
“John”=\>[2, 9],  
“Albert”=\>[0, 2],  
“Jane”=\>[10, 0]}  
=\> nil

> sales\_graph.add( :series, “Michael”, [4, 1] )
> 
> –

sales\_data.each do |rep,sales|  
sales\_graph.add(:series, rep, sales)  
end

Assuming that you have sales\_graph defined somewhere up front.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [May 8, 2009, 4:59am UTC](https://www.ruby-forum.com/t/how-to-break-this-down-for-use-in-a-graph/167367/6 "2009-05-08T04:59:20Z")

</div>

Rob - thank you so much!!! I’ll email you privately to make a small  
contribution for your assistance. It seems so easy now after seeing the  
solution, but I would not have got it on my own.

Regards,

Michael
