Module: BumpsparkHelper
- Included in:
- TuneupHelper
- Defined in:
- lib/bumpspark_helper.rb
Overview
Note: This is a modified copy of _why’s ‘bumpspark’ library, discussed and collaborated on at:
http://redhanded.hobix.com/inspect/sparklinesForMinimalists.html
Many thanks to the various collaborators; _why (concept), MenTaLguY (transparency), and jzp (png)
Instance Method Summary collapse
- #build_data_url(type, data) ⇒ Object
- #build_png(image_rows) ⇒ Object
-
#build_png_chunk(type, data) ⇒ Object
:nodoc:.
- #bumpspark(results) ⇒ Object
- #normalize(results) ⇒ Object
Instance Method Details
#build_data_url(type, data) ⇒ Object
37 38 39 40 |
# File 'lib/bumpspark_helper.rb', line 37 def build_data_url(type,data) data = Base64.encode64(data).delete("\n") return "data:#{type};base64,#{CGI.escape(data)}" end |
#build_png(image_rows) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bumpspark_helper.rb', line 15 def build_png(image_rows) header = [137, 80, 78, 71, 13, 10, 26, 10].pack("C*") raw_data = image_rows.map { |row| [0] + row }.flatten.pack("C*") ihdr_data = [ image_rows.first.length,image_rows.length, 8,2,0,0,0].pack("NNCCCCC") ihdr = build_png_chunk("IHDR", ihdr_data) trns = build_png_chunk("tRNS", ([ 0 ]*6).pack("C6")) idat = build_png_chunk("IDAT", Zlib::Deflate.deflate(raw_data)) iend = build_png_chunk("IEND", "") return header + ihdr + trns + idat + iend end |
#build_png_chunk(type, data) ⇒ Object
:nodoc:
10 11 12 13 |
# File 'lib/bumpspark_helper.rb', line 10 def build_png_chunk(type,data) to_check = type + data return [data.length].pack("N") + to_check + [Zlib.crc32(to_check)].pack("N") end |
#bumpspark(results) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bumpspark_helper.rb', line 27 def bumpspark(results) black = [0, 0, 0] white, red, grey = [0xFF,0xFF,0xFF], [0xFF,0,0], [0x99,0x99,0x99] rows = normalize(results).inject([]) do |ary, r| ary << [black]*15 << [black]*15 ary.last[r/9,4] = [(r > 50 and red or grey)]*4 ary end.transpose.reverse return build_png(rows) end |
#normalize(results) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/bumpspark_helper.rb', line 42 def normalize(results) min, max = results.min, results.max width = max - min return [1] * results.size if width == 0 width += (300 * 1000) results.map do |result| ((result - min) * 100 / width.to_f).to_i end end |