progress_bar_snapshot 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +8 -2
- data/README +34 -19
- data/lib/progress_bar_snapshot.rb +53 -24
- data/lib/progress_bar_snapshot_helper.rb +4 -3
- metadata +2 -2
data/CHANGELOG
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
# ---
|
3
|
-
# Version 0.0.1 First release
|
4
|
-
#
|
5
3
|
#
|
4
|
+
# Version 0.0.4
|
5
|
+
# * added capability to add multiple segments of "progress" into one progress bar.
|
6
|
+
# For example it can show combined progress from 2 or more different sources.
|
7
|
+
# * error handling like filtering input for negative numbers
|
8
|
+
#
|
9
|
+
# Version 0.0.3 First release
|
10
|
+
#
|
11
|
+
|
6
12
|
|
data/README
CHANGED
@@ -4,19 +4,19 @@ A simple widget that generates progress bars in png format. There are other way
|
|
4
4
|
generate a progress bar with css, html and javascript. However, if you have to
|
5
5
|
stick a progress bar in a pdf you probably need to stick it in as an image.
|
6
6
|
|
7
|
-
|
7
|
+
Rmagick gem required.
|
8
8
|
|
9
9
|
== Example usage:
|
10
10
|
|
11
11
|
In a stand alone script:
|
12
|
-
|
12
|
+
require 'rubygems'
|
13
13
|
require 'progress_bar_snapshot'
|
14
14
|
|
15
|
-
pb = ProgressBarSnapshot.new(200, 50,
|
16
|
-
pb.to_file("
|
15
|
+
pb = ProgressBarSnapshot.new(200, 50, [15.0, 25, 4.1, 2.9])
|
16
|
+
pb.to_file("47percent.png")
|
17
17
|
|
18
|
-
This will generate a png file 200x50 called
|
19
|
-
|
18
|
+
This will generate a png file 200x50 called 47percent.png. Since the sum of
|
19
|
+
15.0, 25, 4.1, 2.9 is 47 the progress bar would be 47% full.
|
20
20
|
|
21
21
|
In your rails app:
|
22
22
|
|
@@ -28,37 +28,52 @@ In your rails app:
|
|
28
28
|
...
|
29
29
|
# define an action to render and send the png
|
30
30
|
def progress_bar
|
31
|
+
|
32
|
+
percentages = params[:percentages].split(",").collect { |p| p.to_f }
|
33
|
+
|
31
34
|
bar = ProgressBarSnapshot.new(
|
32
35
|
params[:width].to_i, params[:height].to_i,
|
33
|
-
|
34
|
-
|
36
|
+
percentages)
|
37
|
+
|
35
38
|
send_data bar.to_blob, :type => 'image/png', :disposition => 'inline',
|
36
|
-
:filename => "progress
|
39
|
+
:filename => "progress.png"
|
37
40
|
end
|
38
41
|
|
39
42
|
In your view:
|
40
|
-
|
41
|
-
|
43
|
+
<%= progress_bar_png_tag(
|
44
|
+
[12.4, 24,1, 25.6],
|
45
|
+
['#0398B3', '#E9C74B' ],
|
46
|
+
{ :width => 178,
|
47
|
+
:height => 18,
|
48
|
+
:controller => 'my_something',
|
49
|
+
:action => 'progress_bar' }
|
50
|
+
) %>
|
42
51
|
|
43
52
|
|
44
53
|
Customizable colors:
|
45
54
|
|
46
55
|
These following parameters can be passed in to the progress bar snapshot constructor
|
47
56
|
|
48
|
-
:
|
49
|
-
:
|
50
|
-
:
|
51
|
-
:
|
52
|
-
:
|
53
|
-
:font_color # default color of the font "black",
|
57
|
+
:border_color # outer border color, defaults to "#666666",
|
58
|
+
:progress_colors # progress bar colors, defaults to alternating between 'skyblue', 'grey' ,'salmon',
|
59
|
+
:background # color of unfilled portion of the progress bar, defaults to "white",
|
60
|
+
:font_family # default font to "arial",
|
61
|
+
:font_color # default color of the font "black",
|
54
62
|
|
55
63
|
All the colors are passed straight to Rmagick, so they can be any color Rmagick supports.
|
56
64
|
|
57
65
|
For example:
|
58
66
|
|
59
|
-
pb = ProgressBarSnapshot.new(200, 50, 35.
|
60
|
-
|
67
|
+
pb = ProgressBarSnapshot.new(200, 50, [35.0, 25, 14.1, 12.9],
|
68
|
+
:progress_colors => ['blue', 'SteelBlue', 'skyblue', 'grey'],
|
69
|
+
:background => 'grey90')
|
70
|
+
|
71
|
+
|
72
|
+
Multiple Segments:
|
61
73
|
|
74
|
+
This release of progress bar can be instructed to generate "sausage links" segments of progress bars.
|
75
|
+
You can pass it any number of percentages and their corresponding colors. Each of these percentages
|
76
|
+
would be drawn and their sum would be displayed on the progress bar.
|
62
77
|
|
63
78
|
== Authors, credits, blame, contact!
|
64
79
|
|
@@ -5,9 +5,7 @@ require 'RMagick'
|
|
5
5
|
|
6
6
|
A library for rendering the image of a progress bar.
|
7
7
|
|
8
|
-
Used to generate an progress bar images to a file or in RAILS apps.
|
9
|
-
with css and html, but if you have to put the progress bar in a pdf or an email you might need to just
|
10
|
-
render a picture instead.
|
8
|
+
Used to generate an progress bar images to a file or in RAILS apps.
|
11
9
|
|
12
10
|
RMagick library required.
|
13
11
|
|
@@ -17,7 +15,6 @@ Xiaoke Zhang
|
|
17
15
|
|
18
16
|
==General Usage and Defaults
|
19
17
|
|
20
|
-
|
21
18
|
==License
|
22
19
|
|
23
20
|
Licensed under the MIT license.
|
@@ -26,22 +23,23 @@ Licensed under the MIT license.
|
|
26
23
|
|
27
24
|
class ProgressBarSnapshot
|
28
25
|
|
29
|
-
VERSION = '0.0.
|
26
|
+
VERSION = '0.0.4'
|
30
27
|
|
31
|
-
def initialize (width, height,
|
32
|
-
@width
|
33
|
-
@height
|
34
|
-
@
|
28
|
+
def initialize (width, height, percentages, theme = {})
|
29
|
+
@width = [width, 0].max
|
30
|
+
@height = [height, 0].max
|
31
|
+
@percentages_segments = percentages.select{|p| p >= 0}
|
32
|
+
|
33
|
+
# default theme if none was passed in
|
35
34
|
@default_theme = {
|
36
|
-
:inner_border => "#CCCCCC",
|
37
35
|
:border_color => "#666666",
|
38
|
-
:
|
36
|
+
:progress_colors => ["skyblue", 'grey' ,'salmon'],
|
39
37
|
:background => "white",
|
40
38
|
:font_family => "arial",
|
41
39
|
:font_color => "black",
|
42
40
|
:border_width => 1
|
43
41
|
}
|
44
|
-
@default_theme.update(theme)
|
42
|
+
@default_theme.update(theme)
|
45
43
|
end
|
46
44
|
|
47
45
|
def to_blob(format = 'png')
|
@@ -55,15 +53,24 @@ class ProgressBarSnapshot
|
|
55
53
|
end
|
56
54
|
|
57
55
|
def render
|
56
|
+
# new image
|
58
57
|
canvas = Magick::ImageList.new
|
59
58
|
canvas.new_image(@width, @height)
|
60
59
|
|
60
|
+
# make a new draw object
|
61
61
|
progress_bar = draw_bar
|
62
|
-
|
62
|
+
# make a new text object
|
63
63
|
text = draw_percentage_text
|
64
|
-
|
64
|
+
|
65
|
+
# progress bar
|
66
|
+
total_percentage = [@percentages_segments.inject{|sum, n| sum + n}, 100].min
|
67
|
+
draw_inner_progress(progress_bar) if ((1.0..100.0).include?(total_percentage))
|
68
|
+
|
69
|
+
# draw the progress bar on canvas
|
65
70
|
progress_bar.draw(canvas)
|
66
|
-
|
71
|
+
|
72
|
+
# draw the percentage number on the progress bar
|
73
|
+
text.annotate(canvas, 0,0,0,0, "#{total_percentage.to_i}%")
|
67
74
|
|
68
75
|
canvas
|
69
76
|
end
|
@@ -87,15 +94,31 @@ class ProgressBarSnapshot
|
|
87
94
|
|
88
95
|
def draw_inner_progress(progress_bar)
|
89
96
|
brush_size = @default_theme[:border_width]
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
97
|
+
bottom_y = @height - 2 * brush_size
|
98
|
+
top_y = brush_size
|
99
|
+
max_inner_width = @width - 2 * brush_size
|
100
|
+
|
101
|
+
last_x = brush_size
|
102
|
+
@percentages_segments.each_with_index do |segment, index|
|
103
|
+
# no point drawing if it's increases 0 pixels
|
104
|
+
next if (segment == 0)
|
105
|
+
next if (last_x >= max_inner_width )
|
106
|
+
|
107
|
+
x_increment = [((@width - 2 * brush_size) * segment /100.0).to_i, brush_size].max
|
108
|
+
|
109
|
+
# no bleeding over the edge for inner progress bar
|
110
|
+
next_x = [last_x + x_increment, max_inner_width].min
|
111
|
+
|
112
|
+
# sets up for drawing the box
|
113
|
+
color = get_color(index)
|
114
|
+
progress_bar.fill(color)
|
115
|
+
progress_bar.stroke(color)
|
116
|
+
progress_bar.stroke_width(brush_size)
|
117
|
+
|
118
|
+
# add the rectangle to the draw object
|
119
|
+
progress_bar.rectangle(last_x, top_y, next_x, bottom_y)
|
120
|
+
last_x = next_x
|
121
|
+
end
|
99
122
|
end
|
100
123
|
|
101
124
|
def draw_percentage_text
|
@@ -106,4 +129,10 @@ class ProgressBarSnapshot
|
|
106
129
|
text.fill = @default_theme[:font_color]
|
107
130
|
text
|
108
131
|
end
|
132
|
+
|
133
|
+
def get_color(index)
|
134
|
+
# alternate between the progress colors in case # of percentages
|
135
|
+
# segements is greater than the number of colors given
|
136
|
+
@default_theme[:progress_colors][index % @default_theme[:progress_colors].size]
|
137
|
+
end
|
109
138
|
end
|
@@ -5,15 +5,16 @@
|
|
5
5
|
|
6
6
|
module ProgressBarSnapshotHelper
|
7
7
|
|
8
|
-
def progress_bar_png_tag(percent, opts = {})
|
8
|
+
def progress_bar_png_tag(percent, colors, opts = {})
|
9
9
|
url = {
|
10
|
-
:
|
10
|
+
:percentages => percent.join(","),
|
11
|
+
:colors => colors.join(","),
|
11
12
|
}
|
12
13
|
|
13
14
|
options = url.merge(opts)
|
14
15
|
|
15
16
|
# extra html attributes for the image tag
|
16
|
-
attributes = %(class="#{options[:class] || 'progressbar'}" alt="#{percent.round}%" )
|
17
|
+
attributes = %(class="#{options[:class] || 'progressbar'}" alt="#{percent.sum.round}%" )
|
17
18
|
attributes << %(title="#{options[:title]}" ) if options[:title]
|
18
19
|
|
19
20
|
%(<img src="#{ url_for options }" #{attributes}/>)
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: progress_bar_snapshot
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2008-
|
6
|
+
version: 0.0.4
|
7
|
+
date: 2008-05-09 00:00:00 -07:00
|
8
8
|
summary: Generates png images of progress bars
|
9
9
|
require_paths:
|
10
10
|
- lib
|