Class: Spark
- Inherits:
-
Object
- Object
- Spark
- Defined in:
- lib/spark_pr/spark.rb
Instance Attribute Summary collapse
-
#canvas ⇒ Object
Returns the value of attribute canvas.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#png ⇒ Object
Returns the value of attribute png.
Class Method Summary collapse
- .data_uri(results, options = {}) ⇒ Object
-
.plot(results, options = {}) ⇒ Object
convenience methods.
Instance Method Summary collapse
- #data_uri ⇒ Object
- #discrete(results) ⇒ Object
-
#initialize(options) ⇒ Spark
constructor
A new instance of Spark.
-
#normalize(arr, type = :linear) ⇒ Object
normalize arr to contain values between 0..1 inclusive.
- #smooth(results) ⇒ Object
Constructor Details
#initialize(options) ⇒ Spark
Returns a new instance of Spark.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/spark_pr/spark.rb', line 7 def initialize() # process options o = .inject({}) do |o, (key, value)| o[key.to_sym] = value ; o end [:height, :width, :step].each do |k| o[k] = o[k].to_i if o.has_key?(k) end [:has_min, :has_max, :has_last].each do |k| o[k] = (o[k] ? true : false) if o.has_key?(k) end o[:normalize] ||= :linear o[:normalize] = o[:normalize].to_sym self.opts = o end |
Instance Attribute Details
#canvas ⇒ Object
Returns the value of attribute canvas.
5 6 7 |
# File 'lib/spark_pr/spark.rb', line 5 def canvas @canvas end |
#opts ⇒ Object
Returns the value of attribute opts.
5 6 7 |
# File 'lib/spark_pr/spark.rb', line 5 def opts @opts end |
#png ⇒ Object
Returns the value of attribute png.
5 6 7 |
# File 'lib/spark_pr/spark.rb', line 5 def png @png end |
Class Method Details
Instance Method Details
#data_uri ⇒ Object
111 112 113 |
# File 'lib/spark_pr/spark.rb', line 111 def data_uri %{data:image/png;base64,#{Base64.encode64(png).gsub("\n",'')}} end |
#discrete(results) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/spark_pr/spark.rb', line 81 def discrete( results ) o = { :height => 14, :upper => 0.5, :has_min => false, :has_max => false }.merge(opts) o[:width] ||= results.size*2-1 c = if o[:base_color] SparkCanvas.new(o[:width], o[:height], o[:base_color]) else SparkCanvas.new(o[:width], o[:height]) end results = normalize(results, o[:normalize]) fac = c.height-4 i = -2 results.each do |r| p = c.height - 4 - r*fac c.color = r < o[:upper] ? [0x66,0x66,0x66,0xFF] : [0xFF,0x00,0x00,0xFF] c.line(i+=2, p, i, p+3) end self.png = c.to_png self.canvas = c end |
#normalize(arr, type = :linear) ⇒ Object
normalize arr to contain values between 0..1 inclusive
25 26 27 28 29 30 31 32 |
# File 'lib/spark_pr/spark.rb', line 25 def normalize( arr, type = :linear ) arr = arr.map { |v| v.to_f == 0.0 ? 0.0 : Math.log(v) } if type == :logarithmic return arr if array.min == array.max adj, fac = arr.min, arr.max-arr.min arr.map {|v| (v-adj).quo(fac) rescue 0 } end |
#smooth(results) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/spark_pr/spark.rb', line 34 def smooth( results ) o = { :step => 2, :height => 14, :has_min => false, :has_max => false }.merge(opts) o[:width] ||= (results.size-1)*o[:step] + 5 c = if o[:base_color] SparkCanvas.new(o[:width], o[:height], o[:base_color]) else SparkCanvas.new(o[:width], o[:height]) end results = normalize(results, o[:normalize]) fac = c.height-5 i = -o[:step] coords = results.map do |r| [(i += o[:step])+2, c.height - 3 - r*fac ] end c.color = opts[:line_color] || [0xB0, 0xB0, 0xB0, 0xFF] c.polyline coords if o[:has_min] min_pt = coords[results.index(results.min)] c.color = [0x80, 0x80, 0x00, 0x70] c.rectangle(min_pt[0]-2, min_pt[1]-2, min_pt[0]+2, min_pt[1]+2) end if o[:has_max] max_pt = coords[results.index(results.max)] c.color = [0x00, 0x80, 0x00, 0x70] c.rectangle(max_pt[0]-2, max_pt[1]-2, max_pt[0]+2, max_pt[1]+2) end if o[:has_last] c.color = [0xFF, 0x00, 0x00, 0x70] c.rectangle(coords.last[0]-2, coords.last[1]-2, coords.last[0]+2, coords.last[1]+2) end self.png = c.to_png self.canvas = c end |