Class: Writexlsx::Chart::Scatter

Inherits:
Writexlsx::Chart show all
Includes:
DPtPointWriter, Utility::XmlPrimitives
Defined in:
lib/write_xlsx/chart/scatter.rb

Constant Summary

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 DPtPointWriter

#write_d_pt_point

Methods included from Utility::XmlPrimitives

#r_id_attributes, #write_color, #write_xml_declaration, #xml_str

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, #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) ⇒ Scatter

Returns a new instance of Scatter.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/write_xlsx/chart/scatter.rb', line 27

def initialize(subtype)
  super
  @subtype           = subtype || 'marker_only'
  @cross_between     = 'midCat'
  @horiz_val_axis    = 0
  @val_axis_position = 'b'
  @smooth_allowed    = 1

  # Set the available data label positions for this chart type.
  @label_position_default = 'right'
  @label_positions = {
    'center' => 'ctr',
    'right'  => 'r',
    'left'   => 'l',
    'above'  => 't',
    'below'  => 'b',
    # For backward compatibility.
    'top'    => 't',
    'bottom' => 'b'
  }
end

Instance Method Details

#combine(_chart) ⇒ Object

Override parent method to add a warning.



52
53
54
# File 'lib/write_xlsx/chart/scatter.rb', line 52

def combine(_chart)
  raise 'Combined chart not currently supported with scatter chart as the primary chart'
end

#modify_series_formattingObject

Add default formatting to the series data unless it has already been specified by the user.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/write_xlsx/chart/scatter.rb', line 200

def modify_series_formatting
  # The default scatter style "markers only" requires a line type
  if @subtype == 'marker_only'
    # Go through each series and define default values.
    @series.each do |series|
      # Set a line type unless there is already a user defined type.
      series.line = line_properties(width: 2.25, none: 1, _defined: 1) unless series.line_defined?
    end
  end

  # Turn markers off for subtypes that don't have them
  unless @subtype =~ /marker/
    # Go through each series and define default values.
    @series.each do |series|
      # Set a marker type unless there is already a user defined type.
      series.marker = Marker.new(type: 'none', _defined: 1) unless ptrue?(series.marker)
    end
  end
end

#write_cat_val_axis(x_axis, y_axis, axis_ids, position) ⇒ Object

Write the <c:valAx> element. This is for the second valAx in scatter plots.

Usually the X axis.



226
227
228
229
230
231
232
233
234
235
# File 'lib/write_xlsx/chart/scatter.rb', line 226

def write_cat_val_axis(x_axis, y_axis, axis_ids, position) # :nodoc:
  return unless axis_ids && !axis_ids.empty?

  write_val_axis_base(
    y_axis, x_axis,
    axis_ids[1],
    axis_ids[0],
    x_axis.position || position || @val_axis_position
  )
end

#write_chart_type(params) ⇒ Object

Override the virtual superclass method with a chart specific method.



59
60
61
62
# File 'lib/write_xlsx/chart/scatter.rb', line 59

def write_chart_type(params)
  # Write the c:areaChart element.
  write_scatter_chart(params)
end

#write_plot_areaObject

Over-ridden to have 2 valAx elements for scatter charts instead of catAx/valAx.

Write the <c:plotArea> element.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/write_xlsx/chart/scatter.rb', line 122

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 elements for primary and secondary axes.
    write_chart_type(primary_axes: 1)
    write_chart_type(primary_axes: 0)

    # Write c:catAx and c:valAx elements for series using primary axes.
    write_cat_val_axis(@x_axis, @y_axis, @axis_ids, 'b')

    tmp = @horiz_val_axis
    @horiz_val_axis = 1
    write_val_axis(@x_axis, @y_axis, @axis_ids, 'l')
    @horiz_val_axis = tmp

    # Write c:valAx and c:catAx elements for series using secondary axes.
    write_cat_val_axis(@x2_axis, @y2_axis, @axis2_ids, 'b')

    @horiz_val_axis = 1
    write_val_axis(@x2_axis, @y2_axis, @axis2_ids, 'l')

    # Write the c:spPr element for the plotarea formatting.
    write_sp_pr(@plotarea)
  end
end

#write_scatter_chart(params) ⇒ Object

Write the <c:scatterChart> element.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/write_xlsx/chart/scatter.rb', line 67

def write_scatter_chart(params)
  series = if params[:primary_axes] == 1
             get_primary_axes_series
           else
             get_secondary_axes_series
           end
  return if series.empty?

  style = 'lineMarker'
  # Set the user defined chart subtype
  style = 'smoothMarker' if %w[smooth_with_markers smooth].include?(@subtype)

  # Add default formatting to the series data.
  modify_series_formatting

  @writer.tag_elements('c:scatterChart') do
    # Write the c:scatterStyle element.
    write_scatter_style(style)
    # Write the series elements.
    series.each { |s| write_series(s) }
    # Write the c:marker element.
    write_marker_value
    # Write the c:axId elements
    write_axis_ids(params)
  end
end

#write_scatter_style(val) ⇒ Object

Write the <c:scatterStyle> element.



192
193
194
# File 'lib/write_xlsx/chart/scatter.rb', line 192

def write_scatter_style(val)
  @writer.empty_tag('c:scatterStyle', [['val', val]])
end

#write_ser(series) ⇒ Object

Over-ridden to write c:xVal/c:yVal instead of c:cat/c:val elements.

Write the <c:ser> element.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/write_xlsx/chart/scatter.rb', line 99

def write_ser(series)
  @writer.tag_elements('c:ser') do
    write_ser_base(series)
    # Write the c:xVal element.
    write_x_val(series)
    # Write the c:yVal element.
    write_y_val(series)
    # Write the c:smooth element.
    if @subtype =~ /smooth/ && !series.smooth
      write_c_smooth(1)
    else
      write_c_smooth(series.smooth)
    end
  end
  @series_index += 1
end

#write_x_val(series) ⇒ Object

Write the <c:xVal> element.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/write_xlsx/chart/scatter.rb', line 153

def write_x_val(series)
  formula = series.categories
  data_id = series.cat_data_id
  data    = @formula_data[data_id]

  @writer.tag_elements('c:xVal') do
    # Check the type of cached data.
    type = get_data_type(data)

    # TODO. Can a scatter plot have non-numeric data.

    if type == 'str'
      # Write the c:numRef element.
      write_str_ref(formula, data, type)
    else
      write_num_ref(formula, data, type)
    end
  end
end

#write_y_val(series) ⇒ Object

Write the <c:yVal> element.



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/write_xlsx/chart/scatter.rb', line 176

def write_y_val(series)
  formula = series.values
  data_id = series.val_data_id
  data    = @formula_data[data_id]

  @writer.tag_elements('c:yVal') do
    # Unlike Cat axes data should only be numeric

    # Write the c:numRef element.
    write_num_ref(formula, data, 'num')
  end
end