Class: Gruff::Pie

Inherits:
Base
  • Object
show all
Defined in:
lib/gruff/pie.rb

Constant Summary

Constants inherited from Base

Base::DATA_COLOR_INDEX, Base::DATA_LABEL_INDEX, Base::DATA_VALUES_INDEX, Base::DEBUG, Base::LEGEND_MARGIN, Base::TOP_MARGIN

Constants included from Magick

Magick::BoldWeight, Magick::CenterGravity, Magick::EastGravity, Magick::NormalWeight, Magick::NorthEastGravity, Magick::NorthGravity, Magick::NorthWestGravity, Magick::SouthEastGravity, Magick::SouthGravity, Magick::SouthWestGravity, Magick::WestGravity

Instance Attribute Summary

Attributes inherited from Base

#additional_line_values, #colors, #font, #hide_legend, #hide_line_markers, #hide_title, #labels, #legend_font_size, #marker_color, #marker_count, #marker_font_size, #maximum_value, #minimum_value, #no_data_message, #stacked, #title, #title_font_size, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

#add_color, #data, #initialize, #replace_colors, #theme=, #theme_37signals, #theme_keynote, #theme_odeo, #theme_rails_keynote, #to_blob, #write

Methods included from Deprecated

#graph_height, #graph_left, #graph_top, #graph_width, #scale_measurements, #total_height

Constructor Details

This class inherits a constructor from Gruff::Base

Instance Method Details

#drawObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gruff/pie.rb', line 6

def draw
  @hide_line_markers = true
  
  super

  return unless @has_data

  diameter = @graph_height
  radius = ([@graph_width, @graph_height].min / 2.0) * 0.8
  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) - 10 # Move graph up a bit
  total_sum = sums_for_pie()
  prev_degrees = 0.0

  # Use full data since we can easily calculate percentages
  @data.each do |data_row|
    if data_row[1][0] > 0
      @d = @d.stroke data_row[DATA_COLOR_INDEX]
      @d = @d.fill 'transparent'
      @d.stroke_width(radius) # stroke width should be equal to radius. we'll draw centered on (radius / 2)

      current_degrees = (data_row[1][0] / total_sum) * 360.0 

      # ellipse will draw the the stroke centered on the first two parameters offset by the second two.
      # therefore, in order to draw a circle of the proper diameter we must center the stroke at
      # half the radius for both x and y
      @d = @d.ellipse(center_x, center_y, 
                radius / 2.0, radius / 2.0,
                prev_degrees, prev_degrees + current_degrees + 0.5) # <= +0.5 'fudge factor' gets rid of the ugly gaps
                
      half_angle = prev_degrees + ((prev_degrees + current_degrees) - prev_degrees) / 2
    

      @d = draw_label(center_x,center_y, 
                  half_angle, 
                  radius, 
                  ((data_row[1][0] / total_sum) * 100).round.to_s + '%     ')
    
      prev_degrees += current_degrees
    end
  end

  @d.draw(@base_image)
end