sparklines.rb
来自「用ruby on rails写的一个博客程序,还不错..ruby on rail」· RB 代码 · 共 436 行 · 第 1/2 页
RB
436 行
height = options[:height].to_i upper = options[:upper].to_i below_color = options[:below_color] above_color = options[:above_color] img = Magick::Image.new(results.size * 2 - 1, height) {self.background_color = options[:background_color]} img.format = "PNG" draw = Magick::Draw.new i=0 results.each do |r| color = (r >= upper) && above_color || below_color draw.stroke(color) draw.line(i, (img.rows - r/(101.0/(height-4))-4).to_i,i,(img.rows - r/(101.0/(height-4))).to_i) i+=2 end draw.draw(img) img.to_blob end# Creates a continuous area sparkline## * results is an array of integer values between 0 and 100 inclusive## * options is a hash that takes 4 parameters:## :step - An integer that determines the distance between each point on the sparkline. Defaults to 2.## :height - An integer that determines what the height of the sparkline will be. Defaults to 14## :upper - An ineger that determines the threshold for colorization purposes. Any value less than upper will be colored using below_color, anything above and equal to upper will use above_color. Defaults to 50.## :has_min - Determines whether a dot will be drawn at the lowest value or not. Defaulst to false.## :has_max - Determines whether a dot will be drawn at the highest value or not. Defaulst to false.## :has_last - Determines whether a dot will be drawn at the last value or not. Defaulst to false.## :min_color - A string or color code representing the color that the dot drawn at the smallest value will be displayed as. Defaults to blue.## :max_color - A string or color code representing the color that the dot drawn at the largest value will be displayed as. Defaults to green.## :last_color - A string or color code representing the color that the dot drawn at the last value will be displayed as. Defaults to red.## :above_color - A string or color code representing the color to draw values above or equal the upper value. Defaults to red.## :below_color - A string or color code representing the color to draw values below the upper value. Defaults to gray. def self.area(results=[], options={}) step = options[:step].to_i height = options[:height].to_i upper = options[:upper].to_i has_min = options[:has_min] has_max = options[:has_max] has_last = options[:has_last] min_color = options[:min_color] max_color = options[:max_color] last_color = options[:last_color] below_color = options[:below_color] above_color = options[:above_color] img = Magick::Image.new((results.size - 1) * step + 4, height) {self.background_color = options[:background_color]} img.format = "PNG" draw = Magick::Draw.new coords = [[0,(height - 3 - upper/(101.0/(height-4)))]] i=0 results.each do |r| coords.push [(2 + i), (height - 3 - r/(101.0/(height-4)))] i += step end coords.push [(results.size - 1) * step + 4, (height - 3 - upper/(101.0/(height-4)))] #Block off the bottom half of the image and draw the sparkline draw.fill(above_color) draw.define_clip_path('top') do draw.rectangle(0,0,(results.size - 1) * step + 4,(height - 3 - upper/(101.0/(height-4)))) end draw.clip_path('top') draw.polygon *coords.flatten #Block off the top half of the image and draw the sparkline draw.fill(below_color) draw.define_clip_path('bottom') do draw.rectangle(0,(height - 3 - upper/(101.0/(height-4))),(results.size - 1) * step + 4,height) end draw.clip_path('bottom') draw.polygon *coords.flatten #The sparkline looks kinda nasty if either the above_color or below_color gets the center line draw.fill('black') draw.line(0,(height - 3 - upper/(101.0/(height-4))),(results.size - 1) * step + 4,(height - 3 - upper/(101.0/(height-4)))) #After the parts have been masked, we need to let the whole canvas be drawable again #so a max dot can be displayed draw.define_clip_path('all') do draw.rectangle(0,0,img.columns,img.rows) end draw.clip_path('all') if has_min == 'true' min_pt = coords[results.index(results.min)+1] draw.fill(min_color) draw.rectangle(min_pt[0]-1, min_pt[1]-1, min_pt[0]+1, min_pt[1]+1) end if has_max == 'true' max_pt = coords[results.index(results.max)+1] draw.fill(max_color) draw.rectangle(max_pt[0]-1, max_pt[1]-1, max_pt[0]+1, max_pt[1]+1) end if has_last == 'true' last_pt = coords[-2] draw.fill(last_color) draw.rectangle(last_pt[0]-1, last_pt[1]-1, last_pt[0]+1, last_pt[1]+1) end draw.draw(img) img.to_blob end# Creates a smooth sparkline## * results - an array of integer values between 0 and 100 inclusive## * options - a hash that takes these optional parameters:## :step - An integer that determines the distance between each point on the sparkline. Defaults to 2.## :height - An integer that determines what the height of the sparkline will be. Defaults to 14## :has_min - Determines whether a dot will be drawn at the lowest value or not. Defaulst to false.## :has_max - Determines whether a dot will be drawn at the highest value or not. Defaulst to false.## :has_last - Determines whether a dot will be drawn at the last value or not. Defaulst to false.## :min_color - A string or color code representing the color that the dot drawn at the smallest value will be displayed as. Defaults to blue.## :max_color - A string or color code representing the color that the dot drawn at the largest value will be displayed as. Defaults to green.## :last_color - A string or color code representing the color that the dot drawn at the last value will be displayed as. Defaults to red. def self.smooth(results, options) step = options[:step].to_i height = options[:height].to_i min_color = options[:min_color] max_color = options[:max_color] last_color = options[:last_color] has_min = options[:has_min] has_max = options[:has_max] has_last = options[:has_last] line_color = options[:line_color] img = Magick::Image.new((results.size - 1) * step + 4, height.to_i) {self.background_color = options[:background_color]} img.format = "PNG" draw = Magick::Draw.new draw.stroke(line_color) coords = [] i=0 results.each do |r| coords.push [ 2 + i, (height - 3 - r/(101.0/(height-4))) ] i += step end my_polyline(draw, coords) if has_min == true min_pt = coords[results.index(results.min)] draw.fill(min_color) draw.rectangle(min_pt[0]-2, min_pt[1]-2, min_pt[0]+2, min_pt[1]+2) end if has_max == true max_pt = coords[results.index(results.max)] draw.fill(max_color) draw.rectangle(max_pt[0]-2, max_pt[1]-2, max_pt[0]+2, max_pt[1]+2) end if has_last == true last_pt = coords[-1] draw.fill(last_color) draw.rectangle(last_pt[0]-2, last_pt[1]-2, last_pt[0]+2, last_pt[1]+2) end draw.draw(img) img.to_blob end # This is a function to replace the RMagick polyline function because it doesn't seem to work properly. # # * draw - a RMagick::Draw object. # # * arr - an array of points (represented as two element arrays) def self.my_polyline (draw, arr) i = 0 while i < arr.size - 1 draw.line(arr[i][0], arr[i][1], arr[i+1][0], arr[i+1][1]) i += 1 end end # Draw the error Sparkline. Not implemented yet. def self.plot_error(options={}) img = Magick::Image.new(40,15) {self.background_color = options[:background_color]} img.format = "PNG" draw = Magick::Draw.new draw.fill('red') draw.line(0,0,40,15) draw.line(0,15,40,0) draw.draw(img) img.to_blob endend
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?