Added the Gruff library to the lib/ directory of the the rails folder, and the
[selectricity] / lib / gruff-0.2.8 / lib / gruff / net.rb
1
2 require File.dirname(__FILE__) + '/base'
3
4 # Experimental!!! See also the Spider graph.
5 class Gruff::Net < Gruff::Base
6
7   # Hide parts of the graph to fit more datapoints, or for a different appearance.
8   attr_accessor :hide_dots
9
10   def initialize(*args)
11     super
12     
13     @hide_dots = false
14   end
15
16   def draw
17
18     super
19
20     return unless @has_data
21
22     @radius = @graph_height / 2.0
23     @center_x = @graph_left + (@graph_width / 2.0)
24     @center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit
25
26     @x_increment = @graph_width / (@column_count - 1).to_f
27     circle_radius = clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 2.5), 5.0)
28
29     @d = @d.stroke_opacity 1.0
30     @d = @d.stroke_width clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 4), 5.0)
31
32     if (defined?(@norm_baseline)) then
33       level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
34       @d = @d.push
35       @d.stroke_color @baseline_color
36       @d.fill_opacity 0.0
37       @d.stroke_dasharray(10, 20)
38       @d.stroke_width 5
39       @d.line(@graph_left, level, @graph_left + @graph_width, level)
40       @d = @d.pop
41     end
42
43     @norm_data.each do |data_row|
44       prev_x = prev_y = nil
45       @d = @d.stroke data_row[DATA_COLOR_INDEX]
46       @d = @d.fill data_row[DATA_COLOR_INDEX]
47
48       data_row[1].each_with_index do |data_point, index|
49         next if data_point.nil?
50
51         rad_pos = index * Math::PI * 2 / @column_count
52         point_distance = data_point * @radius
53         start_x = @center_x + Math::sin(rad_pos) * point_distance
54         start_y = @center_y - Math::cos(rad_pos) * point_distance
55
56         next_index = index + 1 < data_row[1].length ? index + 1 : 0
57
58         next_rad_pos = next_index * Math::PI * 2 / @column_count
59         next_point_distance = data_row[1][next_index] * @radius
60         end_x = @center_x + Math::sin(next_rad_pos) * next_point_distance
61         end_y = @center_y - Math::cos(next_rad_pos) * next_point_distance
62
63         @d = @d.line(start_x, start_y, end_x, end_y)
64
65         @d = @d.circle(start_x, start_y, start_x - circle_radius, start_y) unless @hide_dots
66       end
67
68     end
69
70     @d.draw(@base_image)
71   end
72
73
74   # the lines connecting in the center, with the first line vertical
75   def draw_line_markers
76     return if @hide_line_markers
77
78
79     # have to do this here (AGAIN)... see draw() in this class
80     # because this funtion is called before the @radius, @center_x and @center_y are set
81     @radius = @graph_height / 2.0
82     @center_x = @graph_left + (@graph_width / 2.0)
83     @center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit
84
85
86     # Draw horizontal line markers and annotate with numbers
87     @d = @d.stroke(@marker_color)
88     @d = @d.stroke_width 1
89
90
91     (0..@column_count-1).each do |index|
92       rad_pos = index * Math::PI * 2 / @column_count
93
94       @d = @d.line(@center_x, @center_y, @center_x + Math::sin(rad_pos) * @radius, @center_y - Math::cos(rad_pos) * @radius)
95
96
97       marker_label = labels[index] ? labels[index].to_s : '000'
98
99       draw_label(@center_x, @center_y, rad_pos * 360 / (2 * Math::PI), @radius, marker_label)
100     end
101   end
102
103 private
104
105   def draw_label(center_x, center_y, angle, radius, amount)
106     r_offset = 1.1
107     x_offset = center_x # + 15 # The label points need to be tweaked slightly
108     y_offset = center_y # + 0  # This one doesn't though
109     x = x_offset + (radius * r_offset * Math.sin(angle.deg2rad))
110     y = y_offset - (radius * r_offset * Math.cos(angle.deg2rad))
111
112     # Draw label
113     @d.fill = @marker_color
114     @d.font = @font if @font
115     @d.pointsize = scale_fontsize(20)
116     @d.stroke = 'transparent'
117     @d.font_weight = BoldWeight
118     s = angle.deg2rad / (2*Math::PI)
119     @d.gravity = SouthGravity     if s >= 0.96 or s < 0.04
120     @d.gravity = SouthWestGravity if s >= 0.04 or s < 0.21
121     @d.gravity = WestGravity      if s >= 0.21 or s < 0.29
122     @d.gravity = NorthWestGravity if s >= 0.29 or s < 0.46
123     @d.gravity = NorthGravity     if s >= 0.46 or s < 0.54
124     @d.gravity = NorthEastGravity if s >= 0.54 or s < 0.71
125     @d.gravity = EastGravity      if s >= 0.71 or s < 0.79
126     @d.gravity = SouthEastGravity if s >= 0.79 or s < 0.96
127 #     @d.gravity = NorthGravity
128     @d.annotate_scaled(@base_image, 0, 0, x, y, amount, @scale)
129   end
130
131 end
132
133 # # This method is already in Float
134 # class Float
135 #   # Used for degree => radian conversions
136 #   def deg2rad
137 #     self * (Math::PI/180.0)
138 #   end
139 # end
140
141
142

Benjamin Mako Hill || Want to submit a patch?