jagthedrummer-scruffy 0.2.10 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/scruffy/layers/box.rb +114 -0
  2. metadata +2 -1
@@ -0,0 +1,114 @@
1
+ module Scruffy::Layers
2
+ # ==Scruffy::Layers::Box
3
+ #
4
+ # Author:: Brasten Sager
5
+ # Date:: August 6th, 2006
6
+ #
7
+ # Standard bar graph.
8
+ class Box < Base
9
+
10
+ # Draw box plot.
11
+ def draw(svg, coords, options = {})
12
+ coords.each_with_index do |coord,idx|
13
+ x, y, bar_height = (coord.first), coord.last, 1#(height - coord.last)
14
+
15
+ valh = max_value + min_value * -1 #value_height
16
+ maxh = max_value * height / valh #positive area height
17
+ minh = min_value * height / valh #negative area height
18
+ #puts "height = #{height} and max_value = #{max_value} and min_value = #{min_value} and y = #{y} and point = #{points[idx]}"
19
+
20
+ #if points[idx] > 0
21
+ # bar_height = points[idx]*maxh/max_value
22
+ #else
23
+ # bar_height = points[idx]*minh/min_value
24
+ #end
25
+
26
+ #puts " y = #{y} and point = #{points[idx]}"
27
+ #svg.g(:transform => "translate(-#{relative(0.5)}, -#{relative(0.5)})") {
28
+ # svg.rect( :x => x, :y => y, :width => @bar_width + relative(1), :height => bar_height + relative(1),
29
+ # :style => "fill: black; fill-opacity: 0.15; stroke: none;" )
30
+ # svg.rect( :x => x+relative(0.5), :y => y+relative(2), :width => @bar_width + relative(1), :height => bar_height - relative(0.5),
31
+ # :style => "fill: black; fill-opacity: 0.15; stroke: none;" )
32
+ #
33
+ #}
34
+
35
+ svg.line(:x1=>x+@bar_width/2,:x2=>x+@bar_width/2,:y1=>y[0],:y2=>y[4], :style => "stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1")
36
+ svg.line(:x1=>x+@bar_width/4,:x2=>x+@bar_width/4*3,:y1=>y[0],:y2=>y[0], :style => "stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1")
37
+ svg.line(:x1=>x+@bar_width/4,:x2=>x+@bar_width/4*3,:y1=>y[4],:y2=>y[4], :style => "stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1")
38
+ svg.rect( :x => x, :y => y[1], :width => @bar_width, :height => (y[1]-y[3])*-1,
39
+ :fill => color.to_s, 'style' => "opacity: #{opacity}; stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1;" )
40
+ svg.line(:x1=>x,:x2=>x+@bar_width,:y1=>y[2],:y2=>y[2], :style => "stroke:#{(outline.to_s || options[:theme].marker || 'white').to_s}; stroke-width:1")
41
+ #svg.rect( :x => x, :y => y, :width => @bar_width, :height => bar_height,
42
+ # :fill => color.to_s, 'style' => "opacity: #{opacity}; stroke: none;" )
43
+ end
44
+ end
45
+
46
+
47
+ # Returns the highest value in any of this container's layers.
48
+ #
49
+ # If padding is set to :padded, a 15% padding is added to the highest value.
50
+ def top_value(padding=nil) # :nodoc:
51
+ topval = points[0].max
52
+ points.each do |point_set|
53
+ topval = ( (topval < point_set.max) ? point_set.max : topval )
54
+ end
55
+ #topval = layers.inject(0) { |max, layer| (max = ((max < layer.top_value) ? layer.top_value : max)) unless layer.top_value.nil?; max }
56
+ below_zero = (topval <= 0)
57
+ topval = padding == :padded ? (topval + ((topval - bottom_value(nil)) * 0.15)) : topval
58
+ (below_zero && topval > 0) ? 0 : topval
59
+ end
60
+
61
+ # Returns the lowest value in any of this container's layers.
62
+ #
63
+ # If padding is set to :padded, a 15% padding is added below the lowest value.
64
+ # If the lowest value is greater than zero, then the padding will not cross the zero line, preventing
65
+ # negative values from being introduced into the graph purely due to padding.
66
+ def bottom_value(padding=nil) # :nodoc:
67
+ botval = points[0].min
68
+ points.each do |point_set|
69
+ botval = ( (botval>point_set.min) ? point_set.min : botval )
70
+ end
71
+ #botval = layers.inject(0) do |min, layer|
72
+ # (min = ((min > layer.bottom_value) ? layer.bottom_value : min)) unless layer.bottom_value.nil?
73
+ # min
74
+ #end
75
+ above_zero = (botval >= 0)
76
+ botval = (botval - ((top_value(nil) - botval) * 0.15)) if padding == :padded
77
+
78
+ # Don't introduce negative values solely due to padding.
79
+ # A user-provided value must be negative before padding will extend into negative values.
80
+ (above_zero && botval < 0) ? 0 : botval
81
+ end
82
+
83
+ protected
84
+
85
+ # Due to the size of the bar graph, X-axis coords must
86
+ # be squeezed so that the bars do not hang off the ends
87
+ # of the graph.
88
+ #
89
+ # Unfortunately this just mean that bar-graphs and most other graphs
90
+ # end up on different points. Maybe adding a padding to the coordinates
91
+ # should be a graph-wide thing?
92
+ #
93
+ # Update : x-axis coords for lines and area charts should now line
94
+ # up with the center of bar charts.
95
+
96
+ def generate_coordinates(options = {})
97
+ @bar_width = (width / points.size) * 0.9
98
+ options[:point_distance] = (width - (width / points.size)) / (points.size - 1).to_f
99
+
100
+ #TODO more array work with index, try to rework to be accepting of hashes
101
+ coords = (0...points.size).map do |idx|
102
+ x_coord = (options[:point_distance] * idx) + (width / points.size * 0.5) - (@bar_width * 0.5)
103
+ y_coords = []
104
+ points[idx].each do |point|
105
+ relative_percent = ((point == min_value) ? 0 : ((point - min_value) / (max_value - min_value).to_f))
106
+ y_coord = (height - (height * relative_percent))
107
+ y_coords << y_coord
108
+ end
109
+ [x_coord, y_coords]
110
+ end
111
+ coords
112
+ end
113
+ end
114
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jagthedrummer-scruffy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brasten Sager
@@ -50,6 +50,7 @@ files:
50
50
  - lib/scruffy/layers/all_smiles.rb
51
51
  - lib/scruffy/layers/average.rb
52
52
  - lib/scruffy/layers/line.rb
53
+ - lib/scruffy/layers/box.rb
53
54
  - lib/scruffy/rasterizers.rb
54
55
  - lib/scruffy/renderers.rb
55
56
  - lib/scruffy/themes.rb