Class: OoxmlParser::Chart

Inherits:
OOXMLDocumentObject show all
Defined in:
lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb

Constant Summary

Constants inherited from OOXMLDocumentObject

OOXMLDocumentObject::DEFAULT_DIRECTORY_FOR_MEDIA

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OOXMLDocumentObject

#==, add_to_xmls_stack, copy_file_and_rename_to_zip, copy_media_file, current_xml, dir, encrypted_file?, get_link_from_rels, media_folder, option_enabled?, unzip_file

Constructor Details

#initializeChart

Returns a new instance of Chart.



13
14
15
16
17
18
19
20
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 13

def initialize
  @type = ''
  @data = []
  @grouping = nil
  @title = nil
  @legend = nil
  @axises = []
end

Instance Attribute Details

#alternate_contentObject

Returns the value of attribute alternate_content.



11
12
13
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 11

def alternate_content
  @alternate_content
end

#axisesObject

Returns the value of attribute axises.



11
12
13
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 11

def axises
  @axises
end

#dataObject

Returns the value of attribute data.



11
12
13
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 11

def data
  @data
end

#display_labelsObject

Returns the value of attribute display_labels.



11
12
13
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 11

def display_labels
  @display_labels
end

#groupingObject

Returns the value of attribute grouping.



11
12
13
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 11

def grouping
  @grouping
end

#legendObject

Returns the value of attribute legend.



11
12
13
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 11

def legend
  @legend
end

#shape_propertiesObject

Returns the value of attribute shape_properties.



11
12
13
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 11

def shape_properties
  @shape_properties
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 11

def title
  @title
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 11

def type
  @type
end

Class Method Details

.parseObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 44

def self.parse
  chart = Chart.new
  chart_xml = Nokogiri::XML(File.open(OOXMLDocumentObject.current_xml))
  chart_xml.xpath('c:chartSpace/*', 'xmlns:c' => 'http://schemas.openxmlformats.org/drawingml/2006/chart').each do |chart_space_node_child|
    case chart_space_node_child.name
    when 'AlternateContent'
      chart.alternate_content = AlternateContent.parse(chart_space_node_child)
    when 'spPr'
      chart.shape_properties = DocxShapeProperties.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 chart.type.empty?
            case plot_area_node_child.name
            when 'barChart'
              chart.type = :bar
              bar_dir_node = plot_area_node_child.xpath('c:barDir')
              unless bar_dir_node.first.nil?
                chart.type = :column if bar_dir_node.first.attribute('val').value == 'col'
              end
              chart.parse_properties(plot_area_node_child)
            when 'lineChart'
              chart.type = :line
              chart.parse_properties(plot_area_node_child)
            when 'areaChart'
              chart.type = :area
              chart.parse_properties(plot_area_node_child)
            when 'bubbleChart'
              chart.type = :bubble
              chart.parse_properties(plot_area_node_child)
            when 'doughnutChart'
              chart.type = :doughnut
              chart.parse_properties(plot_area_node_child)
            when 'pieChart'
              chart.type = :pie
              chart.parse_properties(plot_area_node_child)
            when 'scatterChart'
              chart.type = :point
              chart.parse_properties(plot_area_node_child)
            when 'radarChart'
              chart.type = :radar
              chart.parse_properties(plot_area_node_child)
            when 'stockChart'
              chart.type = :stock
              chart.parse_properties(plot_area_node_child)
            when 'surface3DChart'
              chart.type = :surface_3d
              chart.parse_properties(plot_area_node_child)
            when 'line3DChart'
              chart.type = :line_3d
              chart.parse_properties(plot_area_node_child)
            when 'bar3DChart'
              chart.type = :bar_3d
              chart.parse_properties(plot_area_node_child)
            when 'pie3DChart'
              chart.type = :pie_3d
              chart.parse_properties(plot_area_node_child)
            end
          end
          chart_node_child.xpath('*').each do |plot_area_node_child|
            case plot_area_node_child.name
            when 'catAx'
              chart.axises << ChartAxis.parse(plot_area_node_child)
            when 'valAx'
              chart.axises << ChartAxis.parse(plot_area_node_child)
            end
          end
        when 'title'
          chart.title = ChartAxisTitle.parse(chart_node_child)
        when 'legend'
          chart.legend = ChartLegend.parse(chart_node_child)
        end
      end
    end
  end
  chart
end

Instance Method Details

#parse_properties(chart_prop_node) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ooxml_parser/common_parser/common_data/alternate_content/chart/chart.rb', line 22

def parse_properties(chart_prop_node)
  chart_prop_node.xpath('*').each do |chart_props_node_child|
    case chart_props_node_child.name
    when 'grouping'
      @grouping = chart_props_node_child.attribute('val').value.to_sym
    when 'ser'
      case @type
      when :bar, :line, :area, :pie, :doughnut, :radar, :stock, :surface_3d, :column
        val = chart_props_node_child.xpath('c:val')[0]
      when :point, :bubble
        val = chart_props_node_child.xpath('c:yVal')[0]
      else
        next
      end
      next if val.xpath('c:numRef').empty?
      @data << ChartCellsRange.parse(val.xpath('c:numRef').first).dup
    when 'dLbls'
      @display_labels = DisplayLabelsProperties.parse(chart_props_node_child)
    end
  end
end