lib/write_xlsx/chart/scatter.rb in write_xlsx-0.0.4 vs lib/write_xlsx/chart/scatter.rb in write_xlsx-0.51.0
- old
+ new
@@ -33,29 +33,37 @@
# )
#
class Scatter < self
include Writexlsx::Utility
- def initialize
- super(self.class)
- @subtype = 'marker_only'
- @cross_between = 'midCat'
- @horiz_val_axis = 0
+ def initialize(subtype)
+ super(subtype)
+ @subtype = subtype || 'marker_only'
+ @cross_between = 'midCat'
+ @horiz_val_axis = 0
+ @val_axis_position = 'b'
end
#
# Override the virtual superclass method with a chart specific method.
#
- def write_chart_type
+ def write_chart_type(params)
# Write the c:areaChart element.
- write_scatter_chart
+ write_scatter_chart(params)
end
#
- # Write the <c:pieChart> element.
+ # Write the <c:scatterChart> element.
#
- def write_scatter_chart
+ 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
@@ -70,20 +78,29 @@
@writer.tag_elements('c:scatterChart') do
# Write the c:scatterStyle element.
write_scatter_style(style)
# Write the series elements.
- write_series
+ 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
#
# Over-ridden to write c:xVal/c:yVal instead of c:cat/c:val elements.
#
# Write the <c:ser> element.
#
- def write_ser(index, series)
+ 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)
@@ -114,33 +131,86 @@
#
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.
+
+ # 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_val_axis('l')
+ # 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'
+ )
end
end
#
# Write the <c:xVal> element.
#
def write_x_val(series)
- write_val_base(series[:_categories], series[:_cat_data_id], 'c:xVal')
+ 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 the <c:yVal> element.
#
def write_y_val(series)
- write_val_base(series[:_values], series[:_val_data_id], 'c:yVal')
+ 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
#
# Write the <c:scatterStyle> element.
#
@@ -185,10 +255,10 @@
# 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
+ if !series[:_marker] || series[:_marker][:_defined] == 0
series[:_marker] = { :type => 'none', :_defined => 1 }
end
end
end
end