Class: Gruff::Pie

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

Overview

Here’s how to make a Gruff::Pie.

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

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

Direct Known Subclasses

Mini::Pie

Constant Summary collapse

DEFAULT_TEXT_OFFSET_PERCENTAGE =
0.15

Constants inherited from Base

Base::DEFAULT_MARGIN, Base::DEFAULT_TARGET_WIDTH, Base::LABEL_MARGIN, Base::LEGEND_MARGIN

Instance Attribute Summary collapse

Attributes inherited from Base

#bold_title, #bottom_margin, #center_labels_over_point, #colors, #font_color, #has_left_labels, #hide_legend, #hide_line_numbers, #hide_title, #label_max_size, #label_stagger_height, #label_truncation_style, #labels, #left_margin, #legend_at_bottom, #legend_box_size, #legend_font_size, #legend_margin, #marker_color, #marker_font_size, #marker_shadow_color, #maximum_value, #minimum_value, #no_data_message, #right_margin, #sort, #sorted_drawing, #title, #title_font, #title_font_size, #title_margin, #top_margin, #use_data_label, #x_axis_increment, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

#add_color, #data, #font=, #initialize, #margins=, #replace_colors, #theme=, #theme_37signals, #theme_greyscale, #theme_keynote, #theme_odeo, #theme_pastel, #theme_rails_keynote, #to_blob, #to_image, #write

Constructor Details

This class inherits a constructor from Gruff::Base

Instance Attribute Details

#hide_labels_less_than=(value) ⇒ Object (writeonly)

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=(value)
  @hide_labels_less_than = value
end

#show_values_as_labels=(value) ⇒ Object (writeonly)

Use values instead of percentages.



30
31
32
# File 'lib/gruff/pie.rb', line 30

def show_values_as_labels=(value)
  @show_values_as_labels = value
end

#text_offset_percentage=(value) ⇒ Object (writeonly)

Affect the distance between the percentages and the pie chart. Defaults to 0.15.



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

def text_offset_percentage=(value)
  @text_offset_percentage = value
end

#zero_degree=(value) ⇒ Object (writeonly)

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.



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

def zero_degree=(value)
  @zero_degree = value
end

Instance Method Details

#drawObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gruff/pie.rb', line 55

def draw
  hide_line_markers

  super

  return unless data_given?

  slices.each do |slice|
    if slice.value > 0
      Gruff::Renderer::Ellipse.new(color: slice.color, width: radius)
                              .render(center_x, center_y, radius / 2.0, radius / 2.0, chart_degrees, chart_degrees + slice.degrees + 0.5)
      process_label_for slice
      update_chart_degrees_with slice.degrees
    end
  end
end

#optionsObject



46
47
48
49
50
51
52
53
# File 'lib/gruff/pie.rb', line 46

def options
  {
    zero_degree: @zero_degree,
    hide_labels_less_than: @hide_labels_less_than,
    text_offset_percentage: @text_offset_percentage,
    show_values_as_labels: @show_values_as_labels
  }
end