Module: Spark
- Defined in:
- lib/spark_pr.rb
Class Method Summary collapse
- .discrete(results, options = {}) ⇒ Object
-
.normalize(arr, type = :linear) ⇒ Object
normalize arr to contain values between 0..1 inclusive.
-
.plot(results, options = {}) ⇒ Object
convenience method.
- .process_options(options) ⇒ Object
- .smooth(results, options = {}) ⇒ Object
Class Method Details
.discrete(results, options = {}) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/spark_pr.rb', line 187 def Spark.discrete( results, = {} ) = self.() o = { :height => 14, :upper => 0.5, :has_min => false, :has_max => false }.merge() o[:width] ||= results.size*2-1 c = SparkCanvas.new(o[:width], o[:height]) results = Spark.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 c end |
.normalize(arr, type = :linear) ⇒ Object
normalize arr to contain values between 0..1 inclusive
119 120 121 122 123 124 125 126 127 |
# File 'lib/spark_pr.rb', line 119 def Spark.normalize( arr, type = :linear ) arr.map!{|v| Math.log(v) } if type == :logarithmic adj, fac = arr.min, arr.max-arr.min arr.map do |v| v = (v-adj).quo(fac) rescue 0 v = 0 if v.respond_to?(:nan?) && v.nan? v end end |
.plot(results, options = {}) ⇒ Object
convenience method
214 215 216 217 218 |
# File 'lib/spark_pr.rb', line 214 def Spark.plot( results, = {}) = self.() [:type] ||= 'smooth' self.send([:type], results, ).to_png end |
.process_options(options) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/spark_pr.rb', line 129 def Spark.( ) 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 o end |
.smooth(results, options = {}) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/spark_pr.rb', line 144 def Spark.smooth( results, = {} ) = self.() o = { :step => 2, :height => 14, :has_min => false, :has_max => false }.merge() o[:width] ||= (results.size-1)*o[:step] + 5 c = SparkCanvas.new(o[:width], o[:height]) results = Spark.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 = [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 c end |