# This is brain dead, but is it stupid?

**URL:** <https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926>\
**Category:** ruby-talk\
**Created:** [2 June 2006 01:34 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926 "2006-06-02T01:34:59Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jeff\_Pritchard](https://rubytalk.org/u/Jeff_Pritchard)\
**Post date:** [2 June 2006 01:34 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/1 "2006-06-02T01:34:59Z")

</div>

I'm about to embark on my very first significant non-command line ruby  
app. It will be a simple rails app that will parse my log file and show  
me some statistics that I am interested in. Some of that will entail  
some simple graphing.

I spent hours last night looking at some graphing libraries that are out  
there. There are only a few to choose from on rubyforge. I wanted  
something simple, but not toooo simple. I looked at sparklites and  
gruff. One is too fancy and requires waaaaaay to much installation, the  
other a little less installation, but doesn't really cut it for a real  
graph.

It occurred to me that the reason they were requiring rmagic and other  
stuff has to do with needing a way to so some really simple drawing.  
Sort of like using photoshop to paint a mustache on your girlfriends  
picture - it works and inspires the expected knee reflex in the girl,  
but it's total overkill for the job.

So I says to myself, you could make a decent bargraph just by drawing a  
simple little "color blob image" in different sizes. I tried it, and it  
looks just fine to me. I'm thinking about cooking up a simple ruby lib  
that will draw a bargraph with x and y labels on it using this  
technique. Here's a simple manual example:  
[http://www.mylittlecorneroftheinternet.com/BrainDeadBarGraph.html](http://www.mylittlecorneroftheinternet.com/BrainDeadBarGraph.html)

What do you think, this is clearly brain dead, but is it stupid too?

thanks,  
jp

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [2 June 2006 02:23 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/2 "2006-06-02T02:23:25Z")

</div>

Jeff Pritchard wrote:

> So I says to myself, you could make a decent bargraph just by drawing a  
> simple little "color blob image" in different sizes. I tried it, and it  
> looks just fine to me. I'm thinking about cooking up a simple ruby lib  
> that will draw a bargraph with x and y labels on it using this  
> technique. Here's a simple manual example:  
> [http://www.mylittlecorneroftheinternet.com/BrainDeadBarGraph.html](http://www.mylittlecorneroftheinternet.com/BrainDeadBarGraph.html)
> 
> What do you think, this is clearly brain dead, but is it stupid too?

No, it's clearly very handy, and forum polls have been doing it this way  
for a long time. 'Twould be a nice library, I think.

You can easily do horizontal graphs, too, this way.

A possible extra feature would be "caps" that don't get stretched that  
are added to top and bottom or left and right of each bar.

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [2 June 2006 02:25 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/3 "2006-06-02T02:25:28Z")

</div>

Jeff Pritchard wrote:

> So I says to myself, you could make a decent bargraph just by drawing a simple little "color blob image" in different sizes.

Use simple, one-color images and resize them in HTML using height and width.

> **···**
>
> --  
> James Britt
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
> [Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://web2.0validator.com](http://web2.0validator.com) - We're the Dot in Web 2.0

---

<div class="post-metadata">

**Author:** ![Trevor\_Squires](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Trevor\_Squires](https://rubytalk.org/u/Trevor_Squires)\
**Post date:** [2 June 2006 02:40 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/4 "2006-06-02T02:40:17Z")

</div>

\<snip\>

Not sure what your requirements are but have you seen:

[http://nubyonrails.topfunky.com/pages/css\_graphs](http://nubyonrails.topfunky.com/pages/css_graphs)

It's pure html/css graphs - might do what you want.

Also from the same author, (but it requires rmagic iirc):

[http://nubyonrails.topfunky.com/pages/gruff](http://nubyonrails.topfunky.com/pages/gruff)

... all just in case you didn't spot them when you were looking for solutions.

Regards,  
Trevor

> **···**
>
> On 1-Jun-06, at 6:34 PM, Jeff Pritchard wrote:
> 
> > I'm about to embark on my very first significant non-command line ruby  
> > app. It will be a simple rails app that will parse my log file and show  
> > me some statistics that I am interested in. Some of that will entail  
> > some simple graphing.
> 
> --  
> Trevor Squires  
> [http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![Jeff\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jeff\_Pritchard](https://rubytalk.org/u/Jeff_Pritchard)\
**Post date:** [2 June 2006 03:54 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/5 "2006-06-02T03:54:00Z")

</div>

Yes, that's the idea. You said it much better than me though.

jp

James Britt wrote:

> **···**
>
> > Jeff Pritchard wrote:
> > 
> > > So I says to myself, you could make a decent bargraph just by drawing a  
> > > simple little "color blob image" in different sizes.
> > 
> > Use simple, one-color images and resize them in HTML using height and  
> > width.
> > 
> > --  
> > James Britt
> > 
> > [http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
> > [Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
> > [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> > [http://web2.0validator.com](http://web2.0validator.com) - We're the Dot in Web 2.0
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jeff\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jeff\_Pritchard](https://rubytalk.org/u/Jeff_Pritchard)\
**Post date:** [2 June 2006 04:02 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/6 "2006-06-02T04:02:56Z")

</div>

Trevor Squires wrote:

> [http://nubyonrails.topfunky.com/pages/css\_graphs](http://nubyonrails.topfunky.com/pages/css_graphs)
> 
> It's pure html/css graphs - might do what you want.

Trevor, I did miss this one when I was at Geofrey's site. Looks  
interesting. The "one graph per page" thing may be enough to make me go  
ahead and implement a similar rails library using my even simpler  
approach.

Clearly though, I'm the Jeffrey Come Lately to graphing libraries. 🙂

thanks,  
jp

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jeff\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jeff\_Pritchard](https://rubytalk.org/u/Jeff_Pritchard)\
**Post date:** [2 June 2006 07:27 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/7 "2006-06-02T07:27:00Z")

</div>

A modest beginning...

class BDBarGraph  
&nbsp;&nbsp;def initialize(height,width,xmin,xmax)  
&nbsp;&nbsp;&nbsp;&nbsp;@height = height \* 1.0  
&nbsp;&nbsp;&nbsp;&nbsp;@width = width \* 1.0  
&nbsp;&nbsp;&nbsp;&nbsp;@xmin = xmin \* 1.0  
&nbsp;&nbsp;&nbsp;&nbsp;@xmax = xmax \* 1.0  
&nbsp;&nbsp;&nbsp;&nbsp;@dataPoints = [] # the y values to plot  
&nbsp;&nbsp;&nbsp;&nbsp;@htmlString = ""  
&nbsp;&nbsp;&nbsp;&nbsp;@barString = "\<img src=\"blublock.jpg\" width=\"replace\_with\_width\"  
height=\"replace\_with\_height\"\>\n"  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;attr\_reader :xmin, :xmax, :dataPoints

&nbsp;&nbsp;def points  
&nbsp;&nbsp;&nbsp;&nbsp;@dataPoints.length  
&nbsp;&nbsp;end

&nbsp;&nbsp;def add\_point(value)  
&nbsp;&nbsp;&nbsp;&nbsp;@dataPoints \<\< value \* 1.0  
&nbsp;&nbsp;end

&nbsp;&nbsp;def draw  
&nbsp;&nbsp;&nbsp;&nbsp;@dataPoints.each do |thisPoint|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempString = ""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempString \<\< @barString  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempString.gsub!(/replace\_with\_width/,(@width/points).to\_i.to\_s)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;barHeight = ((thisPoint - @xmin)/(@xmax - @xmin)) \* @height  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempString.gsub!(/replace\_with\_height/,barHeight.to\_i.to\_s)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@htmlString \<\< tempString  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;puts @htmlString  
&nbsp;&nbsp;end  
end

bar = BDBarGraph.new(200,200,0,100)  
bar.add\_point(50)  
bar.add\_point(75)  
bar.add\_point(100)  
bar.add\_point(25)  
bar.draw

[cpe-66-75-140-208:~] jeff% ruby BDBarGraph.rb  
\<img src="blublock.jpg" width="50" height="100"\>  
\<img src="blublock.jpg" width="50" height="150"\>  
\<img src="blublock.jpg" width="50" height="200"\>  
\<img src="blublock.jpg" width="50" height="50"\>  
[cpe-66-75-140-208:~] jeff%

Still needs stuff to do x and y labels (probably using a table)  
Still needs error checking  
Maybe some refactoring to make it easier to use from erb

Question, something like this that is intended to be used from erb, how  
is error checking usually handled? Just fail quietly? Seems like  
assertions might not be handled gracefully in erb.

Any coding comments welcome.

thanks,  
jp

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![kate\_rhodes](https://avatars.discourse-cdn.com/v4/letter/k/2bfe46/32.png) [@kate\_rhodes](https://rubytalk.org/u/kate_rhodes)\
**Post date:** [2 June 2006 17:42 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/8 "2006-06-02T17:42:45Z")

</div>

don't forget SVG::Graph  
[http://www.germane-software.com/software/SVG/SVG::Graph/](http://www.germane-software.com/software/SVG/SVG::Graph/)  
(powerful and pretty simple but obviously limited to firefox or ie with a  
plugin. I think there is a bug in the current release so you might want to  
use the svn version instead)

PlotKit [http://www.liquidx.net/plotkit/](http://www.liquidx.net/plotkit/)  
uses MochiKit so be sure Prototype isn't loaded on any page that uses  
PlotKit (prototype and mochikit are incompatable)

If you're doing log stats you might want to check out the flash based  
graphing tool used by MeasureMap  
[http://www.measuremap.com/developer/slider/](http://www.measuremap.com/developer/slider/)  
(fairly easy to hook into rails)

-Kate == Masukomi

---

<div class="post-metadata">

**Author:** ![Elliot\_Temple](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Elliot\_Temple](https://rubytalk.org/u/Elliot_Temple)\
**Post date:** [2 June 2006 18:49 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/9 "2006-06-02T18:49:30Z")

</div>

I don't think the replace technique is very elegant. One thing you can do instead is make a string with %s for holes:

irb(main):001:0\> x = "1 %s 3"  
=\> "1 %s 3"  
irb(main):003:0\> x %2  
=\> "1 2 3"

also instead of escaping lots of double quotes by hand you can using a different way to surround the string. see pickaxe page 320

irb(main):007:0\> %Q-a "b" c-  
=\> "a \"b\" c"  
irb(main):008:0\> %[dog "cat" rat]  
=\> "dog \"cat\" rat"

-- Elliot Temple

> **[Curiosity Blog – Elliot Temple](https://www.curi.us/blog/)**
>
> A philosophy blog

> **···**
>
> On Jun 2, 2006, at 12:27 AM, Jeff Pritchard wrote:
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;@barString = "\<img src=\"blublock.jpg\" width=\"replace\_with\_width\"  
> > height=\"replace\_with\_height\"\>\n"
> 
> > Any coding comments welcome.

---

<div class="post-metadata">

**Author:** ![Dave\_Howell2](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Dave\_Howell2](https://rubytalk.org/u/Dave_Howell2)\
**Post date:** [2 June 2006 21:26 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/10 "2006-06-02T21:26:01Z")

</div>

On Jun 2, 2006, at 0:27, Jeff Pritchard wrote about html-based graphing.

Seems worth doing. I added horizontal graphs using a similar technique many years ago, although the scripting language was Tango 3, not Ruby.

You'll almost certainly want to trap the bars within a table. Otherwise any labels you try to add to either end of a bar run the risk of wrapping in a narrow window.

You can also dispense with the color blob graphic by doing something like this instead:  
&nbsp;&nbsp;\<table width=\"replace\_with\_width\" height=\"replace\_with\_height\ cellspacing=0 cellpadding=0\>  
&nbsp;&nbsp;\<tr\>\<td bgcolor="blue"\>\</td\>\</tr\>\</table\>

There's also an argument for using CSS techniques instead of obsolete HTML, as well.

On the other hand, if you do use an image file, there's no point in a boring old monocolor image. For the graphs I did, I created a one-pixel wide image that was, say, 7 pixels tall, and the pixels were something like this  
&nbsp;&nbsp;medium red  
&nbsp;&nbsp;light red  
&nbsp;&nbsp;medium red  
&nbsp;&nbsp;dark red

When stretched out horizontally, it looks cylindrical, with the light red becoming a 'highlight,' much like the roundness seen in OSX widgets and whatnot.

So as you expand and enhance your bar graphing class, consider adding optional .image and .styleclass methods. I could choose to  
&nbsp;&nbsp;graph.image = "/images/bluebar.gif"  
but if I don't, then the bar graph uses tables (or some more modern CSS equivalent).  
&nbsp;&nbsp;graph.barcolor  
might set the bar color to something other than the default, although .image would override that anyway.  
&nbsp;&nbsp;graph.styleclass = "vertbargraph"  
could change the outer enclosing table from  
&nbsp;&nbsp;\<table ...\>  
to  
&nbsp;&nbsp;\<table class="vertbargraph" ...\>  
so that I could define many of my graph characteristics in my stylesheet.

I notice that the "Graphs Helper" for Rails that somebody else pointed out not only is limited to one graph per page (how odd) but also a range of 0-100. I like the fact you included min and max graph range values in your code. My old graphing code also handled over-range values with a special graphic that matched the regular bar, but had an "s" shaped slice cut out of it. If a value was out of range, it would draw a regular bar about 3/4 of the way, stick in the sliced graphic, then finish the bar. Another way I could have done it was to have a triangular end-cap to create an arrow indicating a value off the chart. I can't think of an easy way offhand to do that just with colored table blocks, but there's probably a way with CSS code. Anyway, don't forget to handle out of bounds data values, and/or possibly an auto-scaling option.

Another thing the "Graphs Helper" shows off is value labels overlapping the bars, and multiple bar colors per graph. If/when I next put graphs on a web page, I'll put my labels next to/below the bars, and the bars are almost certainly all going to be the same color. Even Helper's first sample graph shows some problems with getting too fancy. "Apple" didn't get to have a value label, probably because the bar's too short, and the white writing on the yellow bar isn't very legible. Fill the niche for a nice \*simple\* bar graph tool. 🙂

Finally, "draw" seems a bit odd as a rendering method to me. What I expect I'd want back is the HTML, which I can then embed with other stuff in my page. It won't get "drawn" as such until the browser gets it.  
&nbsp;&nbsp;graph.htmlsource  
maybe? Or something more like that?

---

<div class="post-metadata">

**Author:** ![Jeff\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jeff\_Pritchard](https://rubytalk.org/u/Jeff_Pritchard)\
**Post date:** [3 June 2006 01:20 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/11 "2006-06-03T01:20:48Z")

</div>

Thanks for all the great suggestions so far.

I like the idea of an option to skip the colorBlob.jpg file and just use  
tables with cell background colors. Also agree with the concern over  
making it into a psychedelic 70's Acid graphing trip. I hate page  
design that looks like something designed by Liberace. To each their  
own...but that's not me.

I was really hoping to get an answer to my question about the best way  
to report errors in something that will be supplying html into an erb  
document.

My goal is to make this library just barely complete enough to be useful  
for real bar graphs, but keep it simple enough that using it is a ten  
minute exercise rather than adding a bunch of installation and learning  
curve to a project. Geoffrey and others have already cornered the  
market on the heavy artillery. Geoffrey also covered the extreme low  
end where you're primarily after more of a customized graphics clip art,  
with sparklines. I want to fill the niche between sparklines and gruff.  
And with nothing more than including my library.rb file for installation  
(and an optional color blob file).

I'm finding it a bit surprising that anybody is interested in horizontal  
bar graphs. I've always found them a bit off-putting. Like I'm  
supposed to lay my head on the table while I look at them. Will it be  
unforgivably incomplete without support for horizontal bars?

Some other thoughts:  
\* I'm thinking there are probably some other more convenient ways of  
adding points  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- maybe add an array parameter as an optional last parameter to the  
constructor  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- maybe provide a method for supplying an array of points  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- maybe optionally read a file full of CSV points  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- if it were a rails app with sql, what type of input method would  
be most convenient for sticking the results of an sql query right into a  
graph?  
\* I want to make labeling pretty simple. I'm thinking just set it up  
such that there are ten evenly spaced "slots" for placing a custom 'y'  
label. If none are supplied it would fill these ten spots in with  
calculated values based on the min and max.  
\* I'm thinking an optional method that allows you to supply an array of  
'x' labels - one per bar. If nothing is supplied, nothing will be  
"drawn"  
\* I'm thinking that "titling" of the graph can be done outside of the  
graph and not handled by the library  
\* In 'erb' land, is there any advantage to making it a "one liner",  
where the constructor is big and hairy and allows all possible  
parameters to be filled in, and the constructor automatically provides  
the html output?

thanks,  
jp

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeff\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jeff\_Pritchard](https://rubytalk.org/u/Jeff_Pritchard)\
**Post date:** [3 June 2006 01:42 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/12 "2006-06-03T01:42:31Z")

</div>

Elliot Temple wrote:

> I don't think the replace technique is very elegant. One thing you  
> can do instead is make a string with %s for holes:
> 
> irb(main):001:0\> x = "1 %s 3"  
> =\> "1 %s 3"  
> irb(main):003:0\> x %2  
> =\> "1 2 3"
> 
> -- Elliot Temple  
> [Curiosity Blog – Elliot Temple](http://www.curi.us/blog/)

So my multiline gsub tempstring "\<img src=\"blublock.jpg\"  
width=\"replace\_with\_width\" height=\"replace\_with\_height\"\>\n"  
nastiness would become:

@barString = %Q(\<img src="blublock.jpg" width="%d" height="%d"\>\n)  
@htmlString \<\< @barString % [barWidth,barHeight]

yes?

Very cool language.

thanks,  
jp

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Patrick\_Hurley1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Patrick\_Hurley1](https://rubytalk.org/u/Patrick_Hurley1)\
**Post date:** [4 June 2006 14:12 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/13 "2006-06-04T14:12:15Z")

</div>

Or you could just:

@htmlString = %Q(\<img src="blublock.jpg" width="#{barWidth}"  
height="#{barHeight}"\>\n)

pth

> **···**
>
> On 6/2/06, Jeff Pritchard \<jp@jeffpritchard.com\> wrote:
> 
> > Elliot Temple wrote:  
> > \> I don't think the replace technique is very elegant. One thing you  
> > \> can do instead is make a string with %s for holes:  
> > \>  
> > \> irb(main):001:0\> x = "1 %s 3"  
> > \> =\> "1 %s 3"  
> > \> irb(main):003:0\> x %2  
> > \> =\> "1 2 3"  
> > \>  
> > \> -- Elliot Temple  
> > \> [Curiosity Blog – Elliot Temple](http://www.curi.us/blog/)
> > 
> > So my multiline gsub tempstring "\<img src=\"blublock.jpg\"  
> > width=\"replace\_with\_width\" height=\"replace\_with\_height\"\>\n"  
> > nastiness would become:
> > 
> > @barString = %Q(\<img src="blublock.jpg" width="%d" height="%d"\>\n)  
> > @htmlString \<\< @barString % [barWidth,barHeight]
> > 
> > yes?
> > 
> > Very cool language.
> > 
> > thanks,  
> > jp
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![kate\_rhodes](https://avatars.discourse-cdn.com/v4/letter/k/2bfe46/32.png) [@kate\_rhodes](https://rubytalk.org/u/kate_rhodes)\
**Post date:** [5 June 2006 20:43 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/14 "2006-06-05T20:43:18Z")

</div>

Simple example of why horizontal graphs are important:  
Imaging you have 100+ records each of which gets a bar.  
With a vertical bar graph you run off the side of page (even without  
labels). How are you supposed to do it with a label on each bar?  
Horizontal graph allows you to put label + bar on one line, and while  
the full graph may scroll off bottom of page people don't mind because  
we're used to things going below the fold. Horizontal graphs also  
allow you to have more words in the label of each bar because they  
don't affect the length of the graph.

- kate = masukomi

> **···**
>
> On 6/2/06, Jeff Pritchard \<jp@jeffpritchard.com\> wrote:
> 
> > I'm finding it a bit surprising that anybody is interested in horizontal  
> > bar graphs. I've always found them a bit off-putting. Like I'm  
> > supposed to lay my head on the table while I look at them. Will it be  
> > unforgivably incomplete without support for horizontal bars?

---

<div class="post-metadata">

**Author:** ![Dave\_Howell2](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Dave\_Howell2](https://rubytalk.org/u/Dave_Howell2)\
**Post date:** [12 June 2006 20:32 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/15 "2006-06-12T20:32:58Z")

</div>

> I'm finding it a bit surprising that anybody is interested in horizontal  
> bar graphs. I've always found them a bit off-putting. Like I'm  
> supposed to lay my head on the table while I look at them. Will it be  
> unforgivably incomplete without support for horizontal bars?

I'd be perplexed, to be sure. As somebody else pointed out, because our language lays out in horizontal boxes, you can just naturally get a lot more information into a space if the labels are running the same direction as the bars.

> Some other thoughts:  
> \* I'm thinking there are probably some other more convenient ways of  
> adding points  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- maybe add an array parameter as an optional last parameter to the  
> constructor  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- maybe provide a method for supplying an array of points  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- maybe optionally read a file full of CSV points  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- if it were a rails app with sql, what type of input method would  
> be most convenient for sticking the results of an sql query right into a  
> graph?

I sort of expect that a Graph would expect an array of GraphItems. GraphItem would be something like  
&nbsp;&nbsp;:value (required, numeric)  
&nbsp;&nbsp;:label (default nil)  
&nbsp;&nbsp;:barcolor (default to nil, aka Graph.barcolor)  
&nbsp;&nbsp;:barimage (default to nil, aka Graph.barimage)

I could pass in an array like  
&nbsp;&nbsp;[3, 65, 43, 1, 0, 0, 5]  
or I could send in  
&nbsp;&nbsp;[[4, "PCs"], [18, "Macs"], [9, "Other"]]  
or I could do  
&nbsp;&nbsp;[:rutabagas =\> 30, :pomegranates=\>54, :kiwifruit =\> 4325]  
or I could do  
&nbsp;&nbsp;[ \<#GraphItem:@label="Spoon!", @value=43, @barcolor="FF0000", @barimage=nil\>, \<#GraphItem:@label="Fork", @value=12, @barcolor="777777", @barimage=nil\>, \<#GraphItem:@label="Knife", @value=14, @barcolor="777777", @barimage=nil\>

> \* I want to make labeling pretty simple. I'm thinking just set it up  
> such that there are ten evenly spaced "slots" for placing a custom 'y'  
> label. If none are supplied it would fill these ten spots in with  
> calculated values based on the min and max.

I think you'll find value-axis labels to be a bit of a problem. If they're just numbers on a vertical graph, they'll usually work OK. Big numbers on a horiz. graph will be prone to distortion due to label length. Vertically, it may not be very clear where the 'tick' mark would be for that number. Would you try to span each label across two rows? And what if my values are "3" "4" and "5"? Do I just get unevenly spaced labels, or a graph with some dead space at some point?

> \* I'm thinking an optional method that allows you to supply an array of  
> 'x' labels - one per bar. If nothing is supplied, nothing will be  
> "drawn"

Are these bar labels or value-axis labels? I'm not sure which one you think is "x", although since you seem to think in terms of vertical graphs, I guess they're bar labels.

I'd want to supply bar labels at the same time that I provided bar values, not have to break them out and load them separately, which seems kind of weird . . . but maybe that's just me.

> \* I'm thinking that "titling" of the graph can be done outside of the  
> graph and not handled by the library  
> \* In 'erb' land, is there any advantage to making it a "one liner",  
> where the constructor is big and hairy and allows all possible  
> parameters to be filled in, and the constructor automatically provides  
> the html output?

That wouldn't be hard if Graph.new accepts an array, no?

&nbsp;&nbsp;print Graph.new(datapoints).html

I can see people wanting to also pass in .min, .max, .bar\_color, and the rest, although personally I'd probably prefer being able to set defaults for the class.

Graph.default\_bar\_image = "/images/bargraph.gif"  
Graph.default\_bar\_color = htmlColors[:yellow]

mmmm. Default symbolic colors....

> **···**
>
> On Jun 2, 2006, at 18:20, Jeff Pritchard wrote:

---

<div class="post-metadata">

**Author:** ![Jeff\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jeff\_Pritchard](https://rubytalk.org/u/Jeff_Pritchard)\
**Post date:** [4 June 2006 15:01 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/16 "2006-06-04T15:01:48Z")

</div>

True enough Patrick, but that doesn't use any new-to-me coolish aspects  
of the language. 🙂

I'm a bit disappointed nobody bothered to respond to my library design  
questions. I'll risk bad forum etiquette by reposting them here:

Some other thoughts:  
\* What's the best way to report parameter errors in a library intended  
for erb use?  
\* I'm thinking there are probably some other more convenient ways of  
adding points  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- maybe add an array parameter as an optional last parameter to the  
constructor  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- maybe provide a method for supplying an array of points  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- maybe optionally read a file full of CSV points  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- if it were a rails app with sql, what type of input method would  
be most convenient for sticking the results of an sql query right into a  
graph?  
\* I want to make labeling pretty simple. I'm thinking just set it up  
such that there are ten evenly spaced "slots" for placing a custom 'y'  
label. If none are supplied it would fill these ten spots in with  
calculated values based on the min and max.  
\* I'm thinking an optional method that allows you to supply an array of  
'x' labels - one per bar. If nothing is supplied, nothing will be  
"drawn"  
\* I'm thinking that "titling" of the graph can be done outside of the  
graph and not handled by the library  
\* In 'erb' land, is there any advantage to making it a "one liner",  
where the constructor is big and hairy and allows all possible  
parameters to be filled in, and the constructor automatically provides  
the html output?

thanks,  
jp

Patrick Hurley wrote:

> **···**
>
> > On 6/2/06, Jeff Pritchard \<jp@jeffpritchard.com\> wrote:
> > 
> > > \> [Curiosity Blog – Elliot Temple](http://www.curi.us/blog/)  
> > > Very cool language.
> > > 
> > > thanks,  
> > > jp
> > > 
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > Or you could just:
> > 
> > @htmlString = %Q(\<img src="blublock.jpg" width="#{barWidth}"  
> > height="#{barHeight}"\>\n)
> > 
> > pth
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jacob\_Fugal](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jacob\_Fugal](https://rubytalk.org/u/Jacob_Fugal)\
**Post date:** [5 June 2006 15:58 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/17 "2006-06-05T15:58:41Z")

</div>

That doesn't benefit from the template string only needing to be built  
once. Compare these:

&nbsp;&nbsp;template = 'T minus %d...'  
&nbsp;&nbsp;15.downto(1) do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;puts(template % i)  
&nbsp;&nbsp;end

vs.

&nbsp;&nbsp;15.downto(1) do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;puts "T minus #{i}"  
&nbsp;&nbsp;end

I'll admit, I use the latter as well when I'm not concerned with the  
performance in that loop. It does look nicer and more compact. But I'm  
guessing[0] that the former will be significantly faster when 1) the  
number of iterations in the loop is high and 2) the template is  
moderately complex. Why? I assume that String#% is implemented in C  
using sprintf (or some cousin). So the performance of template % i is  
almost entirely related to one very well used and thus well optimized  
libc library call. "T minus #{i}" on the other hand involves parsing  
of the string to determine interpolation etc. This is probably also  
written in C, but involves more work than the quick sprintf call.

Jacob Fugal

[0] Yes, I'm guessing. I haven't actually benchmarked this or looked  
at the ruby sources, so I may be wrong.

> **···**
>
> On 6/4/06, Patrick Hurley \<phurley@gmail.com\> wrote:
> 
> > \> So my multiline gsub tempstring "\<img src=\"blublock.jpg\"  
> > \> width=\"replace\_with\_width\" height=\"replace\_with\_height\"\>\n"  
> > \> nastiness would become:  
> > \>  
> > \> @barString = %Q(\<img src="blublock.jpg" width="%d" height="%d"\>\n)  
> > \> @htmlString \<\< @barString % [barWidth,barHeight]
> > 
> > Or you could just:
> > 
> > @htmlString = %Q(\<img src="blublock.jpg" width="#{barWidth}"  
> > height="#{barHeight}"\>\n)

---

<div class="post-metadata">

**Author:** ![Jeff\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jeff\_Pritchard](https://rubytalk.org/u/Jeff_Pritchard)\
**Post date:** [6 June 2006 01:14 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/18 "2006-06-06T01:14:49Z")

</div>

Thanks Kate,  
You make a good point. Right now I'm working towards a SimpleGraphs  
v0.001 that satisfies my needs for the app that brought it into being.  
Perhaps 0.002 will include horizontal graphs as well.

Still hoping for info on error reporting in an 'erb' library.

best,  
jp

kate rhodes wrote:

> **···**
>
> > On 6/2/06, Jeff Pritchard \<jp@jeffpritchard.com\> wrote:
> > 
> > > I'm finding it a bit surprising that anybody is interested in horizontal  
> > > bar graphs. I've always found them a bit off-putting. Like I'm  
> > > supposed to lay my head on the table while I look at them. Will it be  
> > > unforgivably incomplete without support for horizontal bars?
> > 
> > Simple example of why horizontal graphs are important:  
> > Imaging you have 100+ records each of which gets a bar.  
> > With a vertical bar graph you run off the side of page (even without  
> > labels). How are you supposed to do it with a label on each bar?  
> > Horizontal graph allows you to put label + bar on one line, and while  
> > the full graph may scroll off bottom of page people don't mind because  
> > we're used to things going below the fold. Horizontal graphs also  
> > allow you to have more words in the label of each bar because they  
> > don't affect the length of the graph.
> > 
> > - kate = masukomi
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Patrick\_Hurley1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Patrick\_Hurley1](https://rubytalk.org/u/Patrick_Hurley1)\
**Post date:** [12 June 2006 21:52 UTC](https://rubytalk.org/t/this-is-brain-dead-but-is-it-stupid/27926/19 "2006-06-12T21:52:45Z")

</div>

Just throw an exception on errors -- let the user/framework deal with  
exceptions. They should only be generated on "true" errors.  
Personally, I would let questionable data through, ignoring it -- this  
often simplifies graphing where some of the data can be rough.

pth

> **···**
>
> On 6/5/06, Jeff Pritchard \<jp@jeffpritchard.com\> wrote:
> 
> > Thanks Kate,  
> > You make a good point. Right now I'm working towards a SimpleGraphs  
> > v0.001 that satisfies my needs for the app that brought it into being.  
> > Perhaps 0.002 will include horizontal graphs as well.
> > 
> > Still hoping for info on error reporting in an 'erb' library.
> > 
> > best,  
> > jp
> > 
> > kate rhodes wrote:  
> > \> On 6/2/06, Jeff Pritchard \<jp@jeffpritchard.com\> wrote:  
> > \>\>  
> > \>\> I'm finding it a bit surprising that anybody is interested in horizontal  
> > \>\> bar graphs. I've always found them a bit off-putting. Like I'm  
> > \>\> supposed to lay my head on the table while I look at them. Will it be  
> > \>\> unforgivably incomplete without support for horizontal bars?  
> > \>  
> > \> Simple example of why horizontal graphs are important:  
> > \> Imaging you have 100+ records each of which gets a bar.  
> > \> With a vertical bar graph you run off the side of page (even without  
> > \> labels). How are you supposed to do it with a label on each bar?  
> > \> Horizontal graph allows you to put label + bar on one line, and while  
> > \> the full graph may scroll off bottom of page people don't mind because  
> > \> we're used to things going below the fold. Horizontal graphs also  
> > \> allow you to have more words in the label of each bar because they  
> > \> don't affect the length of the graph.  
> > \>  
> > \> - kate = masukomi
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
