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::CHAR_WIDTHS, Utility::COL_MAX, Utility::PERL_TRUE_VALUES, 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, #color, #convert_date_time, #convert_font_args, #dash_types, delete_files, #escape_url, #fill_properties, #float_to_str, #get_font_latin_attributes, #get_font_style_attributes, #get_image_properties, #layout_properties, #legend_properties, #line_fill_properties, #line_properties, #palette_color, #params_to_font, #pattern_properties, #pixels_to_points, #process_bmp, #process_gif, #process_jpg, #process_png, #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_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_a_solid_fill, #write_a_srgb_clr, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_def_rpr_r_pr_common, #write_div, #write_fill, #write_font, #write_stroke, #write_tx_pr, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xl_string_pixel_width, #xml_str

Methods inherited from Writexlsx::Chart

#add_series, #already_inserted?, #assemble_xml_file, #combine, #data_id, factory, #is_secondary?, #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, #show_na_as_empty_cell, #write_bar_chart, #write_val_axis

Methods included from Gradient

#gradient_properties

Constructor Details

#initialize(subtype) ⇒ Pie

Returns a new instance of Pie.



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

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

#set_rotation(rotation) ⇒ Object

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



50
51
52
53
54
55
56
57
58
# File 'lib/write_xlsx/chart/pie.rb', line 50

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.



177
178
179
180
181
182
183
184
# File 'lib/write_xlsx/chart/pie.rb', line 177

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.



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

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.



63
64
65
66
# File 'lib/write_xlsx/chart/pie.rb', line 63

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

#write_first_slice_angObject

Write the <c:firstSliceAng> element.



206
207
208
# File 'lib/write_xlsx/chart/pie.rb', line 206

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.



125
126
127
128
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
# File 'lib/write_xlsx/chart/pie.rb', line 125

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.



72
73
74
75
76
77
78
79
80
81
# File 'lib/write_xlsx/chart/pie.rb', line 72

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.



89
90
91
92
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
# File 'lib/write_xlsx/chart/pie.rb', line 89

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.



215
216
217
218
219
220
221
# File 'lib/write_xlsx/chart/pie.rb', line 215

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.



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

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.



199
200
201
# File 'lib/write_xlsx/chart/pie.rb', line 199

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