lib/write_xlsx/chart.rb in write_xlsx-0.73.0 vs lib/write_xlsx/chart.rb in write_xlsx-0.74.0
- old
+ new
@@ -210,13 +210,20 @@
#
# Set the properties of the chart legend.
#
def set_legend(params)
- @legend_position = params[:position] || 'right'
+ if params[:none]
+ @legend_position = 'none'
+ else
+ @legend_position = params[:position] || 'right'
+ end
@legend_delete_series = params[:delete_series]
@legend_font = convert_font_args(params[:font])
+
+ # Set the legend layout.
+ @legend_layout = layout_properties(params[:layout])
end
#
# Set the properties of the chart plotarea.
#
@@ -452,10 +459,33 @@
end
id
end
+ #
+ # Convert user defined layout properties to the format required internally.
+ #
+ def layout_properties(args, is_text = false)
+ return unless ptrue?(args)
+
+ properties = is_text ? [:x, :y] : [:x, :y, :width, :height]
+
+ # Check for valid properties.
+ allowable = Hash.new
+ allowable[properties.size] = nil
+
+ # Set the layout properties
+ layout = Hash.new
+ properties.each do |property|
+ value = args[property]
+ # Convert to the format used by Excel for easier testing.
+ layout[property] = sprintf("%.17g", value)
+ end
+
+ layout
+ end
+
private
#
# retun primary/secondary series by :primary_axes flag
#
@@ -625,13 +655,17 @@
line = line_properties(arg[:border]) if (arg[:border])
# Set the fill properties for the chartarea.
fill = fill_properties(arg[:fill])
- area[:_line] = line
- area[:_fill] = fill
+ # Set the plotarea layout.
+ layout = layout_properties(arg[:layout])
+ area[:_line] = line
+ area[:_fill] = fill
+ area[:_layout] = layout
+
return area
end
#
# Returns series which use the primary axes.
@@ -777,14 +811,17 @@
# Write the <c:chart> element.
#
def write_chart # :nodoc:
@writer.tag_elements('c:chart') do
# Write the chart title elements.
- if @title.formula
- write_title_formula(@title, nil)
+ if @title.none
+ # Turn off the title.
+ write_auto_title_deleted
+ elsif @title.formula
+ write_title_formula(@title, nil, nil, @title.layout, @title.overlay)
elsif @title.name
- write_title_rich(@title, nil)
+ write_title_rich(@title, nil, @title.layout, @title.overlay)
end
# Write the c:plotArea element.
write_plot_area
# Write the c:legend element.
@@ -815,11 +852,11 @@
end
def write_plot_area_base(type = nil) # :nodoc:
@writer.tag_elements('c:plotArea') do
# Write the c:layout element.
- write_layout
+ 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 the c:catAx elements for series using primary axes.
@@ -857,15 +894,43 @@
end
#
# Write the <c:layout> element.
#
- def write_layout # :nodoc:
- @writer.empty_tag('c:layout')
+ def write_layout(layout = nil, type = nil) # :nodoc:
+ tag = 'c:layout'
+
+ if layout
+ @writer.tag_elements(tag) { write_manual_layout(layout, type) }
+ else
+ @writer.empty_tag(tag)
+ end
end
#
+ # Write the <c:manualLayout> element.
+ #
+ def write_manual_layout(layout, type)
+ @writer.tag_elements('c:manualLayout') do
+ # Plotarea has a layoutTarget element.
+ @writer.empty_tag('c:layoutTarget', [ ['val', 'inner'] ]) if type == 'plot'
+
+ # Set the x, y positions.
+ @writer.empty_tag('c:xMode', [ ['val', 'edge'] ])
+ @writer.empty_tag('c:yMode', [ ['val', 'edge'] ])
+ @writer.empty_tag('c:x', [ ['val', layout[:x]] ])
+ @writer.empty_tag('c:y', [ ['val', layout[:y]] ])
+
+ # For plotarea and legend set the width and height.
+ if type != 'text'
+ @writer.empty_tag('c:w', [ ['val', layout[:width]] ])
+ @writer.empty_tag('c:h', [ ['val', layout[:height]] ])
+ end
+ end
+ end
+
+ #
# Write the chart type element. This method should be overridden by the
# subclasses.
#
def write_chart_type # :nodoc:
end
@@ -1099,13 +1164,13 @@
# Write the c:minorGridlines element.
write_minor_gridlines(x_axis.minor_gridlines)
# Write the axis title elements.
if x_axis.formula
- write_title_formula(x_axis, horiz, @x_axis)
+ write_title_formula(x_axis, horiz, @x_axis, x_axis.layout)
elsif x_axis.name
- write_title_rich(x_axis, horiz)
+ write_title_rich(x_axis, horiz, x_axis.layout)
end
# Write the c:numFmt element.
write_cat_number_format(x_axis)
@@ -1176,13 +1241,13 @@
# Write the c:minorGridlines element.
write_minor_gridlines(y_axis.minor_gridlines)
# Write the axis title elements.
if y_axis.formula
- write_title_formula(y_axis, horiz)
+ write_title_formula(y_axis, horiz, nil, y_axis.layout)
elsif y_axis.name
- write_title_rich(y_axis, horiz)
+ write_title_rich(y_axis, horiz, y_axis.layout)
end
# Write the c:numberFormat element.
write_number_format(y_axis)
@@ -1252,13 +1317,13 @@
# Write the c:minorGridlines element.
write_minor_gridlines(x_axis.minor_gridlines)
# Write the axis title elements.
if x_axis.formula
- write_title_formula(x_axis, nil)
+ write_title_formula(x_axis, nil, nil, x_axis.layout)
elsif x_axis.name
- write_title_rich(x_axis, nil)
+ write_title_rich(x_axis, nil, x_axis.layout)
end
# Write the c:numFmt element.
write_number_format(x_axis)
# Write the c:majorTickMark element.
write_major_tick_mark(x_axis.major_tick_mark)
@@ -1556,11 +1621,11 @@
@delete_series.each do |index|
# Write the c:legendEntry element.
write_legend_entry(index)
end if @delete_series
# Write the c:layout element.
- write_layout
+ write_layout(@legend_layout, 'legend')
# Write the c:txPr element.
if ptrue?(@legend_font)
write_tx_pr(nil, @legend_font)
end
# Write the c:overlay element.
@@ -1589,11 +1654,11 @@
#
# Write the <c:overlay> element.
#
def write_overlay # :nodoc:
- @writer.empty_tag('c:overlay', ['val', 1])
+ @writer.empty_tag('c:overlay', [ ['val', 1] ])
end
#
# Write the <c:plotVisOnly> element.
#
@@ -1656,29 +1721,42 @@
def write_page_setup # :nodoc:
@writer.empty_tag('c:pageSetup')
end
#
+ # Write the <c:autoTitleDeleted> element.
+ #
+ def write_auto_title_deleted
+ attributes = [ ['val', 1] ]
+
+ @writer.empty_tag('c:autoTitleDeleted', attributes)
+ end
+
+ #
# Write the <c:title> element for a rich string.
#
- def write_title_rich(title, horiz = nil) # :nodoc:
+ def write_title_rich(title, horiz = nil, layout = nil, overlay = nil) # :nodoc:
@writer.tag_elements('c:title') do
# Write the c:tx element.
write_tx_rich(title, horiz)
# Write the c:layout element.
- write_layout
+ write_layout(layout, 'text')
+ # Write the c:overlay element.
+ write_overlay if overlay
end
end
#
# Write the <c:title> element for a rich string.
#
- def write_title_formula(title, horiz = nil, axis = nil) # :nodoc:
+ def write_title_formula(title, horiz = nil, axis = nil, layout = nil, overlay = nil) # :nodoc:
@writer.tag_elements('c:title') do
# Write the c:tx element.
write_tx_formula(title.formula, axis ? axis.data_id : title.data_id)
# Write the c:layout element.
- write_layout
+ write_layout(layout, 'text')
+ # Write the c:overlay element.
+ write_overlay if overlay
# Write the c:txPr element.
write_tx_pr(horiz, axis ? axis.name_font : title.name_font)
end
end