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, #id, #index, #palette

Instance Method Summary collapse

Methods included from Utility

#absolute_char, delete_files, #put_deprecate_message, #substitute_cellref, #underline_attributes, #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, factory, #set_chartarea, #set_embedded_config_data, #set_legend, #set_plotarea, #set_style, #set_title, #set_x_axis, #set_xml_writer, #set_y_axis, #write_bar_chart

Constructor Details

#initializeScatter

Returns a new instance of Scatter.



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

def initialize
  super(self.class)
  @subtype        = 'marker_only'
  @cross_between  = 'midCat'
  @horiz_val_axis = 0
end

Instance Method Details

#modify_series_formattingObject

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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/write_xlsx/chart/scatter.rb', line 171

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][:_defined] == 0
        series[:_marker] = { :type => 'none', :_defined => 1 }
      end
    end
  end
end

#write_c_smoothObject

Write the <c:smooth> element.



156
157
158
159
160
161
162
163
164
165
# File 'lib/write_xlsx/chart/scatter.rb', line 156

def write_c_smooth
  subtype = @subtype
  val     = 1

  return unless subtype =~ /smooth/

  attributes = ['val', val]

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

#write_chart_typeObject

Override the virtual superclass method with a chart specific method.



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

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

#write_plot_areaObject

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

Write the <c:plotArea> element.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/write_xlsx/chart/scatter.rb', line 115

def write_plot_area
  @writer.tag_elements('c:plotArea') do
    # Write the c:layout element.
    write_layout
    # Write the subclass chart type element.
    write_chart_type
    # Write the c:catAx element.
    write_cat_val_axis('b', 1)
    # Write the c:catAx element.
    @horiz_val_axis = 1

    write_val_axis('l')
  end
end

#write_scatter_chartObject

Write the <c:pieChart> element.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/write_xlsx/chart/scatter.rb', line 56

def write_scatter_chart
  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.
    write_series
  end
end

#write_scatter_style(val) ⇒ Object

Write the <c:scatterStyle> element.



147
148
149
150
151
# File 'lib/write_xlsx/chart/scatter.rb', line 147

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

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

#write_ser(index, series) ⇒ Object

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

Write the <c:ser> element.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/write_xlsx/chart/scatter.rb', line 84

def write_ser(index, series)
  @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:dLbls element.
    write_d_lbls(series[:_labels])
    # Write the c:trendline element.
    write_trendline(series[:_trendline])
    # 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.



133
134
135
# File 'lib/write_xlsx/chart/scatter.rb', line 133

def write_x_val(series)
  write_val_base(series[:_categories], series[:_cat_data_id], 'c:xVal')
end

#write_y_val(series) ⇒ Object

Write the <c:yVal> element.



140
141
142
# File 'lib/write_xlsx/chart/scatter.rb', line 140

def write_y_val(series)
  write_val_base(series[:_values], series[:_val_data_id], 'c:yVal')
end