From: Dave Howell Date: 2006-06-03T06:26:01+09:00 Subject: Re: This is brain dead, but is it stupid? 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:
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 medium red light red light red medium red medium red dark red 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 graph.image = "/images/bluebar.gif" but if I don't, then the bar graph uses tables (or some more modern CSS equivalent). graph.barcolor might set the bar color to something other than the default, although .image would override that anyway. graph.styleclass = "vertbargraph" could change the outer enclosing table from to
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. graph.htmlsource maybe? Or something more like that?