chart-topper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +126 -0
  2. data/lib/sinatra/chart_topper.rb +76 -0
  3. metadata +75 -0
@@ -0,0 +1,126 @@
1
+ # Chart Topper
2
+
3
+ Chart Topper is an extension to the [Sinatra DSL](http://github.com/sinatra/sinatra/) to allow you to quickly and dynamically create image based chart's and graphs.
4
+
5
+ # myapp.rb
6
+ require 'rubygems'
7
+ require 'sinatra'
8
+ require 'sinatra/chart_topper'
9
+
10
+ line "A graph of various fruit" do
11
+
12
+ size "400x225"
13
+
14
+ data "Apples", [1, 2, 3, 4, 4, 3]
15
+ data "Oranges", [4, 8, 7, 9, 8, 9]
16
+ data "Watermelon", [2, 3, 1, 5, 6, 8]
17
+ data "Peaches", [9, 9, 10, 8, 7, 9]
18
+
19
+ end
20
+
21
+ Install the software (for more info see the Installation section) and run with:
22
+
23
+ ruby mayapp.rb
24
+
25
+ View your graph at [http://localhost/a\_graph\_of\_various\_fruit.png](http://localhost/a_graph_of_various_fruit.png)
26
+
27
+ ## Installation
28
+
29
+ Chart Topper is built around [Gruff](http://nubyonrails.com/pages/gruff) which is in turn built around [ImageMagick and RMagick](http://rmagick.rubyforge.org/install-osx.html).
30
+
31
+ The quickest way to get all the ImageMagick dependancy installed is to use MacPorts (OS X).
32
+
33
+ sudo port install imagemagick
34
+
35
+ Then install the gem dependancies as normal:
36
+
37
+ sudo gem install gruff rmagick
38
+
39
+ ## URLs
40
+
41
+ Graph URL's are generated from the graph title. Non word characters are stripped, spaces become underscores and the whole thing is lowercased.
42
+
43
+ # Graph is addressable via /this_weeks_expenditures.png
44
+ pie "This week's expenditures" do
45
+ ...
46
+ end
47
+
48
+ ## Graph types
49
+
50
+ Various Gruff graph types are mapped to new Sinatra DSL methods, for a complete list see Sinatra::ChartTopper.GRAPH_TYPES
51
+
52
+ The basic rule is that any Gruff graph type can be generated via it's lowercased and underscore separated equivalent.
53
+
54
+ # Create a graph of type Gruff::AccumulatorBar
55
+ accumulator_bar "My accumulator bar graph" do
56
+ ...
57
+ end
58
+
59
+ ## The Gruff graph object and delegated methods
60
+
61
+ In order to make the DSL a bit nicer to use number of methods are delegated to the Gruff graph object. These can be found in Sinatra::ChartTopper.GRAPH_DELEGATIONS
62
+
63
+ dot "My dot graph" do
64
+ # Delegated to Gruff::Base.data
65
+ data "Tuesday", [2,4,6,8]
66
+ end
67
+
68
+ Direct access to the current Gruff graph object can be gained via the `graph()` method
69
+
70
+ spider "My spider chart" do
71
+ graph.theme = {
72
+ :colors => %w(orange purple green white red),
73
+ :marker_color => 'blue',
74
+ :background_colors => %w(black grey)
75
+ }
76
+ end
77
+
78
+ ## Options
79
+
80
+ You can pass through options to the graph methods. Currently the only available option is `prefix` though. This will add a prefix to the generated URL.
81
+
82
+ # /my/prefix/my_graph.png
83
+ bar "My Graph", :prefix => "/my/prefix/" do
84
+ ...
85
+ end
86
+
87
+ ## Modular style apps
88
+
89
+ You can use the extension in modular style apps too ...
90
+
91
+ require 'sinatra/base'
92
+ require 'sinatra/chart_topper'
93
+
94
+ class MyApp < Sinatra::Base
95
+
96
+ register Sinatra::ChartTopper
97
+
98
+ bar "My Graph" do
99
+ ...
100
+ end
101
+
102
+ end
103
+
104
+ ## Examples
105
+
106
+ It's probably a good place to start, check out the examples directory.
107
+
108
+ ## License
109
+
110
+ License:
111
+
112
+ (GPLv3)
113
+
114
+ Copyright (C) 2009 Matt Haynes
115
+
116
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
117
+
118
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
119
+
120
+ You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>
121
+
122
+
123
+
124
+
125
+
126
+
@@ -0,0 +1,76 @@
1
+ require 'sinatra/base'
2
+ require "gruff"
3
+
4
+ module Sinatra
5
+
6
+ module ChartTopper
7
+
8
+ # These graph types are available as top level methods and proxied through to draw()
9
+ # e.g. graph_type() -> draw(Gruff::GraphType)
10
+ GRAPH_TYPES = [:accumulator_bar, :area, :bar, :bar_conversion, :dot, :line,
11
+ :pie, :side_stacked_bar, :side_bar, :spider, :stacked_area, :stacked_bar]
12
+
13
+ GRAPH_TYPES.each do |method|
14
+ define_method(method) do |*args, &block|
15
+ title, options = *args
16
+ options ||= {}
17
+ camelized = method.to_s.split('_').map {|w| w.capitalize}.join
18
+ draw Gruff.const_get(camelized), title, options, &block
19
+ end
20
+ end
21
+
22
+ # Delegate(?) these methods to the graph
23
+ GRAPH_DELEGATIONS = [:data, :add_color, :theme_37signals, :theme_greyscale,
24
+ :theme_keynote, :theme_odeo, :theme_pastel, :theme_rails_keynote]
25
+
26
+ GRAPH_DELEGATIONS.each do |method|
27
+ define_method method do |*args|
28
+ graph.send(method, *args)
29
+ end
30
+ end
31
+
32
+ # Make filename from title
33
+ def filename(title)
34
+ title.gsub(/[^a-zA-Z0-9_\s]/, '').gsub(/\s/, '_').downcase
35
+ end
36
+
37
+ # Create graph of size (reset's any previous graph!)
38
+ def size(dimensions)
39
+ type = graph.class
40
+ set_graph type.new(dimensions)
41
+ end
42
+
43
+ # Create / return graph object
44
+ def graph(size = nil)
45
+ @graph
46
+ end
47
+
48
+ # Set the graph object
49
+ def set_graph(graph)
50
+ @graph = graph
51
+ end
52
+
53
+ def self.registered(app)
54
+ app.helpers Sinatra::ChartTopper
55
+ end
56
+
57
+ # Define a route to draw a particualar graph
58
+ def draw(type, title, options = {}, &block)
59
+
60
+ prefix = options[:prefix] || "/"
61
+
62
+ get "#{prefix}#{filename(title)}.png" do
63
+ content_type "image/png"
64
+ set_graph type.new
65
+ instance_eval(&block)
66
+ graph.title = title
67
+ graph.to_blob
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ register ChartTopper
75
+
76
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chart-topper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Haynes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-06-15 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
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: gruff
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Chart Topper is an extension to the Sinatra DSL to allow you to quickly and dynamically create image based chart's and graphs.
36
+ email: matt@matthaynes.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/sinatra/chart_topper.rb
45
+ - README.md
46
+ has_rdoc: true
47
+ homepage: http://github.com/matth/chart-topper/tree/master
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: A little Sinatra DSL extension to help make dynamic graphs with Gruff
74
+ test_files: []
75
+