Class: Gruff::Pie

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

Overview

Here’s how to make a Pie graph:

g = Gruff::Pie.new
g.title = "Visual Pie Graph Test"
g.data 'Fries', 20
g.data 'Hamburgers', 50
g.write("test/output/pie_keynote.png")

To control where the pie chart starts creating slices, use #zero_degree.

Direct Known Subclasses

Mini::Pie

Constant Summary collapse

TEXT_OFFSET_PERCENTAGE =
0.15

Constants inherited from Base

Base::DATA_COLOR_INDEX, Base::DATA_LABEL_INDEX, Base::DATA_VALUES_INDEX, Base::DATA_VALUES_X_INDEX, Base::DEBUG, Base::DEFAULT_MARGIN, Base::DEFAULT_TARGET_WIDTH, Base::LABEL_MARGIN, Base::LEGEND_MARGIN, Base::THOUSAND_SEPARATOR

Instance Attribute Summary collapse

Attributes inherited from Base

#additional_line_values, #bottom_margin, #center_labels_over_point, #colors, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #labels, #left_margin, #legend_box_size, #legend_font_size, #legend_margin, #marker_color, #marker_count, #marker_font_size, #maximum_value, #minimum_value, #no_data_message, #right_margin, #sort, #stacked, #title, #title_font_size, #title_margin, #top_margin, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

#add_color, #data, #initialize, #margins=, #replace_colors, #theme=, #theme_37signals, #theme_greyscale, #theme_keynote, #theme_odeo, #theme_pastel, #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 Attribute Details

#hide_labels_less_thanObject

Do not show labels for slices that are less than this percent. Use 0 to always show all labels. Defaults to 0



23
24
25
# File 'lib/gruff/pie.rb', line 23

def hide_labels_less_than
  @hide_labels_less_than
end

#zero_degreeObject

Can be used to make the pie start cutting slices at the top (-90.0) or at another angle. Default is 0.0, which starts at 3 o’clock.



20
21
22
# File 'lib/gruff/pie.rb', line 20

def zero_degree
  @zero_degree
end

Instance Method Details

#drawObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gruff/pie.rb', line 31

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 = @zero_degree

  # Use full data since we can easily calculate percentages
  data = (@sort ? @data.sort{ |a, b| a[DATA_VALUES_INDEX].first <=> b[DATA_VALUES_INDEX].first } : @data)
  data.each do |data_row|
    if data_row[DATA_VALUES_INDEX].first > 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[DATA_VALUES_INDEX].first / 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
      
      label_val = ((data_row[DATA_VALUES_INDEX].first / total_sum) * 100.0).round
      unless label_val < @hide_labels_less_than
        # End the string with %% to escape the single %.
        # RMagick must use sprintf with the string and % has special significance.
        label_string = label_val.to_s + '%%'
        @d = draw_label(center_x,center_y, half_angle,
                        radius + (radius * TEXT_OFFSET_PERCENTAGE),
                        label_string)
      end

      prev_degrees += current_degrees
    end
  end

  # TODO debug a circle where the text is drawn...
  
  @d.draw(@base_image)
end

#initialize_ivarsObject



25
26
27
28
29
# File 'lib/gruff/pie.rb', line 25

def initialize_ivars
  super
  @zero_degree = 0.0
  @hide_labels_less_than = 0.0
end