Forum: Ruby [ANN] SimpleGraphs 0.00000001

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
8dad1ec4d769734583f45fbbee5cd009?d=identicon&s=25 Jeff Pritchard (Guest)
on 2006-06-08 06:50
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:

This is all the erb needed to generate that:

% require "SimpleGraphs.rb"
% bar =,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

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.


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 = []

	attr_reader :xmin, :xmax, :dataPoints

	def points

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

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

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

	def bar_width

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

	def bar_from_file(value)
	  barString = ""
	  barFileString = "\t\t\t\t\t\t<td><img src=\"%s\" width=\"%s\"
	  barString << barFileString % [@colorFile,bar_width,bar_height(value)]

	def bar_from_table(value)
	  barString = ""
	  barTableString = "\t\t\t\t\t\t<td><table width=\"%s\" height=\"%s\"
cellspacing=0 cellpadding=0><tr><td
	  barString << barTableString % [bar_width,bar_height(value)]

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

	def make_y_label_lines
	  outputString = ""
	  oneLine = %Q{ \t\t\t\t\t<tr ><td align="right"
valign="%s"><small>%s</small></td></tr> \n }
	  @yLabels.each_with_index do |label,index|
	    vertical =  case index
                    when 0: "top"
                    when 1..@yLabels.length-2: "middle"
                    when @yLabels.length-1: "bottom"
	    outputString << oneLine % [vertical, label]

	def make_x_label_lines
	  oneLabel = %Q{\t\t\t\t\t\t<td><table width="%s" cellspacing=0
align="center"><small>%s</small></td></tr></table></td> \n}
	  xlabelout = ""
	  @xLabels.each do |label|
	    xlabelout << oneLabel % [bar_width,label]

	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 >
  			<td width="%s" id="bars" valign="bottom">
  				<table cellspacing=5 cellpadding=0 >
  					<tr valign="bottom">
  						<! td table /td for each bar - or td img /td for each bar>
  		<tr height="20" id="x label row" >
  				<table cellspacing=5 cellpadding=0 >
  					<tr valign="bottom">
  						<! x labels go here - td table /td >

  htmlOutputString << bigTableString %
  puts htmlOutputString
	end  #make_html_output
end  # class
This topic is locked and can not be replied to.