class OoxmlParser::Chart

Class for working with Chart data

Attributes

alternate_content[RW]
axis_ids[R]

@return [Array, ValuedChild] array axis id

axises[RW]
data[RW]
display_labels[RW]
grouping[RW]
legend[RW]
pivot_formats[RW]

@return [PivotFormats] list of pivot formats

plot_area[RW]

@return [PlotArea] plot area data

relationships[R]

@return [Relationships] relationships of chart

series[RW]

@return [Array, Series] series of chart

shape_properties[RW]
style[R]

@return [ChartStyle] style of current chart

title[RW]
type[RW]
vary_colors[R]

@return [True, False] This element specifies that each data marker in the series has a different color. ECMA-376 (5th Edition). 21.2.2.227

view_3d[RW]

@return [View3D] properties of 3D view

Public Class Methods

new(parent: nil) click to toggle source
Calls superclass method
# File lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb, line 37
def initialize(parent: nil)
  @axis_ids = []
  @type = ''
  @data = []
  @grouping = nil
  @title = nil
  @legend = nil
  @axises = []
  @series = []
  super
end

Public Instance Methods

parse() click to toggle source

Parse Chart data @return [Chart] result of parsing

# File lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb, line 84
def parse
  chart_xml = parse_xml(root_object.current_xml)
  chart_xml.xpath('*').each do |chart_node|
    case chart_node.name
    when 'chartSpace'
      parse_chart_space(chart_node)
    end
  end
  parse_relationships
  parse_style
  self
end
parse_properties(chart_prop_node) click to toggle source

Parse properties of Chart @param chart_prop_node [Nokogiri::XML:Element] node to parse @return [void]

# File lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb, line 52
def parse_properties(chart_prop_node)
  chart_prop_node.xpath('*').each do |chart_props_node_child|
    case chart_props_node_child.name
    when 'axId'
      @axis_ids << ValuedChild.new(:integer, parent: self).parse(chart_props_node_child)
    when 'grouping'
      @grouping = chart_props_node_child.attribute('val').value.to_sym
    when 'ser'
      @series << Series.new(parent: self).parse(chart_props_node_child)
      val = case @type
            when :point, :bubble
              chart_props_node_child.xpath('c:yVal')[0]
            else
              chart_props_node_child.xpath('c:val')[0]
            end
      next unless val
      next if val.xpath('c:numRef').empty?

      @data << ChartCellsRange.new(parent: self).parse(val.xpath('c:numRef').first)
    when 'dLbls'
      @display_labels = DisplayLabelsProperties.new(parent: self).parse(chart_props_node_child)
    when 'varyColors'
      @vary_colors = option_enabled?(chart_props_node_child)
    end
  end
end

Private Instance Methods

parse_axis(node) click to toggle source

Perform parsing of axis info @param node [Nokogiri::XML:Element] node to parse @return [void]

# File lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb, line 172
def parse_axis(node)
  node.xpath('*').each do |node_child|
    case node_child.name
    when 'catAx', 'valAx'
      @axises << ChartAxis.new(parent: self).parse(node_child)
    end
  end
end
parse_chart_space(chart_space_node) click to toggle source

Parse ‘chartSpace’ node @return [nil]

# File lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb, line 101
def parse_chart_space(chart_space_node)
  chart_space_node.xpath('*').each do |chart_space_node_child|
    case chart_space_node_child.name
    when 'AlternateContent'
      @alternate_content = AlternateContent.new(parent: self).parse(chart_space_node_child)
    when 'spPr'
      @shape_properties = DocxShapeProperties.new(parent: self).parse(chart_space_node_child)
    when 'chart'
      chart_space_node_child.xpath('*').each do |chart_node_child|
        case chart_node_child.name
        when 'plotArea'
          chart_node_child.xpath('*').each do |plot_area_node_child|
            next unless type.empty?

            parse_chart_type(plot_area_node_child)
            parse_properties(plot_area_node_child)
          end
          parse_axis(chart_node_child)
          @plot_area = PlotArea.new(parent: self).parse(chart_node_child)
        when 'title'
          @title = ChartAxisTitle.new(parent: self).parse(chart_node_child)
        when 'legend'
          @legend = ChartLegend.new(parent: self).parse(chart_node_child)
        when 'view3D'
          @view_3d = View3D.new(parent: self).parse(chart_node_child)
        when 'pivotFmts'
          @pivot_formats = PivotFormats.new(parent: self).parse(chart_node_child)
        end
      end
    end
  end
end
parse_chart_type(plot_area_node_child) click to toggle source

Parse chart type and properties @return [nil]

# File lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb, line 136
def parse_chart_type(plot_area_node_child)
  case plot_area_node_child.name
  when 'barChart'
    @type = :bar
    bar_dir_node = plot_area_node_child.xpath('c:barDir')
    @type = :column if bar_dir_node.first.attribute('val').value == 'col'
  when 'lineChart'
    @type = :line
  when 'areaChart'
    @type = :area
  when 'bubbleChart'
    @type = :bubble
  when 'doughnutChart'
    @type = :doughnut
  when 'pieChart'
    @type = :pie
  when 'scatterChart'
    @type = :point
  when 'radarChart'
    @type = :radar
  when 'stockChart'
    @type = :stock
  when 'surface3DChart'
    @type = :surface_3d
  when 'line3DChart'
    @type = :line_3d
  when 'bar3DChart'
    @type = :bar_3d
  when 'pie3DChart'
    @type = :pie_3d
  end
end
parse_relationships() click to toggle source

Parse relationship of chart

# File lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb, line 182
def parse_relationships
  file_name = File.basename(root_object.current_xml)
  relationship_file = "#{root_object.unpacked_folder}" \
                      '/word/charts/' \
                      "_rels/#{file_name}.rels"

  return unless File.exist?(relationship_file)

  @relationships = Relationships.new(parent: self)
                                .parse_file(relationship_file)
end
parse_style() click to toggle source
# File lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb, line 194
def parse_style
  return unless @relationships

  chart_relationship = @relationships.target_by_type('chartStyle')
  return if chart_relationship.empty?

  chart_style_file = chart_relationship.first
  style_file = "#{root_object.unpacked_folder}" \
               "/word/charts/#{chart_style_file}"

  @style = ChartStyleFile.new(parent: self).parse(style_file)
end