# \[ANN\] SimpleGraphs 0.00000001

**URL:** <https://www.ruby-forum.com/t/ann-simplegraphs-0-00000001/225911>\
**Category:** Ruby\
**Created:** [June 8, 2006, 6:50am UTC](https://www.ruby-forum.com/t/ann-simplegraphs-0-00000001/225911 "2006-06-08T06:50:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [June 8, 2006, 6:50am UTC](https://www.ruby-forum.com/t/ann-simplegraphs-0-00000001/225911/1 "2006-06-08T06:50:27Z")

</div>

This is a follow up to a recent thread about “BrainDeadBarGraphs”.

I was out to find a really simple to install and use bar graph library  
and couldn’t find anything that was “just right”. There were some that  
were too simple and some that were not simple enough. For me, this  
library is the Goldilocks of bar graph libraries. Perhaps somebody else  
will find it useful.

Here’s a test page for it:  
[http://www.mylittlecorneroftheinternet.com/test.rhtml](http://www.mylittlecorneroftheinternet.com/test.rhtml)

This is all the erb needed to generate that:

% require “SimpleGraphs.rb”  
% bar = SimpleGraphs.new(200,200,0,100,nil)  
% bar.add\_points([50, 75, 100, 25, 0])  
% bar.add\_y\_labels(%w{100 75 50 25 0})  
% bar.add\_x\_labels(%w{one two three four})  
% bar.make\_html\_output

I have some questions I need to clear up before it is worthy of a real  
version number and perhaps posting at ruby forge.

1. how do you deal with paths in a require statement in eruby land? In  
order to get it to find my library file with the obvious: require  
“SimpleGraphs.rb” I had to put the file in the cgi-bin directory  
where eruby is installed. I would like to be able to put it with the  
rhtml file that requires it. Would I have to use a non-relative path  
(i.e. full URL)? I tried require “./SimpleGraphs.rb” but that didn’t  
work.

2. nobody has ever answered my question about what’s expected in the way  
of error checking and reporting (such as bad parameters, missing  
parameters, etc.) in a library that will be used with eruby. Don’t know  
what is the best method for reporting the errors. Asserts? puts to  
make a message show up in the page? Something else?

3. should I put the big html strings in a separate file from the code?

4. since I’m such a newbie, I figure you guys should “peer” review my  
code before I have the unmitigated gaul and pretense to post it on ruby  
forge. Here’s the contents of the SimpleGraphs.rb file…any and all  
comments welcome - especially constructive ones. 🙂

Please ignore the strange formatting that occurs when you copy and paste  
code into a forum.  
Oh, and yes, I know; a real man would have used CSS, but I used what I  
know. My ‘Eric Meyer on CSS’ book is still in pristine condition.  
Gotta actually read that one of these days.

thanks,  
jp

class SimpleGraphs  
def initialize(height,width,xmin,xmax,colorFile)  
@height = (height \* 1.0) - 20.0  
@width = width \* 1.0  
@xmin = xmin \* 1.0  
@xmax = xmax \* 1.0  
@dataPoints = [] # the y values to plot  
@colorFile = colorFile  
@yLabels = []  
@xLabels = []  
end

```
attr_reader :xmin, :xmax, :dataPoints

def points
	@dataPoints.length
end

def add_points(values)
	values.each {|value| @dataPoints << value * 1.0}
end

def add_y_labels(labels)
  labels.each { |label| @yLabels << label}
end

def add_x_labels(labels)
  labels.each { |label| @xLabels << label}
end

def bar_width
  (@width/points.to_f).to_i.to_s
end

def bar_height(value)
  (((value - @xmin)/(@xmax - @xmin)) * @height).to_i.to_s
end

def bar_from_file(value)
  barString = ""
  barFileString = "\t\t\t\t\t\t<td><img src=\"%s\" width=\"%s\" 

```

height="%s"\>\n"  
barString \<\< barFileString % [@colorFile,bar\_width,bar\_height(value)]  
barString  
end

```
def bar_from_table(value)
  barString = ""
  barTableString = "\t\t\t\t\t\t<td><table width=\"%s\" height=\"%s\" 

```

cellspacing=0 cellpadding=0\>\<td  
bgcolor="blue"\>\n"  
barString \<\< barTableString % [bar\_width,bar\_height(value)]  
barString  
end

```
def make_bars
  bars = ""
  @dataPoints.each do |thisPoint|
  if @barColorFile != nil
    bars << bar_from_file(thisPoint)
  else
    bars << bar_from_table(thisPoint)
  end
end
bars

```

end

```
def make_y_label_lines
  outputString = ""
  oneLine = %Q{ \t\t\t\t\t<tr ><td align="right" 

```

valign=“%s”\>%s \n }  
@yLabels.each\_with\_index do |label,index|  
vertical = case index  
when 0: “top”  
when removed\_email\_address@domain.invalid-2: “middle”  
when @yLabels.length-1: “bottom”  
end  
outputString \<\< oneLine % [vertical, label]  
end  
outputString  
end

```
def make_x_label_lines
  oneLabel = %Q{\t\t\t\t\t\t<td><table width="%s" cellspacing=0 

```

cellpadding=0\>%s \n}  
xlabelout = “”  
@xLabels.each do |label|  
xlabelout \<\< oneLabel % [bar\_width,label]  
end  
xlabelout  
end

```
def make_html_output
  htmlOutputString = ""
  bigTableString = %Q(
  <table id="y labels and bars" cellspacing=0 cellpadding=0 border=0 >
	<! this height is the graph height minus the x label height>
	<tr height="%s">
		<td id="y labels" align="right" valign="bottom">
			<table height="%s" cellspacing=0 cellpadding=0 align="right" 

```

valign=“bottom” border=0 \>  
\<! tr td small label for each ylabel requested - valign top  
middle and bottom \>  
%s

  
  
\<! td table /td for each bar - or td img /td for each bar\>  
%s  
  

|
| |

  
  
  
  
  
  
  
  
\<! x labels go here - td table /td \>  
%s  
  

|
| |

  
  
  
  
)

htmlOutputString \<\< bigTableString %  
[@height.to\_i.to\_s,@height.to\_i.to\_s,make\_y\_label\_lines,@width.to\_i.to\_s,make\_bars,make\_x\_label\_lines]  
puts htmlOutputString  
end #make\_html\_output  
end # class
