Class: Writexlsx::Chart::Scatter

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

Overview

The Scatter chart module also supports the following sub-types:

markers_only (the default)
straight_with_markers
straight
smooth_with_markers
smooth

These can be specified at creation time via the add_chart() Worksheet method:

chart = workbook.add_chart(
    :type    => 'scatter',
    :subtype => 'straight_with_markers'
)

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

#embedded, #formula_data, #formula_ids, #height, #id, #index, #name, #palette, #protection, #width, #x_offset, #x_scale, #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, delete_files, #float_to_str, #pixels_to_points, #ptrue?, #put_deprecate_message, #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, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke, #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, #assemble_xml_file, #convert_font_args, factory, #get_data_id, #line_properties, #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

Constructor Details

#initialize(subtype) ⇒ Scatter

Returns a new instance of Scatter.



38
39
40
41
42
43
44
# File 'lib/write_xlsx/chart/scatter.rb', line 38

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

Instance Method Details

#modify_series_formattingObject

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



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/write_xlsx/chart/scatter.rb', line 248

def modify_series_formatting
  subtype = @subtype

  # 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.
      if series[:_line][:_defined] == 0
        series[:_line] = { :width => 2.25, :none => 1, :_defined => 1 }
      end
    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.
      if !series[:_marker] || series[:_marker][:_defined] == 0
        series[:_marker] = { :type => 'none', :_defined => 1 }
      end
    end
  end
end

#write_c_smoothObject

Write the <c:smooth> element.



233
234
235
236
237
238
239
240
241
242
# File 'lib/write_xlsx/chart/scatter.rb', line 233

def write_c_smooth
  subtype = @subtype
  val     = 1

  return unless subtype =~ /smooth/

  attributes = ['val', val]

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

#write_chart_type(params) ⇒ Object

Override the virtual superclass method with a chart specific method.



49
50
51
52
# File 'lib/write_xlsx/chart/scatter.rb', line 49

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

#write_d_pt_point(index, point) ⇒ Object

Write an individual <c:dPt> element. Override the parent method to add markers.



278
279
280
281
282
283
284
285
286
287
# File 'lib/write_xlsx/chart/scatter.rb', line 278

def write_d_pt_point(index, point)
  @writer.tag_elements('c:dPt') do
    # Write the c:idx element.
    write_idx(index)
    @writer.tag_elements('c:marker') do
      # Write the c:spPr element.
      write_sp_pr(point)
    end
  end
end

#write_plot_areaObject

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

Write the <c:plotArea> element.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/write_xlsx/chart/scatter.rb', line 136

def write_plot_area
  @writer.tag_elements('c:plotArea') do
    # Write the c:layout element.
    write_layout

    # 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   => @x_axis,
                       :y_axis   => @y_axis,
                       :axis_ids => @axis_ids,
                       :position => 'b'
                       )
    tmp = @horiz_val_axis
    @horiz_val_axis = 1
    write_val_axis(
                   :x_axis   => @x_axis,
                   :y_axis   => @y_axis,
                   :axis_ids => @axis_ids,
                   :position => 'l'
                   )
    @horiz_val_axis = tmp

    # Write c:valAx and c:catAx elements for series using secondary axes
    write_cat_val_axis(
                       :x_axis   => @x2_axis,
                       :y_axis   => @y2_axis,
                       :axis_ids => @axis2_ids,
                       :position => 'b'
                       )
    @horiz_val_axis = 1
    write_val_axis(
                   :x_axis   => @x2_axis,
                   :y_axis   => @y2_axis,
                   :axis_ids => @axis2_ids,
                   :position => '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.



57
58
59
60
61
62
63
64
65
66
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
# File 'lib/write_xlsx/chart/scatter.rb', line 57

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

  style   = 'lineMarker'
  subtype = @subtype

  # Set the user defined chart subtype
  case subtype
  when 'marker_only', 'straight_with_markers', 'straight'
    style = 'lineMarker'
  when 'smooth_with_markers', 'smooth'
    style = 'smoothMarker'
  end

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



224
225
226
227
228
# File 'lib/write_xlsx/chart/scatter.rb', line 224

def write_scatter_style(val)
  attributes = ['val', val]

  @writer.empty_tag('c:scatterStyle', attributes)
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.



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

def write_ser(series)
  index = @series_index
  @series_index += 1

  @writer.tag_elements('c:ser') do
    # Write the c:idx element.
    write_idx(index)
    # Write the c:order element.
    write_order(index)
    # Write the series name.
    write_series_name(series)
    # Write the c:spPr element.
    write_sp_pr(series)
    # Write the c:marker element.
    write_marker(series[:_marker])
    # Write the c:dPt element.
    write_d_pt(series[:_points])
    # Write the c:dLbls element.
    write_d_lbls(series[:_labels])
    # Write the c:trendline element.
    write_trendline(series[:_trendline])
    # Write the c:errBars element.
    write_error_bars(series[:_error_bars])
    # Write the c:xVal element.
    write_x_val(series)
    # Write the c:yVal element.
    write_y_val(series)
    # Write the c:smooth element.
    write_c_smooth
  end
end

#write_x_val(series) ⇒ Object

Write the <c:xVal> element.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/write_xlsx/chart/scatter.rb', line 185

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.



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/write_xlsx/chart/scatter.rb', line 208

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