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

- old
+ new

@@ -716,10 +716,13 @@ # :major_unit # :crossing # :reverse # :log_base # :label_position + # :major_gridlines + # :minor_gridlines + # :visible # # These are explained below. Some properties are only applicable to value # or category axes, as indicated. See "Value and Category Axes" for an # explanation of Excel's distinction between the axis types. # @@ -895,56 +898,10 @@ # def set_plotarea(params) # TODO. Need to refactor for XLSX format. return - - return if params.empty? - - area = @plotarea - - # Set the plotarea visibility. - if params[:visible] - area[:_visible] = params[:visible] - return unless area[:_visible] - end - - # TODO. could move this out of if statement. - area[:_bg_color_index] = 0x08 - - # Set the chart background colour. - if params[:color] - index, rgb = get_color_indices(params[:color]) - if index - area[:_fg_color_index] = index - area[:_fg_color_rgb] = rgb - area[:_bg_color_index] = 0x08 - area[:_bg_color_rgb] = 0x000000 - end - - end - - # Set the border line colour. - if params[:line_color] - index, rgb = get_color_indices(params[:line_color]) - if index - area[:_line_color_index] = index - area[:_line_color_rgb] = rgb - end - end - - # Set the border line pattern. - if params[:line_pattern] - pattern = get_line_pattern(params[:line_pattern]) - area[:_line_pattern] = pattern - end - - # Set the border line weight. - if params[:line_weight] - weight = get_line_weight(params[:line_weight]) - area[:_line_weight] = weight - end end # # Set the properties of the chart chartarea. # @@ -956,62 +913,10 @@ # set_style() method. # def set_chartarea(params) # TODO. Need to refactor for XLSX format. return - - return if params.empty? - - area = @chartarea - - # Embedded automatic line weight has a different default value. - area[:_line_weight] = 0xFFFF if @embedded - - # Set the chart background colour. - if params[:color] - index, rgb = get_color_indices(params[:color]) - if index - area[:_fg_color_index] = index - area[:_fg_color_rgb] = rgb - area[:_bg_color_index] = 0x08 - area[:_bg_color_rgb] = 0x000000 - area[:_area_pattern] = 1 - area[:_area_options] = 0x0000 if @embedded - area[:_visible] = 1 - end - end - - # Set the border line colour. - if params[:line_color] - index, rgb = get_color_indices(params[:line_color]) - if index - area[:_line_color_index] = index - area[:_line_color_rgb] = rgb - area[:_line_pattern] = 0x00 - area[:_line_options] = 0x0000 - area[:_visible] = 1 - end - end - - # Set the border line pattern. - if params[:line_pattern] - pattern = get_line_pattern(params[:line_pattern]) - area[:_line_pattern] = pattern - area[:_line_options] = 0x0000 - area[:_line_color_index] = 0x4F unless params[:line_color] - area[:_visible] = 1 - end - - # Set the border line weight. - if params[:line_weight] - weight = get_line_weight(params[:line_weight]) - area[:_line_weight] = weight - area[:_line_options] = 0x0000 - area[:_line_pattern] = 0x00 unless params[:line_pattern] - area[:_line_color_index] = 0x4F unless params[:line_color] - area[:_visible] = 1 - end end # # Set on of the 42 built-in Excel chart styles. The default style is 2. # @@ -1058,14 +963,10 @@ # # Setup the default configuration data for an embedded chart. # def set_embedded_config_data @embedded = 1 - - # TODO. We may be able to remove this after refactoring. - - @chartarea = default_chartarea_property_for_embedded end # # Write the <c:barChart> element. # @@ -1142,13 +1043,18 @@ :_visible => arg[:visible] || 1 } # Map major_gridlines properties. if arg[:major_gridlines] && ptrue?(arg[:major_gridlines][:visible]) - axis[:_major_gridlines] = { :_visible => arg[:major_gridlines][:visible] } + axis[:_major_gridlines] = get_gridline_properties(arg[:major_gridlines]) end + # Map minor_gridlines properties. + if arg[:minor_gridlines] && ptrue?(arg[:minor_gridlines][:visible]) + axis[:_minor_gridlines] = get_gridline_properties(arg[:minor_gridlines]) + end + # Only use the first letter of bottom, top, left or right. axis[:_position] = axis[:_position].downcase[0, 1] if axis[:_position] # Set the font properties if present. axis[:_num_font] = convert_font_args(arg[:num_font]) @@ -1294,76 +1200,10 @@ sprintf("%02X%02X%02X", *rgb) end # - # Get the Excel chart index for line pattern that corresponds to the user - # defined value. - # - def get_line_pattern(value) # :nodoc: - value = value.downcase - default = 0 - - patterns = { - 0 => 5, - 1 => 0, - 2 => 1, - 3 => 2, - 4 => 3, - 5 => 4, - 6 => 7, - 7 => 6, - 8 => 8, - 'solid' => 0, - 'dash' => 1, - 'dot' => 2, - 'dash-dot' => 3, - 'dash-dot-dot' => 4, - 'none' => 5, - 'dark-gray' => 6, - 'medium-gray' => 7, - 'light-gray' => 8 - } - - if patterns.has_key(:value) - pattern = patterns[:value] - else - pattern = default - end - - pattern - end - - # - # Get the Excel chart index for line weight that corresponds to the user - # defined value. - # - def get_line_weight(value) # :nodoc: - value = value.downcase - default = 0 - - weights = { - 1 => -1, - 2 => 0, - 3 => 1, - 4 => 2, - 'hairline' => -1, - 'narrow' => 0, - 'medium' => 1, - 'wide' => 2 - } - - if weights[:value] - weight = weights[:value] - else - weight = default - end - - weight - end - - # # Convert user defined line properties to the structure required internally. # def get_line_properties(line) # :nodoc: return { :_defined => 0 } unless line @@ -1484,10 +1324,23 @@ return trendline end # + # Convert user defined gridline properties to the structure required internally. + # + def get_gridline_properties(args) + # Set the visible property for the gridline. + gridline = { :_visible => args[:visible] } + + # Set the line properties for the gridline. + gridline[:_line] = get_line_properties(args[:line]) + + gridline + end + + # # Convert user defined labels properties to the structure required internally. # def get_labels_properties(labels) # :nodoc: return nil unless labels @@ -1584,13 +1437,10 @@ end # # Setup the default properties for a chart. # def set_default_properties # :nodoc: - @chartarea = default_chartarea_property - @plotarea = default_plotarea_property - # Set the default axis properties. @x_axis[:_defaults] = { :num_format => 'General', :major_gridlines => { :visible => 0 } } @@ -2048,10 +1898,13 @@ write_axis_pos(position, y_axis[:_reverse]) # Write the c:majorGridlines element. write_major_gridlines(x_axis[:_major_gridlines]) + # Write the c:minorGridlines element. + write_minor_gridlines(x_axis[:_minor_gridlines]) + # Write the axis title elements. if title = x_axis[:_formula] write_title_formula(title, @x_axis[:_data_id], horiz, @x_axis[:_name_font]) elsif title = x_axis[:_name] write_title_rich(title, horiz, x_axis[:_name_font]) @@ -2113,10 +1966,13 @@ write_axis_pos(position, x_axis[:_reverse]) # Write the c:majorGridlines element. write_major_gridlines(y_axis[:_major_gridlines]) + # Write the c:minorGridlines element. + write_minor_gridlines(y_axis[:_minor_gridlines]) + # Write the axis title elements. if title = y_axis[:_formula] write_title_formula(title, y_axis[:_data_id], horiz, y_axis[:_name_font]) elsif title = y_axis[:_name] write_title_rich(title, horiz, y_axis[:_name_font]) @@ -2179,10 +2035,13 @@ write_axis_pos(position, y_axis[:_reverse]) # Write the c:majorGridlines element. write_major_gridlines(x_axis[:_major_gridlines]) + # Write the c:minorGridlines element. + write_minor_gridlines(x_axis[:_minor_gridlines]) + # Write the axis title elements. if title = x_axis[:_formula] write_title_formula(title, y_axis[:_data_id], horiz, x_axis[:_name_font]) elsif title = x_axis[:_name] write_title_rich(title, horiz, x_axis[:_name_font]) @@ -2282,10 +2141,13 @@ write_axis_pos(position, y_axis[:reverse]) # Write the c:majorGridlines element. write_major_gridlines(x_axis[:_major_gridlines]) + # Write the c:minorGridlines element. + write_minor_gridlines(x_axis[:_minor_gridlines]) + # Write the axis title elements. if title = x_axis[:_formula] write_title_formula(title, x_axis[:_data_id], nil, x_axis[:_name_font]) elsif title = x_axis[:_name] write_title_rich(title, nil, x_axis[:_name_font]) @@ -2563,14 +2425,38 @@ # def write_major_gridlines(gridlines) # :nodoc: return unless gridlines return unless ptrue?(gridlines[:_visible]) - @writer.empty_tag('c:majorGridlines') + if gridlines[:_line] && ptrue?(gridlines[:_line][:_defined]) + @writer.tag_elements('c:majorGridlines') do + # Write the c:spPr element. + write_sp_pr(gridlines) + end + else + @writer.empty_tag('c:majorGridlines') + end end # + # Write the <c:minorGridlines> element. + # + def write_minor_gridlines(gridlines) # :nodoc: + return unless gridlines + return unless ptrue?(gridlines[:_visible]) + + if gridlines[:_line] && ptrue?(gridlines[:_line][:_defined]) + @writer.tag_elements('c:minorGridlines') do + # Write the c:spPr element. + write_sp_pr(gridlines) + end + else + @writer.empty_tag('c:minorGridlines') + end + end + + # # Write the <c:crossBetween> element. # def write_cross_between # :nodoc: val = @cross_between || 'between' @@ -3073,10 +2959,10 @@ @writer.tag_elements('a:ln', attributes) do # Write the line fill. if ptrue?(line[:none]) # Write the a:noFill element. write_a_no_fill - else + elsif ptrue?(line[:color]) # Write the a:solidFill element. write_a_solid_fill(line) end # Write the line/dash type. if type = line[:dash_type]