class Gruff::Spider

Experimental!!! See also the Net graph.

Submitted by Kevin Clark glu.ttono.us/

Attributes

hide_axes[RW]
hide_text[R]

Hide all text

transparent_background[R]

Public Class Methods

new(max_value, target_width = 800) click to toggle source
Calls superclass method Gruff::Base.new
# File lib/gruff/spider.rb, line 23
def initialize(max_value, target_width = 800)
  super(target_width)
  @max_value = max_value
  @hide_legend = true;
end

Public Instance Methods

draw() click to toggle source
Calls superclass method Gruff::Base#draw
# File lib/gruff/spider.rb, line 29
def draw
  @hide_line_markers = true
  
  super

  return unless @has_data

  # Setup basic positioning
  diameter = @graph_height
  radius = @graph_height / 2.0
  top_x = @graph_left + (@graph_width - diameter) / 2.0
  center_x = @graph_left + (@graph_width / 2.0)
  center_y = @graph_top + (@graph_height / 2.0) - 25 # Move graph up a bit
  
  @unit_length = radius / @max_value
  
      
  total_sum = sums_for_spider
  prev_degrees = 0.0
  additive_angle = (2 * Math::PI)/ @data.size
  
  current_angle = 0.0

  # Draw axes
  draw_axes(center_x, center_y, radius, additive_angle) unless hide_axes    
  
  # Draw polygon
  draw_polygon(center_x, center_y, additive_angle)
  
   
  @d.draw(@base_image)
end
hide_text=(value) click to toggle source
# File lib/gruff/spider.rb, line 19
def hide_text=(value)
  @hide_title = @hide_text = value
end
transparent_background=(value) click to toggle source
# File lib/gruff/spider.rb, line 14
def transparent_background=(value)
  @transparent_background = value
  @base_image = render_transparent_background if value
end

Private Instance Methods

draw_axes(center_x, center_y, radius, additive_angle, line_color = nil) click to toggle source
# File lib/gruff/spider.rb, line 88
def draw_axes(center_x, center_y, radius, additive_angle, line_color = nil)
  return if hide_axes
  
  current_angle = 0.0
  
  @data.each do |data_row|
    @d.stroke(line_color || data_row[DATA_COLOR_INDEX])
    @d.stroke_width 5.0
  
    x_offset = radius * Math.cos(current_angle)
    y_offset = radius * Math.sin(current_angle)

    @d.line(center_x, center_y,
            center_x + x_offset,
            center_y + y_offset)
          
    draw_label(center_x, center_y, current_angle, radius, data_row[DATA_LABEL_INDEX].to_s) unless hide_text
          
    current_angle += additive_angle
  end
end
draw_label(center_x, center_y, angle, radius, amount) click to toggle source
# File lib/gruff/spider.rb, line 68
def draw_label(center_x, center_y, angle, radius, amount)
  r_offset = 50      # The distance out from the center of the pie to get point
  x_offset = center_x      # The label points need to be tweaked slightly
  y_offset = center_y + 0  # This one doesn't though
  x = x_offset + ((radius + r_offset) * Math.cos(angle))
  y = y_offset + ((radius + r_offset) * Math.sin(angle))
  
  # Draw label
  @d.fill = @marker_color
  @d.font = @font if @font
  @d.pointsize = scale_fontsize(legend_font_size)
  @d.stroke = 'transparent'
  @d.font_weight = BoldWeight
  @d.gravity = CenterGravity
  @d.annotate_scaled( @base_image, 
                    0, 0,
                    x, y, 
                    amount, @scale)
end
draw_polygon(center_x, center_y, additive_angle, color = nil) click to toggle source
# File lib/gruff/spider.rb, line 110
def draw_polygon(center_x, center_y, additive_angle, color = nil)
  points = []
  current_angle = 0.0
  @data.each do |data_row|
    points << center_x + normalize_points(data_row[DATA_VALUES_INDEX].first) * Math.cos(current_angle)
    points << center_y + normalize_points(data_row[DATA_VALUES_INDEX].first) * Math.sin(current_angle)
    current_angle += additive_angle
  end
  
  @d.stroke_width 1.0
  @d.stroke(color || @marker_color)
  @d.fill(color || @marker_color)
  @d.fill_opacity 0.4
  @d.polygon(*points)
end
normalize_points(value) click to toggle source
# File lib/gruff/spider.rb, line 64
def normalize_points(value)
  value * @unit_length
end
sums_for_spider() click to toggle source
# File lib/gruff/spider.rb, line 126
def sums_for_spider
  @data.inject(0.0) {|sum, data_row| sum += data_row[DATA_VALUES_INDEX].first}
end