Class: ProgressBarSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/progress_bar_snapshot.rb

Overview

A library for rendering the image of a progress bar.

Used to generate an progress bar images to a file or in RAILS apps.

RMagick library required.

Authors

Xiaoke Zhang

General Usage and Defaults

License

Licensed under the MIT license.

Constant Summary collapse

VERSION =
'0.0.4'

Instance Method Summary collapse

Constructor Details

#initialize(width, height, percentages, theme = {}) ⇒ ProgressBarSnapshot

Returns a new instance of ProgressBarSnapshot.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/progress_bar_snapshot.rb', line 28

def initialize (width, height, percentages, theme = {})
  @width                     = [width, 0].max
  @height                    = [height, 0].max
  @percentages_segments      = percentages.select{|p| p >= 0}

  # default theme if none was passed in
  @default_theme   = {
    :border_color   => "#666666", 
    :progress_colors => ["skyblue", 'grey' ,'salmon'],
    :background     => "white",
    :font_family    => "arial",
    :font_color     => "black",
    :border_width   => 1
  }
  @default_theme.update(theme)    
end

Instance Method Details

#renderObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/progress_bar_snapshot.rb', line 55

def render
  # new image
  canvas = Magick::ImageList.new
  canvas.new_image(@width, @height)
 
  # make a new draw object
  progress_bar = draw_bar
  # make a new text object
  text = draw_percentage_text

  # progress bar
  total_percentage = [@percentages_segments.inject{|sum, n| sum + n}, 100].min
  draw_inner_progress(progress_bar) if ((1.0..100.0).include?(total_percentage))

  # draw the progress bar on canvas
  progress_bar.draw(canvas)
  
  # draw the percentage number on the progress bar
  text.annotate(canvas, 0,0,0,0, "#{total_percentage.to_i}%")

  canvas
end

#to_blob(format = 'png') ⇒ Object



45
46
47
48
49
# File 'lib/progress_bar_snapshot.rb', line 45

def to_blob(format = 'png')
  image = render
  image.format = format
  image.to_blob {self.depth = 8}
end

#to_file(filename) ⇒ Object



51
52
53
# File 'lib/progress_bar_snapshot.rb', line 51

def to_file(filename)
  render.write(filename) {self.depth = 8}
end