lib/write_xlsx/chart.rb in write_xlsx-0.57.0 vs lib/write_xlsx/chart.rb in write_xlsx-0.58.0

- old
+ new

@@ -492,10 +492,12 @@ @formula_ids = {} @formula_data = [] @horiz_cat_axis = 0 @horiz_val_axis = 1 @protection = 0 + @chartarea = {} + @plotarea = {} @x_axis = {} @y_axis = {} @x2_axis = {} @y2_axis = {} @name = '' @@ -529,10 +531,13 @@ write_protection # Write the c:chart element. write_chart + # Write the c:spPr element for the chartarea formatting. + write_sp_pr(@chartarea) + # Write the c:printSettings element. write_print_settings if @embedded && @embedded != 0 end # Close the XML writer object and filehandle. @@ -895,13 +900,12 @@ # This method isn't implemented yet and is only available in # writeexcel gem. However, it can be simulated using the # set_style() method. # def set_plotarea(params) - - # TODO. Need to refactor for XLSX format. - return + # Convert the user defined properties to internal properties. + @plotarea = get_area_properties(params) end # # Set the properties of the chart chartarea. # @@ -911,12 +915,12 @@ # This method isn't implemented yet and is only available in # writeexcel gem. However, it can be simulated using the # set_style() method. # def set_chartarea(params) - # TODO. Need to refactor for XLSX format. - return + # Convert the user defined properties to internal properties. + @chartarea = get_area_properties(params) end # # Set on of the 42 built-in Excel chart styles. The default style is 2. # @@ -1200,10 +1204,62 @@ sprintf("%02X%02X%02X", *rgb) end # + # Get the Spreadsheet::WriteExcel line pattern for backward compatibility. + # + def get_swe_line_pattern(val) + value = val.downcase + default = 'solid' + + patterns = { + 0 => 'solid', + 1 => 'dash', + 2 => 'dot', + 3 => 'dash_dot', + 4 => 'long_dash_dot_dot', + 5 => 'none', + 6 => 'solid', + 7 => 'solid', + 8 => 'solid', + 'solid' => 'solid', + 'dash' => 'dash', + 'dot' => 'dot', + 'dash-dot' => 'dash_dot', + 'dash-dot-dot' => 'long_dash_dot_dot', + 'none' => 'none', + 'dark-gray' => 'solid', + 'medium-gray' => 'solid', + 'light-gray' => 'solid' + } + + patterns[value] || default + end + + # + # Get the Spreadsheet::WriteExcel line weight for backward compatibility. + # + def get_swe_line_weight(val) + value = val.downcase + default = 1 + + weights = { + 1 => 0.25, + 2 => 1, + 3 => 2, + 4 => 3, + 'hairline' => 0.25, + 'narrow' => 1, + 'medium' => 2, + 'wide' => 3 + } + + weights[value] || default + end + + # # Convert user defined line properties to the structure required internally. # def get_line_properties(line) # :nodoc: return { :_defined => 0 } unless line @@ -1366,10 +1422,55 @@ end labels end + # + # Convert user defined area properties to the structure required internally. + # + def get_area_properties(arg) # :nodoc: + area = {} + + # Map deprecated Spreadsheet::WriteExcel fill colour. + arg[:fill] = { :color => arg[:color] } if arg[:color] + + # Map deprecated Spreadsheet::WriteExcel line_weight. + if arg[:line_weight] + width = get_swe_line_weight(arg[:line_weight]) + arg[:border] = { :width => width } + end + + # Map deprecated Spreadsheet::WriteExcel line_pattern. + if arg[:line_pattern] + pattern = get_swe_line_pattern(arg[:line_pattern]) + if pattern == 'none' + arg[:border] = { :none => 1 } + else + arg[:border][:dash_type] = pattern + end + end + + # Map deprecated Spreadsheet::WriteExcel line colour. + arg[:border][:color] = arg[:line_color] if arg[:line_color] + + # Handle Excel::Writer::XLSX style properties. + + # Set the line properties for the chartarea. + line = get_line_properties(arg[:line]) + + # Allow 'border' as a synonym for 'line'. + line = get_line_properties(arg[:border]) if (arg[:border]) + + # Set the fill properties for the chartarea. + fill = get_fill_properties(arg[:fill]) + + area[:_line] = line + area[:_fill] = fill + + return area + end + def value_or_raise(hash, key, msg) raise "Unknown #{msg} '#{key}'" unless hash[key.to_sym] hash[key.to_sym] end @@ -1629,10 +1730,13 @@ :y_axis => @y2_axis, :axis_ids => @axis2_ids } write_val_axis(params) write_cat_or_date_axis(params, type) + + # Write the c:spPr element for the plotarea formatting. + write_sp_pr(@plotarea) end end def write_cat_or_date_axis(params, type) if type == :stock @@ -2933,10 +3037,10 @@ # Write the a:solidFill element. write_a_solid_fill(series[:_fill]) end end # Write the a:ln element. - write_a_ln(series[:_line]) if series[:_line] && series[:_line][:_defined] != 0 + write_a_ln(series[:_line]) if series[:_line] && ptrue?(series[:_line][:_defined]) end end # # Write the <a:ln> element.