srawlins-scruffy 0.2.9

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.
Files changed (56) hide show
  1. data/CHANGES.txt +115 -0
  2. data/LICENCE.txt +20 -0
  3. data/Manifest.txt +74 -0
  4. data/README.txt +66 -0
  5. data/lib/scruffy.rb +30 -0
  6. data/lib/scruffy/components.rb +21 -0
  7. data/lib/scruffy/components/background.rb +24 -0
  8. data/lib/scruffy/components/base.rb +57 -0
  9. data/lib/scruffy/components/data_markers.rb +42 -0
  10. data/lib/scruffy/components/graphs.rb +51 -0
  11. data/lib/scruffy/components/grid.rb +54 -0
  12. data/lib/scruffy/components/label.rb +17 -0
  13. data/lib/scruffy/components/legend.rb +147 -0
  14. data/lib/scruffy/components/style_info.rb +22 -0
  15. data/lib/scruffy/components/title.rb +19 -0
  16. data/lib/scruffy/components/value_markers.rb +25 -0
  17. data/lib/scruffy/components/viewport.rb +37 -0
  18. data/lib/scruffy/formatters.rb +230 -0
  19. data/lib/scruffy/graph.rb +205 -0
  20. data/lib/scruffy/graph_state.rb +29 -0
  21. data/lib/scruffy/helpers.rb +13 -0
  22. data/lib/scruffy/helpers/canvas.rb +41 -0
  23. data/lib/scruffy/helpers/layer_container.rb +119 -0
  24. data/lib/scruffy/helpers/marker_helper.rb +25 -0
  25. data/lib/scruffy/helpers/meta.rb +5 -0
  26. data/lib/scruffy/helpers/point_container.rb +99 -0
  27. data/lib/scruffy/layers.rb +28 -0
  28. data/lib/scruffy/layers/all_smiles.rb +137 -0
  29. data/lib/scruffy/layers/area.rb +46 -0
  30. data/lib/scruffy/layers/average.rb +67 -0
  31. data/lib/scruffy/layers/bar.rb +68 -0
  32. data/lib/scruffy/layers/base.rb +211 -0
  33. data/lib/scruffy/layers/line.rb +46 -0
  34. data/lib/scruffy/layers/pie.rb +123 -0
  35. data/lib/scruffy/layers/pie_slice.rb +119 -0
  36. data/lib/scruffy/layers/scatter.rb +29 -0
  37. data/lib/scruffy/layers/sparkline_bar.rb +39 -0
  38. data/lib/scruffy/layers/stacked.rb +87 -0
  39. data/lib/scruffy/rasterizers.rb +14 -0
  40. data/lib/scruffy/rasterizers/batik_rasterizer.rb +39 -0
  41. data/lib/scruffy/rasterizers/rmagick_rasterizer.rb +27 -0
  42. data/lib/scruffy/renderers.rb +24 -0
  43. data/lib/scruffy/renderers/base.rb +95 -0
  44. data/lib/scruffy/renderers/cubed.rb +44 -0
  45. data/lib/scruffy/renderers/cubed3d.rb +53 -0
  46. data/lib/scruffy/renderers/empty.rb +22 -0
  47. data/lib/scruffy/renderers/pie.rb +20 -0
  48. data/lib/scruffy/renderers/reversed.rb +17 -0
  49. data/lib/scruffy/renderers/sparkline.rb +10 -0
  50. data/lib/scruffy/renderers/split.rb +48 -0
  51. data/lib/scruffy/renderers/standard.rb +37 -0
  52. data/lib/scruffy/themes.rb +175 -0
  53. data/lib/scruffy/version.rb +9 -0
  54. data/test/graph_creation_test.rb +286 -0
  55. data/test/test_helper.rb +2 -0
  56. metadata +132 -0
@@ -0,0 +1,14 @@
1
+ # ===Scruffy Rasterizers
2
+ #
3
+ # Author:: Brasten Sager
4
+ # Date:: August 10th, 2006
5
+ #
6
+ # These handle the job of rasterizing SVG images to other image formats.
7
+ # At the moment, only RMagickRasterizer exists, but others may soon follow.
8
+ #
9
+ # I'm somewhat interesting in finding a way to integrate Apache Batik, as it's
10
+ # SVG rendering seems to be superior to ImageMagick's.
11
+ module Scruffy::Rasterizers; end
12
+
13
+ require 'scruffy/rasterizers/rmagick_rasterizer.rb'
14
+ require 'scruffy/rasterizers/batik_rasterizer.rb'
@@ -0,0 +1,39 @@
1
+ module Scruffy::Rasterizers
2
+ # == Scruffy::Rasterizers::BatikRasterizer
3
+ #
4
+ # Author:: Brasten Sager
5
+ # Date:: August 14th, 2006
6
+ #
7
+ # Purely experimental. Can be used to rasterize SVG graphs with
8
+ # Apache Batik.
9
+ class BatikRasterizer
10
+ # Returns new BatikRasterizer.
11
+ #
12
+ # Options:
13
+ # command:: Command needed to execute Batik. (ie: 'java -classpath {...}')
14
+ # temp_folder:: Folder for storing temporary files being passed between Scruffy and Batik.
15
+ def initialize(options={})
16
+ @command = options[:command]
17
+ @temp_folder = options[:temp_folder]
18
+ end
19
+
20
+ # Rasterize graph.
21
+ #
22
+ # Options:
23
+ # as:: Image format to generate (PNG, JPG, et al.)
24
+ def rasterize(svg, options={})
25
+ File.open(@temp_folder + '/temp_svg.svg', 'w') { |file|
26
+ file.write(svg)
27
+ }
28
+
29
+ `#{@command} -d #{@temp_folder} -m image/#{options[:as].downcase} #{@temp_folder}/temp_svg.svg`
30
+
31
+ image = ""
32
+ File.open(@temp_folder + '/temp_svg.' + options[:as].downcase, 'r') { |file|
33
+ image = file.read
34
+ }
35
+
36
+ image
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ module Scruffy::Rasterizers
2
+
3
+ # == RMagickRasterizer
4
+ #
5
+ # Author:: Brasten Sager
6
+ # Date:: August 14th, 2006
7
+ #
8
+ # The RMagickRasterizer converts SVG graphs to images using ImageMagick.
9
+ class RMagickRasterizer
10
+ def rasterize(svg, options={})
11
+ # I know this seems weird, I'm open to suggestions.
12
+ # I didn't want RMagick required unless absolutely necessary.
13
+ require 'RMagick'
14
+
15
+ image = Magick::Image::from_blob(svg)[0]
16
+
17
+ # Removed for now
18
+ # image.resize!(options[:size][0], options[:size][1], Magick::BoxFilter, 1.25) if options[:actual_size]
19
+
20
+ if options[:to]
21
+ image.write(options[:to]) { self.format = options[:as] }
22
+ end
23
+
24
+ image.to_blob { self.format = options[:as] }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ # ===Scruffy Renderers
2
+ #
3
+ # Author:: Brasten Sager
4
+ # Date:: August 14th, 2006
5
+ #
6
+ # Renderers piece the entire graph together from a collection
7
+ # of components. Creating new renderers allows you to create
8
+ # entirely new layouts for your graphs.
9
+ #
10
+ # Scruffy::Renderers::Base contains the basic functionality needed
11
+ # by a layout. The easiest way to create a new layout is by subclassing
12
+ # Base.
13
+ module Scruffy::Renderers; end
14
+
15
+ require 'scruffy/renderers/base'
16
+ require 'scruffy/renderers/empty'
17
+ require 'scruffy/renderers/standard'
18
+ require 'scruffy/renderers/axis_legend'
19
+ require 'scruffy/renderers/legend_to_right'
20
+ require 'scruffy/renderers/reversed'
21
+ require 'scruffy/renderers/cubed'
22
+ require 'scruffy/renderers/split'
23
+ require 'scruffy/renderers/cubed3d'
24
+ require 'scruffy/renderers/pie'
@@ -0,0 +1,95 @@
1
+ require 'builder'
2
+ module Scruffy::Renderers
3
+ # ===Scruffy::Renderers::Base
4
+ #
5
+ # Author:: Brasten Sager
6
+ # Date:: August 14th, 2006
7
+ #
8
+ # Provides all the base functionality needed to render a graph, but
9
+ # does not provide a default layout.
10
+ #
11
+ # For a basic layout, see Scruffy::Renderers::Standard.
12
+ class Base
13
+ include Scruffy::Helpers::Canvas
14
+
15
+ attr_accessor :options
16
+
17
+ def initialize(options = {})
18
+ self.components = []
19
+ self.options = options
20
+ define_layout
21
+ end
22
+
23
+ # Renders the graph and all components.
24
+ def render(options = {})
25
+ options[:graph_id] ||= 'scruffy_graph'
26
+ options[:complexity] ||= (global_complexity || :normal)
27
+
28
+ # Allow subclasses to muck with components prior to renders.
29
+ rendertime_renderer = self.clone
30
+ rendertime_renderer.instance_eval { before_render if respond_to?(:before_render) }
31
+
32
+ svg = Builder::XmlMarkup.new(:indent => 2)
33
+ unless options[:no_doctype_header]
34
+ svg.instruct!
35
+ svg.instruct! 'DOCTYPE', 'svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" type'
36
+ end
37
+ svg.svg(:xmlns => "http://www.w3.org/2000/svg", 'xmlns:xlink' => "http://www.w3.org/1999/xlink", :width => options[:size].first, :height => options[:size].last) {
38
+ svg.g(:id => options[:graph_id]) {
39
+ rendertime_renderer.components.each do |component|
40
+ component.render(svg,
41
+ bounds_for( options[:size], component.position, component.size ),
42
+ options)
43
+ end
44
+ }
45
+ }
46
+ svg.target!
47
+ end
48
+
49
+ def before_render
50
+ if self.options
51
+ set_values(self.options[:values]) if (self.options[:values] && self.options[:values] != :hide)
52
+ hide_grid if (self.options[:grid] == :hide)
53
+ hide_values if (self.options[:values] == :hide)
54
+ hide_labels if (self.options[:labels] == :hide)
55
+ end
56
+ end
57
+
58
+ def method_missing(sym, *args)
59
+ self.options = {} if self.options.nil?
60
+
61
+ if args.size > 0
62
+ self.options[sym] = args[0]
63
+ else
64
+ return self.options[sym]
65
+ end
66
+ end
67
+
68
+ protected
69
+ def hide_grid
70
+ grids.each { |grid| grid.visible = false }
71
+ end
72
+
73
+ def set_values(val)
74
+ values.each { |value| value.markers = val }
75
+ grids.each { |grid| grid.markers = val }
76
+ end
77
+
78
+ def hide_values
79
+ values.each { |value| value.visible = false }
80
+ end
81
+
82
+ def hide_labels
83
+ labels.each { |label| label.visible = false }
84
+ end
85
+
86
+ private
87
+ def global_complexity
88
+ if Kernel.const_defined? "SCRUFFY_COMPLEXITY"
89
+ SCRUFFY_COMPLEXITY
90
+ else
91
+ nil
92
+ end
93
+ end
94
+ end # base
95
+ end
@@ -0,0 +1,44 @@
1
+ module Scruffy::Renderers
2
+ # ===Scruffy::Renderers::Cubed
3
+ #
4
+ # Author:: Brasten Sager
5
+ # Date:: August 14th, 2006
6
+ #
7
+ # Graph layout consisting of four separate graphs arranged in a 2x2 grid.
8
+ class Cubed < Empty
9
+ VIEWPORT_SIZE = [35, 30]
10
+ VIEWPORTS = { :top_left => [10, 25],
11
+ :top_right => [55, 25],
12
+ :bottom_left => [10, 65],
13
+ :bottom_right => [55, 65] }
14
+
15
+ # Returns a Cubed instance.
16
+ def define_layout
17
+ super do |components|
18
+ components << Scruffy::Components::Title.new(:title, :position => [5, 2], :size => [90, 7])
19
+
20
+ VIEWPORTS.each_pair do |category, position|
21
+ components << Scruffy::Components::Viewport.new(category, :position => position,
22
+ :size => VIEWPORT_SIZE, &graph_block(category))
23
+ end
24
+
25
+ components << Scruffy::Components::Legend.new(:legend, :position => [5, 13], :size => [90, 5])
26
+ end
27
+ end
28
+
29
+ private
30
+ # Returns a typical graph layout.
31
+ #
32
+ # These are squeezed into viewports.
33
+ def graph_block(graph_filter)
34
+ block = Proc.new { |components|
35
+ components << Scruffy::Components::Grid.new(:grid, :position => [10, 0], :size => [90, 89])
36
+ components << Scruffy::Components::ValueMarkers.new(:value_markers, :position => [0, 2], :size => [8, 89])
37
+ components << Scruffy::Components::DataMarkers.new(:data_markers, :position => [10, 92], :size => [90, 8])
38
+ components << Scruffy::Components::Graphs.new(:graphs, :position => [10, 0], :size => [90, 89], :only => graph_filter)
39
+ }
40
+
41
+ block
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,53 @@
1
+ module Scruffy::Renderers
2
+ # ===Scruffy::Renderers::Cubed3d
3
+ #
4
+ # Author:: Brasten Sager
5
+ # Date:: August 17th, 2006
6
+ #
7
+ # A 3-dimensional cube effect.
8
+ class Cubed3d < Empty
9
+ VIEWPORT_SIZE = [25, 45]
10
+ VIEWPORTS = { :top_left => [10, 25],
11
+ :top_right => [55, 25],
12
+ :bottom_left => [10, 65],
13
+ :bottom_right => [55, 65] }
14
+
15
+ # Returns a Cubed instance.
16
+ def define_layout
17
+ super do |components|
18
+ components << Scruffy::Components::Title.new(:title, :position => [5, 2], :size => [90, 7])
19
+
20
+ components << Scruffy::Components::Viewport.new(:one, :position => [10, 50],
21
+ :size => VIEWPORT_SIZE, :skewY => '-25',
22
+ &graph_block(:one))
23
+ components << Scruffy::Components::Viewport.new(:two, :position => [30, 50],
24
+ :size => VIEWPORT_SIZE, :skewY => '-25',
25
+ &graph_block(:two))
26
+ components << Scruffy::Components::Viewport.new(:three, :position => [50, 50],
27
+ :size => VIEWPORT_SIZE, :skewY => '-25',
28
+ &graph_block(:three))
29
+ components << Scruffy::Components::Viewport.new(:four, :position => [70, 50],
30
+ :size => VIEWPORT_SIZE, :skewY => '-25',
31
+ &graph_block(:four))
32
+
33
+
34
+ components << Scruffy::Components::Legend.new(:legend, :position => [5, 13], :size => [90, 5])
35
+ end
36
+ end
37
+
38
+ private
39
+ # Returns a typical graph layout.
40
+ #
41
+ # These are squeezed into viewports.
42
+ def graph_block(graph_filter)
43
+ block = Proc.new { |components|
44
+ components << Scruffy::Components::Grid.new(:grid, :position => [10, 0], :size => [90, 89])
45
+ components << Scruffy::Components::ValueMarkers.new(:value_markers, :position => [0, 2], :size => [8, 89])
46
+ components << Scruffy::Components::DataMarkers.new(:data_markers, :position => [10, 92], :size => [90, 8])
47
+ components << Scruffy::Components::Graphs.new(:graphs, :position => [10, 0], :size => [90, 89], :only => graph_filter)
48
+ }
49
+
50
+ block
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,22 @@
1
+ module Scruffy::Renderers
2
+ # ===Scruffy::Renderers::Empty
3
+ #
4
+ # Author:: Brasten Sager
5
+ # Date:: August 17th, 2006
6
+ #
7
+ # An Empty graph isn't completely empty, it adds a background componenet
8
+ # to itself before handing other all other layout responsibilities to it's
9
+ # subclasses or caller.
10
+ class Empty < Base
11
+
12
+ # Returns a renderer with just a background.
13
+ #
14
+ # If a block is provided, the components array is passed to
15
+ # the block, allowing callers to add components during initialize.
16
+ def define_layout
17
+ self.components << Scruffy::Components::Background.new(:background, :position => [0,0], :size =>[100, 100])
18
+
19
+ yield(self.components) if block_given?
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ module Scruffy::Renderers
2
+ # ===Scruffy::Renderers::Base
3
+ #
4
+ # Author:: A.J. Ostman
5
+ # Date:: August 14th, 2006
6
+ #
7
+ # Provides a more appropriate rendering for Pie Charts.
8
+ # Does not show grid or Data markers, but does add Pie Value Markers.
9
+ class Pie < Base
10
+
11
+ def initialize
12
+ self.components = []
13
+ self.components << Scruffy::Components::Background.new(:background, :position => [0,0], :size =>[100, 100])
14
+ self.components << Scruffy::Components::Graphs.new(:graphs, :position => [-15, 12], :size => [90, 88])
15
+ self.components << Scruffy::Components::Title.new(:title, :position => [5, 2], :size => [90, 7])
16
+ self.components << Scruffy::Components::Legend.new(:legend, :position => [60, 15], :size => [40, 88], :vertical_legend => true)
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,17 @@
1
+ module Scruffy
2
+ module Renderers
3
+ class Reversed < Base
4
+
5
+ def define_layout
6
+ self.components << Scruffy::Components::Background.new(:background, :position => [0,0], :size =>[100, 100])
7
+ self.components << Scruffy::Components::Title.new(:title, :position => [98, 95], :size => [1, 3], :text_anchor => 'end')
8
+ #self.components << Scruffy::Components::Grid.new(:grid, :position => [14, 12], :size => [78.5, 70])
9
+ self.components << Scruffy::Components::ValueMarkers.new(:value_markers, :position => [2, 14], :size => [10, 70])
10
+ self.components << Scruffy::Components::DataMarkers.new(:data_markers, :position => [14, 3.5], :size => [78.5, 4])
11
+ self.components << Scruffy::Components::Graphs.new(:graphs, :position => [14, 12], :size => [78.5, 70])
12
+ self.components << Scruffy::Components::Legend.new(:legend, :position => [3, 90], :size => [55, 6])
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ module Scruffy
2
+ module Renderers
3
+ # Experimental, do not use.
4
+ class Sparkline < Base
5
+ def define_layout
6
+ self.components << Scruffy::Components::Graphs.new(:sparkline, :position => [0, 0], :size => [100, 100])
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,48 @@
1
+ module Scruffy
2
+ module Renderers
3
+ # Renderer that splits the graphs up into four other little graphs.
4
+ class Split < Empty
5
+ def define_layout
6
+ super do |components|
7
+ components << Scruffy::Components::Title.new(:title, :position => [5, 2], :size => [90, 7])
8
+ components << Scruffy::Components::Label.new(:label_one, :text => self.options[:split_label] || '',
9
+ :position => [30, 54.5], :size => [40, 3])
10
+
11
+ # Viewports
12
+ components << Scruffy::Components::Viewport.new(:top, :position => [3, 20],
13
+ :size => [90, 30], &graph_block(:top))
14
+ components << Scruffy::Components::Viewport.new(:bottom, :position => [3, 65],
15
+ :size => [90, 30], &graph_block(:bottom))
16
+
17
+ components << Scruffy::Components::Legend.new(:legend, :position => [5, 11], :size => [90, 4])
18
+ end
19
+ end
20
+
21
+ protected
22
+ def labels
23
+ [component(:top).component(:labels), component(:bottom).component(:labels)]
24
+ end
25
+
26
+ def values
27
+ [component(:top).component(:values), component(:bottom).component(:values)]
28
+ end
29
+
30
+ def grids
31
+ [component(:top).component(:grid), component(:bottom).component(:grid)]
32
+ end
33
+
34
+
35
+ private
36
+ def graph_block(graph_filter)
37
+ block = Proc.new { |components|
38
+ components << Scruffy::Components::Grid.new(:grid, :position => [10, 0], :size => [90, 89])
39
+ components << Scruffy::Components::ValueMarkers.new(:values, :position => [0, 2], :size => [8, 89])
40
+ components << Scruffy::Components::DataMarkers.new(:labels, :position => [10, 92], :size => [90, 8])
41
+ components << Scruffy::Components::Graphs.new(:graphs, :position => [10, 0], :size => [90, 89], :only => graph_filter)
42
+ }
43
+
44
+ block
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ module Scruffy::Renderers
2
+ class Standard < Empty
3
+
4
+ def define_layout
5
+ super do |components|
6
+ components << Scruffy::Components::Title.new(:title, :position => [5, 2], :size => [90, 7])
7
+ components << Scruffy::Components::Viewport.new(:view, :position => [2, 26], :size => [90, 66]) do |graph|
8
+ graph << Scruffy::Components::ValueMarkers.new(:values, :position => [0, 2], :size => [8, 89])
9
+ graph << Scruffy::Components::Grid.new(:grid, :position => [10, 0], :size => [90, 89], :stroke_width => 1)
10
+ graph << Scruffy::Components::VGrid.new(:vgrid, :position => [10, 0], :size => [90, 89], :stroke_width => 1)
11
+ graph << Scruffy::Components::DataMarkers.new(:labels, :position => [10, 92], :size => [90, 8])
12
+ graph << Scruffy::Components::Graphs.new(:graphs, :position => [10, 0], :size => [90, 89])
13
+ end
14
+ components << Scruffy::Components::Legend.new(:legend, :position => [5, 13], :size => [90, 6])
15
+ end
16
+ end
17
+
18
+ protected
19
+ def hide_values
20
+ super
21
+ component(:view).position[0] = -10
22
+ component(:view).size[0] = 100
23
+ end
24
+
25
+ def labels
26
+ [component(:view).component(:labels)]
27
+ end
28
+
29
+ def values
30
+ [component(:view).component(:values)]
31
+ end
32
+
33
+ def grids
34
+ [component(:view).component(:grid),component(:view).component(:vgrid)]
35
+ end
36
+ end
37
+ end