From: Carl Youngblood Date: 2005-06-28T04:02:45+09:00 Subject: Re: What do you want to see in a Sparklines Library? ------=_Part_1303_25629569.1119898910310 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Here's an improvement. Make your CGI script accept parameters so that you= =20 can make a generic script that will generate sparklines for all types of=20 data: ... cgi =3D CGI.new data =3D cgi['data'] if data.empty? send_to_browser (0..22).map { rand 100 } else send_to_browser data.split(',').collect! {|e| e.to_i} end Example in action: http://youngbloods.org/sparkline2.cgi?data=3D23,19,67,20,5 Carl On 6/27/05, Carl Youngblood wrote: >=20 > One thing that would make sparklines a lot more universally accessible to= =20 > all browsers would be to get rid of the dependency on data urls. The basi= c=20 > idea is to return the proper MIME type to the browser and then just start= =20 > spitting out the raw binary data of the PNG. >=20 > Here is an example: >=20 > #!/usr/bin/ruby >=20 > require 'base64' > require 'cgi' > require 'zlib' >=20 > def build_png_chunk(type,data) > to_check =3D type + data > return [data.length].pack("N") + to_check + > [Zlib.crc32(to_check)].pack("N") > end >=20 > def build_png(image_rows) > header =3D [137, 80, 78, 71, 13, 10, 26, 10].pack("C*") > raw_data =3D image_rows.map { |row| [0] + row }.flatten.pack("C*") > ihdr_data =3D [image_rows.first.length, > image_rows.length, > 8,2,0,0,0].pack("NNCCCCC") > ihdr =3D build_png_chunk("IHDR", ihdr_data) > idat =3D build_png_chunk("IDAT", Zlib::Deflate.deflate(raw_data)) > iend =3D build_png_chunk("IEND", "") >=20 > return header + ihdr + idat + iend > end >=20 > def bumpspark(results) > white, red, grey =3D [0xFF,0xFF,0xFF], [0,0,0xFF], [0x99,0x99,0x99] > rows =3D results.inject([]) do |ary, r| > ary << [white]*15 << [white]*15 > ary.last[r/9,4] =3D [(r > 50 and red or grey)]*4 > ary > end.transpose > return build_png(rows) > end >=20 > def send_to_browser(results) > puts "Content-type: image/png" > puts "Expires: Wed, 11 Nov 1998 11:11:11 GMT" > puts "Cache-Control: no-cache" > puts "Cache-Control: must-revalidate" > puts > print bumpspark(results) > end >=20 > send_to_browser (0..22).map { rand 100 } >=20 >=20 > See this in action at the following URL: >=20 > http://youngbloods.org/sparkline.cgi >=20 > Carl > ------=_Part_1303_25629569.1119898910310--