Class: Writexlsx::Chart::Pie

Inherits:
Writexlsx::Chart show all
Includes:
Utility::CellReference, Utility::SheetnameQuoting, Utility::XmlPrimitives
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 Writexlsx::Constants

Writexlsx::Constants::COL_MAX, Writexlsx::Constants::ROW_MAX, Writexlsx::Constants::SHEETNAME_MAX, Writexlsx::Constants::STR_MAX

Constants included from Utility::ChartFormatting

Utility::ChartFormatting::PATTERN_TYPES

Constants included from Utility::Common

Utility::Common::PERL_TRUE_VALUES

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::SheetnameQuoting

#quote_sheetname

Methods included from Utility::XmlPrimitives

#r_id_attributes, #write_color, #write_xml_declaration, #xml_str

Methods included from Utility::CellReference

#row_col_notation, #substitute_cellref, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell

Methods inherited from Writexlsx::Chart

#already_inserted?, factory, #is_secondary?, #set_xml_writer, #write_bar_chart

Methods included from AxisWriter

#write_val_axis

Methods included from XmlWriter

#assemble_xml_file

Methods included from SeriesData

#data_id, #process_names

Methods included from Settings

#add_series, #combine, #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_y2_axis, #set_y_axis, #show_blanks_as, #show_hidden_data, #show_na_as_empty_cell

Methods included from Gradient

#gradient_properties

Methods included from Utility::RichText

#convert_font_args, #get_font_latin_attributes, #get_font_style_attributes, #params_to_font, #underline_attributes, #write_a_body_pr, #write_a_def_rpr, #write_a_end_para_rpr, #write_a_lst_style, #write_a_p_formula, #write_a_p_pr_formula, #write_def_rpr_r_pr_common, #write_tx_pr

Methods included from Utility::ChartFormatting

#color, #dash_types, #fill_properties, #layout_properties, #legend_properties, #line_fill_properties, #line_properties, #palette_color_from_index, #pattern_properties, #value_or_raise, #write_a_solid_fill, #write_a_srgb_clr

Methods included from Utility::Common

#absolute_char, #check_parameter, #float_to_str, #ptrue?, #put_deprecate_message

Constructor Details

#initialize(subtype) ⇒ Pie

Returns a new instance of Pie.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/write_xlsx/chart/pie.rb', line 35

def initialize(subtype)
  super
  @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

#set_rotation(rotation) ⇒ Object

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



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

def set_rotation(rotation)
  return unless rotation

  if rotation.between?(0, 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.



181
182
183
184
185
186
187
188
# File 'lib/write_xlsx/chart/pie.rb', line 181

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.



193
194
195
196
197
198
# File 'lib/write_xlsx/chart/pie.rb', line 193

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.



210
211
212
# File 'lib/write_xlsx/chart/pie.rb', line 210

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.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/write_xlsx/chart/pie.rb', line 129

def write_legend
  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:spPr element.
    write_sp_pr(@legend)
    # 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/write_xlsx/chart/pie.rb', line 93

def write_plot_area
  second_chart = @combined

  @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
    # Configure a combined chart if present.
    if second_chart
      # Secondary axis has unique id otherwise use same as primary.
      second_chart.id = if second_chart.is_secondary?
                          1000 + @id
                        else
                          @id
                        end

      # Share the same filehandle for writing
      second_chart.writer = @writer

      # Share series index with primary chart.
      second_chart.series_index = @series_index

      # Write the subclass chart type elements for combined chart.
      second_chart.write_chart_type
    end
    # Write the c:spPr eleent for the plotarea formatting.
    write_sp_pr(@plotarea)
  end
end

#write_show_leader_linesObject

Write the <c:showLeaderLines> element. This is for Pie/Doughnut charts. Other chart types only supported leader lines after Excel 2015 via an extension element.



219
220
221
222
223
224
225
# File 'lib/write_xlsx/chart/pie.rb', line 219

def write_show_leader_lines
  val  = 1

  attributes = [['val', val]]

  @writer.empty_tag('c:showLeaderLines', attributes)
end

#write_tx_pr_legend(horiz, font) ⇒ Object

Write the <c:txPr> element for legends.



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/write_xlsx/chart/pie.rb', line 164

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

  @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.



203
204
205
# File 'lib/write_xlsx/chart/pie.rb', line 203

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