ramhoj-scruffy 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/README.txt +66 -0
  2. data/lib/scruffy.rb +25 -0
  3. data/lib/scruffy/components.rb +21 -0
  4. data/lib/scruffy/components/background.rb +24 -0
  5. data/lib/scruffy/components/base.rb +57 -0
  6. data/lib/scruffy/components/data_markers.rb +26 -0
  7. data/lib/scruffy/components/graphs.rb +48 -0
  8. data/lib/scruffy/components/grid.rb +19 -0
  9. data/lib/scruffy/components/label.rb +17 -0
  10. data/lib/scruffy/components/legend.rb +105 -0
  11. data/lib/scruffy/components/style_info.rb +22 -0
  12. data/lib/scruffy/components/title.rb +19 -0
  13. data/lib/scruffy/components/value_markers.rb +32 -0
  14. data/lib/scruffy/components/viewport.rb +37 -0
  15. data/lib/scruffy/formatters.rb +213 -0
  16. data/lib/scruffy/graph.rb +190 -0
  17. data/lib/scruffy/graph_state.rb +24 -0
  18. data/lib/scruffy/helpers.rb +12 -0
  19. data/lib/scruffy/helpers/canvas.rb +41 -0
  20. data/lib/scruffy/helpers/layer_container.rb +95 -0
  21. data/lib/scruffy/helpers/meta.rb +5 -0
  22. data/lib/scruffy/helpers/point_container.rb +70 -0
  23. data/lib/scruffy/layers.rb +24 -0
  24. data/lib/scruffy/layers/all_smiles.rb +137 -0
  25. data/lib/scruffy/layers/area.rb +46 -0
  26. data/lib/scruffy/layers/average.rb +67 -0
  27. data/lib/scruffy/layers/bar.rb +52 -0
  28. data/lib/scruffy/layers/base.rb +191 -0
  29. data/lib/scruffy/layers/line.rb +46 -0
  30. data/lib/scruffy/layers/pie.rb +123 -0
  31. data/lib/scruffy/layers/pie_slice.rb +119 -0
  32. data/lib/scruffy/layers/scatter.rb +21 -0
  33. data/lib/scruffy/layers/sparkline_bar.rb +39 -0
  34. data/lib/scruffy/layers/stacked.rb +87 -0
  35. data/lib/scruffy/rasterizers.rb +14 -0
  36. data/lib/scruffy/rasterizers/batik_rasterizer.rb +39 -0
  37. data/lib/scruffy/rasterizers/rmagick_rasterizer.rb +27 -0
  38. data/lib/scruffy/renderers.rb +22 -0
  39. data/lib/scruffy/renderers/base.rb +93 -0
  40. data/lib/scruffy/renderers/cubed.rb +44 -0
  41. data/lib/scruffy/renderers/cubed3d.rb +53 -0
  42. data/lib/scruffy/renderers/empty.rb +22 -0
  43. data/lib/scruffy/renderers/pie.rb +20 -0
  44. data/lib/scruffy/renderers/reversed.rb +17 -0
  45. data/lib/scruffy/renderers/sparkline.rb +10 -0
  46. data/lib/scruffy/renderers/split.rb +48 -0
  47. data/lib/scruffy/renderers/standard.rb +36 -0
  48. data/lib/scruffy/themes.rb +156 -0
  49. data/lib/scruffy/version.rb +3 -0
  50. data/spec/output/array.svg +47 -0
  51. data/spec/output/hash.svg +47 -0
  52. data/spec/scruffy/graph_spec.rb +175 -0
  53. data/spec/scruffy/layers/base_spec.rb +30 -0
  54. data/spec/spec_helper.rb +8 -0
  55. metadata +155 -0
@@ -0,0 +1,47 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <?DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" type=""?>
3
+ <svg xmlns:xlink="http://www.w3.org/1999/xlink" width="600" height="360" xmlns="http://www.w3.org/2000/svg">
4
+ <g id="scruffy_graph">
5
+ <g transform="translate(0.0, 0.0)" id="background">
6
+ <defs>
7
+ <linearGradient x1="0%" y1="0%" x2="0%" y2="100%" id="BackgroundGradient">
8
+ <stop offset="5%" stop-color="#FFFFFF"/>
9
+ <stop offset="95%" stop-color="#FFFFFF"/>
10
+ </linearGradient>
11
+ </defs>
12
+ <rect fill="url(#BackgroundGradient)" width="600.0" height="360.0" x="0" y="0"/>
13
+ </g>
14
+ <g transform="translate(30.0, 7.2)" id="title">
15
+ <text fill="#999999" text-anchor="middle" class="title" font-size="25.2" x="270.0" font-family="" y="25.2" stroke-width="0" stroke="none">Graph</text>
16
+ </g>
17
+ <g transform="translate(12.0, 93.6)" id="view">
18
+ <g>
19
+ <g transform="translate(0.0, 4.752)" id="values">
20
+ <text fill="#999999" text-anchor="end" font-size="16.91712" x="96.12" font-family="" y="211.464">1</text>
21
+ <text fill="#999999" text-anchor="end" font-size="16.91712" x="96.12" font-family="" y="158.598">1</text>
22
+ <text fill="#999999" text-anchor="end" font-size="16.91712" x="96.12" font-family="" y="105.732">2</text>
23
+ <text fill="#999999" text-anchor="end" font-size="16.91712" x="96.12" font-family="" y="52.866">3</text>
24
+ <text fill="#999999" text-anchor="end" font-size="16.91712" x="96.12" font-family="" y="0.0">4</text>
25
+ </g>
26
+ <g transform="translate(106.8, 0.0)" id="grid">
27
+ <line x1="0" y1="0.0" x2="427.2" style="stroke: #999999; stroke-width: 1;" y2="0.0"/>
28
+ <line x1="0" y1="52.866" x2="427.2" style="stroke: #999999; stroke-width: 1;" y2="52.866"/>
29
+ <line x1="0" y1="105.732" x2="427.2" style="stroke: #999999; stroke-width: 1;" y2="105.732"/>
30
+ <line x1="0" y1="158.598" x2="427.2" style="stroke: #999999; stroke-width: 1;" y2="158.598"/>
31
+ <line x1="0" y1="211.464" x2="427.2" style="stroke: #999999; stroke-width: 1;" y2="211.464"/>
32
+ </g>
33
+ <g transform="translate(106.8, 218.592)" id="labels">
34
+ </g>
35
+ <g transform="translate(106.8, 0.0)" id="graphs">
36
+ <g class="graph_layer" id="component_graphs_graph_0">
37
+ <polyline fill="none" style="" stroke-width="1" stroke="#1919B3" points="0.0,183.881739130435 142.4,122.587826086957 427.2,0.0"/>
38
+ </g>
39
+ </g>
40
+ </g>
41
+ </g>
42
+ <g transform="translate(30.0, 46.8)" id="legend">
43
+ <rect fill="#1919B3" width="10.8" height="10.8" x="243.0" y="0"/>
44
+ <text fill="#999999" font-size="15.552" x="262.44" font-family="" y="11.664" style="color: #999999">Data</text>
45
+ </g>
46
+ </g>
47
+ </svg>
@@ -0,0 +1,175 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require 'scruffy'
3
+ require 'fileutils'
4
+
5
+ module CustomRenderers
6
+ def blank_renderer
7
+ object = Object.new
8
+ object.instance_eval { class << self;self;end }.instance_eval { define_method :render do |options|
9
+ puts "Rendering"
10
+ end
11
+ }
12
+ object
13
+ end
14
+ end
15
+
16
+ describe "A new Scruffy::Graph" do
17
+ include CustomRenderers
18
+
19
+ before do
20
+ @graph = Scruffy::Graph.new
21
+ end
22
+
23
+ it "should have a blank title" do
24
+ @graph.title.should be_nil
25
+ end
26
+
27
+ it "should be set to the keynote theme" do
28
+ @graph.theme.class.should == Scruffy::Themes::Standard
29
+ end
30
+
31
+ it "should have zero layers" do
32
+ @graph.layers.should be_empty
33
+ end
34
+
35
+ it "should not have a default layer type" do
36
+ @graph.default_type.should be_nil
37
+ end
38
+
39
+ it "should not have any point markers (x-axis values)" do
40
+ @graph.point_markers.should be_nil
41
+ end
42
+
43
+ it "should format values as numbers by default" do
44
+ @graph.value_formatter.should be_instance_of(Scruffy::Formatters::Number)
45
+ end
46
+
47
+ it "should use a StandardRenderer" do
48
+ @graph.renderer.should_be be_instance_of(Scruffy::Renderers::Standard)
49
+ end
50
+
51
+ it "should accept a new title" do
52
+ @graph.title = "New Title"
53
+ @graph.title.should == "New Title"
54
+ end
55
+
56
+ it "should accept a new theme" do
57
+ @graph.theme = Scruffy::Themes::Mephisto
58
+ @graph.theme.should equal(Scruffy::Themes::Mephisto)
59
+ end
60
+
61
+ it "should accept a new default type" do
62
+ @graph.default_type = :line
63
+ @graph.default_type.should equal(:line)
64
+ end
65
+
66
+ it "should accept new point markers" do
67
+ markers = ['Jan', 'Feb', 'Mar']
68
+ @graph.point_markers = markers
69
+ @graph.point_markers.should equal(markers)
70
+ end
71
+
72
+ it "should accept a new renderer" do
73
+ renderer = blank_renderer
74
+ @graph.renderer = renderer
75
+ @graph.renderer.should equal(renderer)
76
+ end
77
+
78
+ it "should not accept renderers with missing #render methods" do
79
+ lambda { @graph.renderer = 1 }.should raise_error(ArgumentError)
80
+ end
81
+ end
82
+
83
+ describe "A Scruffy::Graph's initialization block" do
84
+ include CustomRenderers
85
+
86
+ it "should accept just a default_type Symbol" do
87
+ lambda { Scruffy::Graph.new(:line) }.should_not raise_error
88
+ end
89
+
90
+ it "should accept just an options hash" do
91
+ lambda { Scruffy::Graph.new({:title => "My Title"}) }.should_not raise_error
92
+ lambda { Scruffy::Graph.new(:title => "My Title", :theme => Scruffy::Themes::Keynote) }.should_not raise_error
93
+ end
94
+
95
+ it "should accept both a default_type and options hash" do
96
+ lambda {
97
+ Scruffy::Graph.new(:line, {:title => "My Title"})
98
+ Scruffy::Graph.new(:line, :title => "My Title")
99
+ }.should_not raise_error
100
+ end
101
+
102
+ it "should reject any invalid argument combination" do
103
+ lambda { Scruffy::Graph.new({:title => "My Title"}, :line) }.should raise_error(ArgumentError)
104
+ lambda { Scruffy::Graph.new(:line, {:title => "My Title"}, "Any additional arguments.") }.should raise_error(ArgumentError)
105
+ lambda { Scruffy::Graph.new(:line, "Any additional arguments.") }.should raise_error(ArgumentError)
106
+ end
107
+
108
+ it "should reject any options that are not supported" do
109
+ lambda { Scruffy::Graph.new(:title => "My Title", :some_key => "Some Value") }.should raise_error(ArgumentError)
110
+ end
111
+
112
+ it "should successfully save all valid options" do
113
+ options = {:title => "My Title",
114
+ :theme => {:background => [:black],
115
+ :colors => [:red => 'red', :yellow => 'yellow']},
116
+ :layers => [ Scruffy::Layers::Line.new(:points => [100, 200, 300]) ],
117
+ :default_type => :average,
118
+ :value_formatter => Scruffy::Formatters::Currency.new,
119
+ :point_markers => ['One Hundred', 'Two Hundred', 'Three Hundred']}
120
+
121
+ @graph = Scruffy::Graph.new(options)
122
+
123
+ @graph.title.should == options[:title]
124
+ @graph.theme.should == options[:theme]
125
+ @graph.layers.should == options[:layers]
126
+ @graph.default_type.should == options[:default_type]
127
+ @graph.value_formatter.should == options[:value_formatter]
128
+ @graph.point_markers.should == options[:point_markers]
129
+ end
130
+ end
131
+
132
+
133
+ context "A fully populated Graph" do
134
+ before do
135
+ FileUtils.rm_f File.dirname(__FILE__) + '/*.png'
136
+ FileUtils.rm_f File.dirname(__FILE__) + '/*.jpg'
137
+
138
+ @graph = Scruffy::Graph.new :title => 'Test Graph'
139
+ @graph << Scruffy::Layers::Average.new(:title => 'Average', :points => @graph.layers)
140
+ @graph.layers.first.relevant_data = false
141
+ @graph << Scruffy::Layers::AllSmiles.new(:title => 'Smiles', :points => [100, 200, 300])
142
+ @graph << Scruffy::Layers::Area.new(:title => 'Area', :points => [100, 200, 300])
143
+ @graph << Scruffy::Layers::Bar.new(:title => 'Bar', :points => [100, 200, 300])
144
+ @graph << Scruffy::Layers::Line.new(:title => 'Line', :points => [100, 200, 300])
145
+ end
146
+
147
+ it "should render to SVG" do
148
+ @graph.render(:width => 800).should be_instance_of(String)
149
+ end
150
+
151
+ it "should rasterize to PNG" do
152
+ lambda {
153
+ #@graph.render(:width => 800, :as => 'PNG', :to => outfile('test_graph.png'))
154
+ }.should_not raise_error
155
+ end
156
+
157
+ it "should rasterize to JPG" do
158
+ lambda {
159
+ #@graph.render(:width => 800, :as => 'JPG', :to => outfile('test_graph.jpg'))
160
+ }.should_not raise_error
161
+ end
162
+ end
163
+
164
+
165
+ context "A graph with hash data" do
166
+ specify "should render identically to a graph with array data" do
167
+ @hashgraph = Scruffy::Graph.new :title => 'Graph'
168
+ @hashgraph.add(:line, 'Data', { 0 => 1, 1 => 2, 3 => 4 })
169
+
170
+ @arraygraph = Scruffy::Graph.new :title => 'Graph'
171
+ @arraygraph.add(:line, 'Data', [1, 2, nil, 4])
172
+
173
+ @hashgraph.render(:to => outfile('hash.svg')).should == @arraygraph.render(:to => outfile('array.svg'))
174
+ end
175
+ end
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
2
+ require 'scruffy'
3
+
4
+ context "Any layer" do
5
+ specify "should accept but ignore nil data" do
6
+ render([100, nil, 300]).should eql([[0.0, 400], [400.0, 0.0]])
7
+ end
8
+
9
+ specify "should accept hash data as sequentially-keyed data" do
10
+ render([100, nil, 300]).should eql([[0.0, 400], [400.0, 0.0]])
11
+ render({0 => 100, 2 => 300}).should eql([[0.0, 400], [400.0, 0.0]])
12
+ end
13
+
14
+ def render points
15
+ layer = Scruffy::Layers::Base.new(:title => 'My base Layer', :points => points)
16
+
17
+ #stub out draw to just return coords
18
+ class << layer; def draw(svg, coords, options={}); coords end; end;
19
+
20
+ coords = []
21
+
22
+ lambda {
23
+ size = [400,400]
24
+ coords = layer.render(Scruffy::Renderers::Base.new.render(:layers => [layer], :size => size),
25
+ :size => size, :min_value => 100, :max_value => 300)
26
+ }.should_not raise_error
27
+
28
+ coords
29
+ end
30
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ def outfile( name )
4
+ output_dir = File.join(File.dirname(__FILE__), "output")
5
+
6
+ FileUtils.mkdir( output_dir ) unless File.exist?( output_dir )
7
+ File.join( output_dir, name )
8
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ramhoj-scruffy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 6
9
+ version: 0.2.6
10
+ platform: ruby
11
+ authors:
12
+ - Jonas Nicklas
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-03 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: builder
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ version: "2.0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rmagick
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 2
42
+ - 0
43
+ version: "2.0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 1
55
+ - 1
56
+ version: "1.1"
57
+ type: :development
58
+ version_requirements: *id003
59
+ description: Scruffy is a library for outputting graphs to image or SVG
60
+ email:
61
+ - jonas.nicklas@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - README.txt
68
+ files:
69
+ - lib/scruffy/components/background.rb
70
+ - lib/scruffy/components/base.rb
71
+ - lib/scruffy/components/data_markers.rb
72
+ - lib/scruffy/components/graphs.rb
73
+ - lib/scruffy/components/grid.rb
74
+ - lib/scruffy/components/label.rb
75
+ - lib/scruffy/components/legend.rb
76
+ - lib/scruffy/components/style_info.rb
77
+ - lib/scruffy/components/title.rb
78
+ - lib/scruffy/components/value_markers.rb
79
+ - lib/scruffy/components/viewport.rb
80
+ - lib/scruffy/components.rb
81
+ - lib/scruffy/formatters.rb
82
+ - lib/scruffy/graph.rb
83
+ - lib/scruffy/graph_state.rb
84
+ - lib/scruffy/helpers/canvas.rb
85
+ - lib/scruffy/helpers/layer_container.rb
86
+ - lib/scruffy/helpers/meta.rb
87
+ - lib/scruffy/helpers/point_container.rb
88
+ - lib/scruffy/helpers.rb
89
+ - lib/scruffy/layers/all_smiles.rb
90
+ - lib/scruffy/layers/area.rb
91
+ - lib/scruffy/layers/average.rb
92
+ - lib/scruffy/layers/bar.rb
93
+ - lib/scruffy/layers/base.rb
94
+ - lib/scruffy/layers/line.rb
95
+ - lib/scruffy/layers/pie.rb
96
+ - lib/scruffy/layers/pie_slice.rb
97
+ - lib/scruffy/layers/scatter.rb
98
+ - lib/scruffy/layers/sparkline_bar.rb
99
+ - lib/scruffy/layers/stacked.rb
100
+ - lib/scruffy/layers.rb
101
+ - lib/scruffy/rasterizers/batik_rasterizer.rb
102
+ - lib/scruffy/rasterizers/rmagick_rasterizer.rb
103
+ - lib/scruffy/rasterizers.rb
104
+ - lib/scruffy/renderers/base.rb
105
+ - lib/scruffy/renderers/cubed.rb
106
+ - lib/scruffy/renderers/cubed3d.rb
107
+ - lib/scruffy/renderers/empty.rb
108
+ - lib/scruffy/renderers/pie.rb
109
+ - lib/scruffy/renderers/reversed.rb
110
+ - lib/scruffy/renderers/sparkline.rb
111
+ - lib/scruffy/renderers/split.rb
112
+ - lib/scruffy/renderers/standard.rb
113
+ - lib/scruffy/renderers.rb
114
+ - lib/scruffy/themes.rb
115
+ - lib/scruffy/version.rb
116
+ - lib/scruffy.rb
117
+ - spec/output/array.svg
118
+ - spec/output/hash.svg
119
+ - spec/scruffy/graph_spec.rb
120
+ - spec/scruffy/layers/base_spec.rb
121
+ - spec/spec_helper.rb
122
+ - README.txt
123
+ has_rdoc: true
124
+ homepage: http://github.com/jnicklas/scruffy
125
+ licenses: []
126
+
127
+ post_install_message:
128
+ rdoc_options:
129
+ - --main
130
+ - README.txt
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ segments:
145
+ - 0
146
+ version: "0"
147
+ requirements: []
148
+
149
+ rubyforge_project: scruffy
150
+ rubygems_version: 1.3.6
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: Scruffy is a library for outputting graphs to image or SVG
154
+ test_files: []
155
+