lib/write_xlsx/chart.rb in write_xlsx-0.72.3.beta1 vs lib/write_xlsx/chart.rb in write_xlsx-0.73.0

- old
+ new

@@ -116,10 +116,11 @@ @y_scale = 1 @x_offset = 0 @y_offset = 0 @table = nil @smooth_allowed = 0 + @cross_between = 'between' set_default_properties end def set_xml_writer(filename) # :nodoc: @@ -209,12 +210,13 @@ # # Set the properties of the chart legend. # def set_legend(params) - @legend_position = params[:position] || 'right' + @legend_position = params[:position] || 'right' @legend_delete_series = params[:delete_series] + @legend_font = convert_font_args(params[:font]) end # # Set the properties of the chart plotarea. # @@ -678,11 +680,14 @@ attributes << ['sz', font[:_size]] if ptrue?(font[:_size]) attributes << ['b', font[:_bold]] if font[:_bold] attributes << ['i', font[:_italic]] if font[:_italic] attributes << ['u', 'sng'] if font[:_underline] - attributes << ['baseline', font[:_baseline]] + # Turn off baseline when testing fonts that don't have it. + if font[:_baseline] != -1 + attributes << ['baseline', font[:_baseline]] + end attributes end # # Get the font latin attributes from a font hash. @@ -1017,10 +1022,17 @@ def write_str_ref(formula, data, type) # :nodoc: write_num_or_str_ref('c:strRef', formula, data, type) end # + # Write the <c:numLit> element for literal number list elements. + # + def write_num_lit(data) + write_num_base('c:numLit', data) + end + + # # Write the <c:f> element. # def write_series_formula(formula) # :nodoc: # Strip the leading '=' from the formula. formula = formula.sub(/^=/, '') @@ -1187,11 +1199,11 @@ write_cross_axis(axis_ids_0) write_crossing(x_axis.crossing) # Write the c:crossBetween element. - write_cross_between + write_cross_between(x_axis.position_axis) # Write the c:majorUnit element. write_c_major_unit(y_axis.major_unit) # Write the c:minorUnit element. @@ -1465,12 +1477,12 @@ end # # Write the <c:crossBetween> element. # - def write_cross_between # :nodoc: - val = @cross_between || 'between' + def write_cross_between(val = nil) # :nodoc: + val ||= @cross_between @writer.empty_tag('c:crossBetween', [ ['val', val] ]) end # @@ -1511,12 +1523,12 @@ # # Write the <c:legend> element. # def write_legend # :nodoc: - position = @legend_position - overlay = false + position = @legend_position + overlay = false if @legend_delete_series && @legend_delete_series.kind_of?(Array) @delete_series = @legend_delete_series end @@ -1545,10 +1557,14 @@ # Write the c:legendEntry element. write_legend_entry(index) end if @delete_series # Write the c:layout element. write_layout + # Write the c:txPr element. + if ptrue?(@legend_font) + write_tx_pr(nil, @legend_font) + end # Write the c:overlay element. write_overlay if overlay end end @@ -2105,21 +2121,23 @@ # # Write the <c:numCache> element. # def write_num_cache(data) # :nodoc: - @writer.tag_elements('c:numCache') do + write_num_base('c:numCache', data) + end + def write_num_base(tag, data) + @writer.tag_elements(tag) do + # Write the c:formatCode element. write_format_code('General') # Write the c:ptCount element. write_pt_count(data.size) - (0..data.size - 1).each do |i| - token = data[i] - + data.each_with_index do |token, i| # Write non-numeric data as 0. if token && !(token.to_s =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) token = 0 end @@ -2354,11 +2372,17 @@ unless ptrue?(error_bars[:_endcap]) # Write the c:noEndCap element. write_no_end_cap end - if error_bars[:_type] != 'stdErr' + case error_bars[:_type] + when 'stdErr' + # Don't need to write a c:errValType tag. + when 'cust' + # Write the custom error tags. + write_custom_error(error_bars) + else # Write the c:val element. write_error_val(error_bars[:_value]) end # Write the c:spPr element. @@ -2397,9 +2421,33 @@ # # Write the <c:val> element. # def write_error_val(val) @writer.empty_tag('c:val', [ ['val', val] ]) + end + + # + # Write the custom error bars type. + # + def write_custom_error(error_bars) + if ptrue?(error_bars[:_plus_values]) + # Write the c:plus element. + @writer.tag_elements('c:plus') do + if error_bars[:_plus_values] =~ /^=/ # '=Sheet1!$A$1:$A$5' + write_num_ref(error_bars[:_plus_values], error_bars[:_plus_data], 'num') + else # [1, 2, 3] + write_num_lit(error_bars[:_plus_values]) + end + end + # Write the c:minus element. + @writer.tag_elements('c:minus') do + if error_bars[:_minus_values] =~ /^=/ # '=Sheet1!$A$1:$A$5' + write_num_ref(error_bars[:_minus_values], error_bars[:_minus_data], 'num') + else # [1, 2, 3] + write_num_lit(error_bars[:_minus_values]) + end + end + end end # # Write the <c:upDownBars> element. #