bumpspark 1.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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Bruce Williams (and collaborators at RedHanded)
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 PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL 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.
@@ -0,0 +1,66 @@
1
+ # Bumpspark
2
+
3
+ Generate "bumpspark"-style sparklines from Ruby & Rails.
4
+
5
+ Note: This library is based on _why's `bumpspark' code, originally discussed and
6
+ collaborated on at [RedHanded] [1]. It has been refactored and built out as
7
+ a gem suitable for inclusion in Rails projects (and standalone Ruby code).
8
+
9
+ Bumpsparks are sparklines which show discrete data points and highlight
10
+ extremes. If you like Tufte and _why, you'll probably like these.
11
+
12
+ ## Credits
13
+
14
+ Thanks to the various collaborators on _why's original post:
15
+
16
+ * _why (concept, BMP implementation)
17
+ * jzp (png)
18
+ * MenTaLguY (transparency)
19
+
20
+ ## Installation
21
+
22
+ sudo gem install bruce-bumpspark --source http://gems.github.com
23
+
24
+ ## Usage
25
+
26
+ ### From Rails
27
+
28
+ 1. Include the gem as a dependency in `config/environment.rb`
29
+
30
+ config.gem 'bruce-bumpspark', :lib => 'bumpspark', :source => 'http://gems.github.com'
31
+
32
+ 2. Use `bumpspark_tag` from your views or helpers, passing it the data points
33
+ you'd like graphed.
34
+
35
+ <%= bumpspark_tag [12, 34, 12, 42, 12, 23] %>
36
+
37
+ ## From Ruby
38
+
39
+ Simply create a `Bumpspark::Graph` instance and call `to_png` on it.
40
+
41
+ require 'bumpspark'
42
+
43
+ graph = Bumpspark::Graph.new [12, 34, 12, 42, 12, 23]
44
+
45
+ File.open('bumpspark.png', 'wb') do |file|
46
+ file.write graph.to_png
47
+ end
48
+
49
+ ## Note on Patches/Pull Requests
50
+
51
+ Please check the TODO file for information on things that need doing.
52
+
53
+ * Fork the project.
54
+ * Make your feature addition or bug fix.
55
+ * Add tests for it. This is important so I don't break it in a
56
+ future version unintentionally.
57
+ * Commit, do not mess with rakefile, version, or history.
58
+ (if you want to have your own version, that is fine but
59
+ bump version in a commit by itself I can ignore when I pull)
60
+ * Send me a pull request. Bonus points for topic branches.
61
+
62
+ ## Copyright
63
+
64
+ Copyright (c) 2009 Bruce Williams, et al. See LICENSE for details.
65
+
66
+ [1]: http://redhanded.hobix.com/inspect/sparklinesForMinimalists.html
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "bumpspark"
8
+ gem.summary = %Q{Generates "bumpspark"-style sparklines for Ruby & Rails}
9
+ gem.description = %Q{Generates transparent PNG "bumpspark"-style sparklines. Use from Ruby directly or as a Rails helper generating an image tag w/ built-in data, as conceived by whytheluckystiff.}
10
+ gem.email = "bruce@codefluency.com"
11
+ gem.homepage = "http://github.com/bruce/bumpspark"
12
+ gem.authors = ["Bruce Williams"]
13
+ # TESTING ONLY
14
+ gem.add_development_dependency "thoughtbot-shoulda"
15
+ gem.add_development_dependency "activesupport"
16
+ gem.add_development_dependency "actionpack"
17
+ gem.add_development_dependency "rmagick"
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/*_test.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/*_test.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ if File.exist?('VERSION')
51
+ version = File.read('VERSION')
52
+ else
53
+ version = ""
54
+ end
55
+
56
+ rdoc.rdoc_dir = 'rdoc'
57
+ rdoc.title = "bumpspark #{version}"
58
+ rdoc.rdoc_files.include('README*')
59
+ rdoc.rdoc_files.include('lib/**/*.rb')
60
+ end
@@ -0,0 +1,15 @@
1
+ # Things to do
2
+
3
+ ## Useful
4
+
5
+ * Configuration of colors
6
+ * Support specific, value-based color thresholds
7
+
8
+ ## Pedantic
9
+
10
+ * More advanced normalization rules
11
+
12
+ ## Please contribute
13
+
14
+ * Support for different formats
15
+ * More in-depth tests
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.4
@@ -0,0 +1,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{bumpspark}
8
+ s.version = "1.0.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bruce Williams"]
12
+ s.date = %q{2009-08-30}
13
+ s.description = %q{Generates transparent PNG "bumpspark"-style sparklines. Use from Ruby directly or as a Rails helper generating an image tag w/ built-in data, as conceived by whytheluckystiff.}
14
+ s.email = %q{bruce@codefluency.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "TODO.markdown",
26
+ "VERSION",
27
+ "bumpspark.gemspec",
28
+ "lib/bumpspark.rb",
29
+ "lib/bumpspark/formats/png.rb",
30
+ "lib/bumpspark/graph.rb",
31
+ "lib/bumpspark_helper.rb",
32
+ "rails/init.rb",
33
+ "test/bumpspark_helper_test.rb",
34
+ "test/bumpspark_test.rb",
35
+ "test/test_helper.rb"
36
+ ]
37
+ s.has_rdoc = true
38
+ s.homepage = %q{http://github.com/bruce/bumpspark}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.2}
42
+ s.summary = %q{Generates "bumpspark"-style sparklines for Ruby & Rails}
43
+ s.test_files = [
44
+ "test/bumpspark_helper_test.rb",
45
+ "test/bumpspark_test.rb",
46
+ "test/test_helper.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ s.add_development_dependency(%q<activesupport>, [">= 0"])
56
+ s.add_development_dependency(%q<actionpack>, [">= 0"])
57
+ s.add_development_dependency(%q<rmagick>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ s.add_dependency(%q<activesupport>, [">= 0"])
61
+ s.add_dependency(%q<actionpack>, [">= 0"])
62
+ s.add_dependency(%q<rmagick>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
66
+ s.add_dependency(%q<activesupport>, [">= 0"])
67
+ s.add_dependency(%q<actionpack>, [">= 0"])
68
+ s.add_dependency(%q<rmagick>, [">= 0"])
69
+ end
70
+ end
@@ -0,0 +1,6 @@
1
+ require File.dirname(__FILE__) << "/bumpspark/formats/png"
2
+ require File.dirname(__FILE__) << "/bumpspark/graph"
3
+
4
+ module Bumpspark
5
+ VERSION = File.read(File.dirname(__FILE__) << "/../VERSION").strip
6
+ end
@@ -0,0 +1,63 @@
1
+ require 'zlib'
2
+
3
+ module Bumpspark
4
+
5
+ module Formats
6
+
7
+ module PNG
8
+
9
+ BLACK = [0, 0, 0]
10
+ RED = [0xFF, 0, 0]
11
+ GREY = [0x99, 0x99, 0x99]
12
+
13
+ HEADER = [137, 80, 78, 71, 13, 10, 26, 10].pack("C*")
14
+
15
+ # Generate a PNG
16
+ def to_png
17
+ generate_png
18
+ end
19
+
20
+ private
21
+
22
+ def rows
23
+ normalized_numbers.inject([]) { |ary, r|
24
+ ary << [BLACK] * 15 << [BLACK] * 15
25
+ ary.last[r / 9,4] = [(r > 50 and RED or GREY)] * 4
26
+ ary
27
+ }.transpose.reverse
28
+ end
29
+
30
+ def generate_png
31
+ raw_data = rows.map { |row| [0] + row }.flatten.pack("C*")
32
+ ihdr_data = [
33
+ rows.first.length,rows.length,
34
+ 8,2,0,0,0
35
+ ].pack("NNCCCCC")
36
+ ihdr = chunk("IHDR", ihdr_data)
37
+ trns = chunk("tRNS", ([ 0 ]*6).pack("C6"))
38
+ idat = chunk("IDAT", Zlib::Deflate.deflate(raw_data))
39
+ iend = chunk("IEND", "")
40
+ HEADER + ihdr + trns + idat + iend
41
+ end
42
+
43
+ def chunk(type, data)
44
+ to_check = type + data
45
+ [data.length].pack("N") + to_check + [Zlib.crc32(to_check)].pack("N")
46
+ end
47
+
48
+ def normalized_numbers
49
+ nums = numbers.empty? ? [0] : numbers
50
+ min, max = nums.min, nums.max
51
+ width = max - min
52
+ return [1] * nums.size if width == 0
53
+ width += (300 * 1000)
54
+ nums.map do |result|
55
+ ((result - min) * 100 / width.to_f).to_i
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,14 @@
1
+ module Bumpspark
2
+
3
+ class Graph
4
+
5
+ include Formats::PNG
6
+
7
+ attr_reader :numbers
8
+ def initialize(numbers)
9
+ @numbers = numbers
10
+ end
11
+
12
+ end
13
+
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'base64'
2
+
3
+ module BumpsparkHelper
4
+
5
+ # Generate a "bumpspark"-style sparkline image tag
6
+ #
7
+ # call-seq:
8
+ # <%= bumpspark_tag([20, 23, 12, 23]) %>
9
+ def bumpspark_tag(numbers, html_opts={})
10
+ graph = Bumpspark::Graph.new(numbers)
11
+ tag(:img, html_opts.merge(:src => bumpspark_tag_src(graph)))
12
+ end
13
+
14
+ def bumpspark_tag_src(graph) #:nodoc:
15
+ data = Base64.encode64(graph.to_png).delete("\n")
16
+ return "data:image/png;base64,#{CGI.escape(data)}"
17
+ end
18
+
19
+ end
@@ -0,0 +1 @@
1
+ ActionView::Base.send(:include, BumpsparkHelper)
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ require 'activesupport'
4
+ require 'action_view'
5
+ require 'action_controller'
6
+
7
+ require 'bumpspark_helper'
8
+
9
+ BumpsparkHelper.send(:include, ActionView::Helpers::TagHelper)
10
+ BumpsparkHelper.send(:include, ActionView::Helpers::AssetTagHelper)
11
+
12
+ class BumpsparkHelperTest < Test::Unit::TestCase
13
+
14
+ include BumpsparkHelper
15
+ include ActionView::Helpers::AssetTagHelper
16
+
17
+ should "generate an image" do
18
+ tag = bumpspark_tag([1, 2, 3])
19
+ assert tag.include?('<img'), tag
20
+ assert tag.include?('src'), tag
21
+ end
22
+
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+ require 'rmagick'
3
+
4
+ class BumpsparkTest < Test::Unit::TestCase
5
+
6
+ context "generating pngs" do
7
+
8
+ should "work for an empty bumpspark" do
9
+ assert_valid_png []
10
+ end
11
+
12
+ should "work for a bumpspark with one datapoint" do
13
+ assert_valid_png [1]
14
+ end
15
+
16
+ should "work for a bumpspark with multiple datapoints" do
17
+ assert_valid_png [1, 2, 3]
18
+ end
19
+
20
+ end
21
+
22
+ def assert_valid_png(data)
23
+ image = Magick::Image.from_blob(generate(data)).first
24
+ assert image, "No valid image generated"
25
+ assert_equal "image/png", image.mime_type, "Image has wrong mime type"
26
+ end
27
+
28
+ def generate(data, format=:png)
29
+ Bumpspark::Graph.new(data).send("to_#{format}")
30
+ end
31
+
32
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'bumpspark'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bumpspark
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Bruce Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-30 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: actionpack
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: rmagick
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: Generates transparent PNG "bumpspark"-style sparklines. Use from Ruby directly or as a Rails helper generating an image tag w/ built-in data, as conceived by whytheluckystiff.
56
+ email: bruce@codefluency.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.markdown
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.markdown
69
+ - Rakefile
70
+ - TODO.markdown
71
+ - VERSION
72
+ - bumpspark.gemspec
73
+ - lib/bumpspark.rb
74
+ - lib/bumpspark/formats/png.rb
75
+ - lib/bumpspark/graph.rb
76
+ - lib/bumpspark_helper.rb
77
+ - rails/init.rb
78
+ - test/bumpspark_helper_test.rb
79
+ - test/bumpspark_test.rb
80
+ - test/test_helper.rb
81
+ has_rdoc: true
82
+ homepage: http://github.com/bruce/bumpspark
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options:
87
+ - --charset=UTF-8
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.2
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Generates "bumpspark"-style sparklines for Ruby & Rails
109
+ test_files:
110
+ - test/bumpspark_helper_test.rb
111
+ - test/bumpspark_test.rb
112
+ - test/test_helper.rb