| Class | Gruff::Net |
| In: |
lib/gruff/net.rb
|
| Parent: | Gruff::Base |
Experimental!!! See also the Spider graph.
| dot_radius | [RW] | |
| hide_dots | [RW] | Hide parts of the graph to fit more datapoints, or for a different appearance. |
| line_width | [RW] | Dimensions of lines and dots; calculated based on dataset size if left unspecified |
# File lib/gruff/net.rb, line 14
14: def initialize(*args)
15: super
16:
17: @hide_dots = false
18: @hide_line_numbers = true
19: end
# File lib/gruff/net.rb, line 21
21: def draw
22:
23: super
24:
25: return unless @has_data
26:
27: @radius = @graph_height / 2.0
28: @center_x = @graph_left + (@graph_width / 2.0)
29: @center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit
30:
31: @x_increment = @graph_width / (@column_count - 1).to_f
32: circle_radius = dot_radius ||
33: clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 2.5), 5.0)
34:
35: @d = @d.stroke_opacity 1.0
36: @d = @d.stroke_width line_width ||
37: clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 4), 5.0)
38:
39: if (defined?(@norm_baseline)) then
40: level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
41: @d = @d.push
42: @d.stroke_color @baseline_color
43: @d.fill_opacity 0.0
44: @d.stroke_dasharray(10, 20)
45: @d.stroke_width 5
46: @d.line(@graph_left, level, @graph_left + @graph_width, level)
47: @d = @d.pop
48: end
49:
50: @norm_data.each do |data_row|
51: prev_x = prev_y = nil
52: @d = @d.stroke data_row[DATA_COLOR_INDEX]
53: @d = @d.fill data_row[DATA_COLOR_INDEX]
54:
55: data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
56: next if data_point.nil?
57:
58: rad_pos = index * Math::PI * 2 / @column_count
59: point_distance = data_point * @radius
60: start_x = @center_x + Math::sin(rad_pos) * point_distance
61: start_y = @center_y - Math::cos(rad_pos) * point_distance
62:
63: next_index = index + 1 < data_row[DATA_VALUES_INDEX].length ? index + 1 : 0
64:
65: next_rad_pos = next_index * Math::PI * 2 / @column_count
66: next_point_distance = data_row[DATA_VALUES_INDEX][next_index] * @radius
67: end_x = @center_x + Math::sin(next_rad_pos) * next_point_distance
68: end_y = @center_y - Math::cos(next_rad_pos) * next_point_distance
69:
70: @d = @d.line(start_x, start_y, end_x, end_y)
71:
72: @d = @d.circle(start_x, start_y, start_x - circle_radius, start_y) unless @hide_dots
73: end
74:
75: end
76:
77: @d.draw(@base_image)
78: end
the lines connecting in the center, with the first line vertical
# File lib/gruff/net.rb, line 82
82: def draw_line_markers
83: return if @hide_line_markers
84:
85:
86: # have to do this here (AGAIN)... see draw() in this class
87: # because this funtion is called before the @radius, @center_x and @center_y are set
88: @radius = @graph_height / 2.0
89: @center_x = @graph_left + (@graph_width / 2.0)
90: @center_y = @graph_top + (@graph_height / 2.0) - 10 # Move graph up a bit
91:
92:
93: # Draw horizontal line markers and annotate with numbers
94: @d = @d.stroke(@marker_color)
95: @d = @d.stroke_width 1
96:
97:
98: (0..@column_count-1).each do |index|
99: rad_pos = index * Math::PI * 2 / @column_count
100:
101: @d = @d.line(@center_x, @center_y, @center_x + Math::sin(rad_pos) * @radius, @center_y - Math::cos(rad_pos) * @radius)
102:
103:
104: marker_label = labels[index] ? labels[index].to_s : '000'
105:
106: draw_label(@center_x, @center_y, rad_pos * 360 / (2 * Math::PI), @radius, marker_label)
107: end
108: end
# File lib/gruff/net.rb, line 112
112: def draw_label(center_x, center_y, angle, radius, amount)
113: r_offset = 1.1
114: x_offset = center_x # + 15 # The label points need to be tweaked slightly
115: y_offset = center_y # + 0 # This one doesn't though
116: x = x_offset + (radius * r_offset * Math.sin(angle.deg2rad))
117: y = y_offset - (radius * r_offset * Math.cos(angle.deg2rad))
118:
119: # Draw label
120: @d.fill = @marker_color
121: @d.font = @font if @font
122: @d.pointsize = scale_fontsize(20)
123: @d.stroke = 'transparent'
124: @d.font_weight = BoldWeight
125: @d.gravity = CenterGravity
126: @d.annotate_scaled(@base_image, 0, 0, x, y, amount, @scale)
127: end