Class: Writexlsx::Chart::Pie

Inherits:
Writexlsx::Chart show all
Includes:
Utility
Defined in:
lib/write_xlsx/chart/pie.rb

Overview

A Pie chart doesn't have an X or Y axis so the following common chart methods are ignored.

chart.set_x_axis
chart.set_y_axis

Direct Known Subclasses

Doughnut

Constant Summary

Constants included from Utility

Utility::COL_MAX, Utility::ROW_MAX, Utility::SHEETNAME_MAX, Utility::STR_MAX

Instance Attribute Summary

Attributes inherited from Writexlsx::Chart

#already_inserted, #axis2_ids, #combined, #date_category, #embedded, #formula_data, #formula_ids, #height, #id, #index, #label_position_default, #label_positions, #name, #palette, #protection, #series_index, #width, #writer, #x2_axis, #x_offset, #x_scale, #y2_axis, #y_offset, #y_scale

Instance Method Summary collapse

Methods included from Utility

#absolute_char, #check_dimensions, #check_dimensions_and_update_max_min_values, #check_parameter, #convert_date_time, #dash_types, delete_files, #fill_properties, #float_to_str, #layout_properties, #line_fill_properties, #line_properties, #palette_color, #pixels_to_points, #process_workbook_options, #ptrue?, #put_deprecate_message, #quote_sheetname, #r_id_attributes, #row_col_notation, #shape_style_base, #store_col_max_min_values, #store_row_max_min_values, #substitute_cellref, #underline_attributes, #v_shape_attributes_base, #v_shape_style_base, #value_or_raise, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xml_str

Methods inherited from Writexlsx::Chart

#add_series, #already_inserted?, #assemble_xml_file, #convert_font_args, #data_id, factory, #is_secondary?, #params_to_font, #process_names, #set_chartarea, #set_drop_lines, #set_embedded_config_data, #set_high_low_lines, #set_legend, #set_plotarea, #set_size, #set_style, #set_table, #set_title, #set_up_down_bars, #set_x2_axis, #set_x_axis, #set_xml_writer, #set_y2_axis, #set_y_axis, #show_blanks_as, #show_hidden_data, #write_bar_chart, #write_val_axis

Constructor Details

#initialize(subtype) ⇒ Pie


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/write_xlsx/chart/pie.rb', line 29

def initialize(subtype)
  super(subtype)
  @vary_data_color = 1
  @rotation        = 0

  # Set the available data label positions for this chart type.
  @label_position_default = 'best_fit'
  @label_positions = {
    'center'      => 'ctr',
    'inside_base' => 'inBase',
    'inside_end'  => 'inEnd',
    'outside_end' => 'outEnd',
    'best_fit'    => 'bestFit'
  }
end

Instance Method Details

#combine(chart) ⇒ Object

Override parent method to add a warning.


48
49
50
# File 'lib/write_xlsx/chart/pie.rb', line 48

def combine(chart)
  raise "Combined chart not currently supported for Pie charts"
end

#set_rotation(rotation) ⇒ Object

Set the Pie/Doughnut chart rotation: the angle of the first slice.


55
56
57
58
59
60
61
62
# File 'lib/write_xlsx/chart/pie.rb', line 55

def set_rotation(rotation)
  return unless rotation
  if rotation >= 0 && rotation <= 360
    @rotation = rotation
  else
    raise "Chart rotation $rotation outside range: 0 <= rotation <= 360"
  end
end

#write_a_p_legend(font) ⇒ Object

Write the <a:p> element for legends.


160
161
162
163
164
165
166
167
# File 'lib/write_xlsx/chart/pie.rb', line 160

def write_a_p_legend(font)
  @writer.tag_elements('a:p') do
    # Write the a:pPr element.
    write_a_p_pr_legend(font)
    # Write the a:endParaRPr element.
    write_a_end_para_rpr
  end
end

#write_a_p_pr_legend(font) ⇒ Object

Write the <a:pPr> element for legends.


172
173
174
175
176
177
# File 'lib/write_xlsx/chart/pie.rb', line 172

def write_a_p_pr_legend(font)
  @writer.tag_elements('a:pPr', [ ['rtl', 0] ]) do
    # Write the a:defRPr element.
    write_a_def_rpr(font)
  end
end

#write_chart_typeObject

Override the virtual superclass method with a chart specific method.


67
68
69
70
# File 'lib/write_xlsx/chart/pie.rb', line 67

def write_chart_type
  # Write the c:areaChart element.
  write_pie_chart
end

#write_first_slice_angObject

Write the <c:firstSliceAng> element.


189
190
191
# File 'lib/write_xlsx/chart/pie.rb', line 189

def write_first_slice_ang
  @writer.empty_tag('c:firstSliceAng', [ ['val', @rotation] ])
end

#write_legendObject

Over-ridden method to add <c:txPr> to legend.

Write the <c:legend> element.


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/write_xlsx/chart/pie.rb', line 107

def write_legend
  position = @legend_position
  allowed  = %w(right left top bottom)
  delete_series = @legend_delete_series || []

  if @legend_position =~ /^overlay_/
    position = @legend_position.sub(/^overlay_/, '')
    overlay = true
  else
    position = @legend_position
    overlay = false
  end

  return if position == 'none'
  return unless allowed.include?(position)

  @writer.tag_elements('c:legend') do
    # Write the c:legendPos element.
    write_legend_pos(position[0])
    # Remove series labels from the legend.
    # Write the c:legendEntry element.
    delete_series.each { |index| write_legend_entry(index) }
    # Write the c:layout element.
    write_layout(@legend_layout, 'legend')
    # Write the c:overlay element.
    write_overlay if overlay
    # Write the c:txPr element. Over-ridden.
    write_tx_pr_legend(0, @legend_font)
  end
end

#write_pie_chartObject

Write the <c:pieChart> element. Over-ridden method to remove axis_id code since pie charts don't require val and vat axes.


76
77
78
79
80
81
82
83
84
85
# File 'lib/write_xlsx/chart/pie.rb', line 76

def write_pie_chart
  @writer.tag_elements('c:pieChart') do
    # Write the c:varyColors element.
    write_vary_colors
    # Write the series elements.
    @series.each {|s| write_series(s)}
    # Write the c:firstSliceAng element.
    write_first_slice_ang
  end
end

#write_plot_areaObject

Over-ridden method to remove the cat_axis() and val_axis() code since Pie/Doughnut charts don't require those axes.

Write the <c:plotArea> element.


93
94
95
96
97
98
99
100
# File 'lib/write_xlsx/chart/pie.rb', line 93

def write_plot_area
  @writer.tag_elements('c:plotArea') do
    # Write the c:layout element.
    write_layout(@plotarea.layout, 'plot')
    # Write the subclass chart type element.
    write_chart_type
  end
end

#write_tx_pr_legend(horiz, font) ⇒ Object

Write the <c:txPr> element for legends.


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/write_xlsx/chart/pie.rb', line 141

def write_tx_pr_legend(horiz, font)
  rotation = nil
  if ptrue?(font) && font[:_rotation]
    rotation = font[:_rotation]
  end

  @writer.tag_elements('c:txPr') do
    # Write the a:bodyPr element.
    write_a_body_pr(rotation, horiz)
    # Write the a:lstStyle element.
    write_a_lst_style
    # Write the a:p element.
    write_a_p_legend(font)
  end
end

#write_vary_colorsObject

Write the <c:varyColors> element.


182
183
184
# File 'lib/write_xlsx/chart/pie.rb', line 182

def write_vary_colors
  @writer.empty_tag('c:varyColors', [ ['val', 1] ])
end