[ANN] SimpleGraphs 0.00000001


#1

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

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. :slight_smile:

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