progress_bar_snapshot 0.0.3
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 +6 -0
- data/MIT-LICENSE +20 -0
- data/README +66 -0
- data/lib/progress_bar_snapshot.rb +109 -0
- data/lib/progress_bar_snapshot_helper.rb +22 -0
- metadata +50 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2005 Tobias Luetke
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
= ProgressbarSnapshot
|
2
|
+
|
3
|
+
A simple widget that generates progress bars in png format. There are other ways to
|
4
|
+
generate a progress bar with css, html and javascript. However, if you have to
|
5
|
+
stick a progress bar in a pdf you probably need to stick it in as an image.
|
6
|
+
|
7
|
+
Requires the Rmagick library
|
8
|
+
|
9
|
+
== Example usage:
|
10
|
+
|
11
|
+
In a stand alone script:
|
12
|
+
|
13
|
+
require 'progress_bar_snapshot'
|
14
|
+
|
15
|
+
pb = ProgressBarSnapshot.new(200, 50, 85.0)
|
16
|
+
pb.to_file("85percent.png")
|
17
|
+
|
18
|
+
This will generate a png file 200x50 called 85percent.png. The progress bar would be
|
19
|
+
85% full.
|
20
|
+
|
21
|
+
In your rails app:
|
22
|
+
|
23
|
+
In your controller:
|
24
|
+
require 'progress_bar_snapshot'
|
25
|
+
...
|
26
|
+
class MySomethingController < ApplicationController
|
27
|
+
helper :progress_bar_snapshot
|
28
|
+
...
|
29
|
+
# define an action to render and send the png
|
30
|
+
def progress_bar
|
31
|
+
bar = ProgressBarSnapshot.new(
|
32
|
+
params[:width].to_i, params[:height].to_i,
|
33
|
+
params[:percentage].to_f)
|
34
|
+
|
35
|
+
send_data bar.to_blob, :type => 'image/png', :disposition => 'inline',
|
36
|
+
:filename => "progress#{params[:percentage].to_i}.png"
|
37
|
+
end
|
38
|
+
|
39
|
+
In your view:
|
40
|
+
<%= progress_bar_png_tag('88.1', {
|
41
|
+
:width => 200, :height=> 20, :controller => 'my_something', :action => 'progress_bar'}) %>
|
42
|
+
|
43
|
+
|
44
|
+
Customizable colors:
|
45
|
+
|
46
|
+
These following parameters can be passed in to the progress bar snapshot constructor
|
47
|
+
|
48
|
+
:inner_border # inner border of the progress bar default to "#CCCCCC",
|
49
|
+
:border_color # outer border color defaults to "#666666",
|
50
|
+
:progress_color # progress bar color defaults to "#B3D235",
|
51
|
+
:background # color of unfilled portion of the progress bar, defaults to "white",
|
52
|
+
:font_family # default font to "arial",
|
53
|
+
:font_color # default color of the font "black",
|
54
|
+
|
55
|
+
All the colors are passed straight to Rmagick, so they can be any color Rmagick supports.
|
56
|
+
|
57
|
+
For example:
|
58
|
+
|
59
|
+
pb = ProgressBarSnapshot.new(200, 50, 35.1, :border_color => 'black',
|
60
|
+
:progress_color => 'green', :font_color => 'white', :background => 'grey')
|
61
|
+
|
62
|
+
|
63
|
+
== Authors, credits, blame, contact!
|
64
|
+
|
65
|
+
xiaoke@microplace.com
|
66
|
+
julio@microplace.com
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'RMagick'
|
3
|
+
|
4
|
+
=begin rdoc
|
5
|
+
|
6
|
+
A library for rendering the image of a progress bar.
|
7
|
+
|
8
|
+
Used to generate an progress bar images to a file or in RAILS apps. There are easier ways to do progrss bars
|
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.
|
11
|
+
|
12
|
+
RMagick library required.
|
13
|
+
|
14
|
+
==Authors
|
15
|
+
|
16
|
+
Xiaoke Zhang
|
17
|
+
|
18
|
+
==General Usage and Defaults
|
19
|
+
|
20
|
+
|
21
|
+
==License
|
22
|
+
|
23
|
+
Licensed under the MIT license.
|
24
|
+
|
25
|
+
=end
|
26
|
+
|
27
|
+
class ProgressBarSnapshot
|
28
|
+
|
29
|
+
VERSION = '0.0.3'
|
30
|
+
|
31
|
+
def initialize (width, height, percentage, theme = {})
|
32
|
+
@width = width
|
33
|
+
@height = height
|
34
|
+
@percentage = percentage
|
35
|
+
@default_theme = {
|
36
|
+
:inner_border => "#CCCCCC",
|
37
|
+
:border_color => "#666666",
|
38
|
+
:progress_color => "skyblue",
|
39
|
+
:background => "white",
|
40
|
+
:font_family => "arial",
|
41
|
+
:font_color => "black",
|
42
|
+
:border_width => 1
|
43
|
+
}
|
44
|
+
@default_theme.update(theme)
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_blob(format = 'png')
|
48
|
+
image = render
|
49
|
+
image.format = format
|
50
|
+
image.to_blob {self.depth = 8}
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_file(filename)
|
54
|
+
render.write(filename) {self.depth = 8}
|
55
|
+
end
|
56
|
+
|
57
|
+
def render
|
58
|
+
canvas = Magick::ImageList.new
|
59
|
+
canvas.new_image(@width, @height)
|
60
|
+
|
61
|
+
progress_bar = draw_bar
|
62
|
+
draw_inner_progress(progress_bar) if ((1.0..100.0).include?(@percentage))
|
63
|
+
text = draw_percentage_text
|
64
|
+
|
65
|
+
progress_bar.draw(canvas)
|
66
|
+
text.annotate(canvas, 0,0,0,0, "#{@percentage.to_i}%")
|
67
|
+
|
68
|
+
canvas
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def draw_bar
|
74
|
+
progress_bar = Magick::Draw.new
|
75
|
+
|
76
|
+
progress_bar.stroke(@default_theme[:border_color])
|
77
|
+
progress_bar.fill(@default_theme[:background])
|
78
|
+
# progress_bar.stroke_opacity(1)
|
79
|
+
|
80
|
+
brush_size = @default_theme[:border_width]
|
81
|
+
progress_bar.stroke_width(brush_size)
|
82
|
+
# outer border
|
83
|
+
progress_bar.rectangle(0, 0, @width - brush_size, @height - brush_size)
|
84
|
+
|
85
|
+
progress_bar
|
86
|
+
end
|
87
|
+
|
88
|
+
def draw_inner_progress(progress_bar)
|
89
|
+
brush_size = @default_theme[:border_width]
|
90
|
+
|
91
|
+
inner_x = [((@width - 2*brush_size) * @percentage /100.0).to_i, brush_size].max
|
92
|
+
inner_y = @height - 2*brush_size
|
93
|
+
progress_bar.stroke(@default_theme[:inner_border])
|
94
|
+
progress_bar.fill(@default_theme[:progress_color])
|
95
|
+
|
96
|
+
brush_size = @default_theme[:border_width]
|
97
|
+
progress_bar.stroke_width(brush_size)
|
98
|
+
progress_bar.rectangle(brush_size, brush_size, inner_x , inner_y)
|
99
|
+
end
|
100
|
+
|
101
|
+
def draw_percentage_text
|
102
|
+
text = Magick::Draw.new
|
103
|
+
text.font_family = @default_theme[:font_family]
|
104
|
+
text.pointsize = (@height * 0.7).to_i
|
105
|
+
text.gravity = Magick::CenterGravity
|
106
|
+
text.fill = @default_theme[:font_color]
|
107
|
+
text
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# Provides a tag for embedding progress bar snapshots in your views
|
2
|
+
|
3
|
+
# need a controler action to actually do the image generation and sending of the
|
4
|
+
# image blob. See README for examples.
|
5
|
+
|
6
|
+
module ProgressBarSnapshotHelper
|
7
|
+
|
8
|
+
def progress_bar_png_tag(percent, opts = {})
|
9
|
+
url = {
|
10
|
+
:percentage => percent
|
11
|
+
}
|
12
|
+
|
13
|
+
options = url.merge(opts)
|
14
|
+
|
15
|
+
# extra html attributes for the image tag
|
16
|
+
attributes = %(class="#{options[:class] || 'progressbar'}" alt="#{percent.round}%" )
|
17
|
+
attributes << %(title="#{options[:title]}" ) if options[:title]
|
18
|
+
|
19
|
+
%(<img src="#{ url_for options }" #{attributes}/>)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: progress_bar_snapshot
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.3
|
7
|
+
date: 2008-03-27 00:00:00 -07:00
|
8
|
+
summary: Generates png images of progress bars
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: xiaoke@microplace.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description: Generats png images of progress bars, useful for web pages or use in pdf files.
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Xiaoke Zhang
|
31
|
+
files:
|
32
|
+
- README
|
33
|
+
- CHANGELOG
|
34
|
+
- MIT-LICENSE
|
35
|
+
- lib/progress_bar_snapshot.rb
|
36
|
+
- lib/progress_bar_snapshot_helper.rb
|
37
|
+
test_files: []
|
38
|
+
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
executables: []
|
44
|
+
|
45
|
+
extensions: []
|
46
|
+
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
dependencies: []
|
50
|
+
|