axlsx 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. data/Rakefile +13 -0
  2. data/lib/axlsx.rb +38 -0
  3. data/lib/axlsx/content_type/content_type.rb +23 -0
  4. data/lib/axlsx/content_type/default.rb +32 -0
  5. data/lib/axlsx/content_type/override.rb +30 -0
  6. data/lib/axlsx/doc_props/app.rb +148 -0
  7. data/lib/axlsx/doc_props/core.rb +34 -0
  8. data/lib/axlsx/drawing/axis.rb +78 -0
  9. data/lib/axlsx/drawing/bar_3D_chart.rb +138 -0
  10. data/lib/axlsx/drawing/bar_series.rb +91 -0
  11. data/lib/axlsx/drawing/cat_axis.rb +58 -0
  12. data/lib/axlsx/drawing/chart.rb +120 -0
  13. data/lib/axlsx/drawing/drawing.rb +121 -0
  14. data/lib/axlsx/drawing/graphic_frame.rb +55 -0
  15. data/lib/axlsx/drawing/marker.rb +57 -0
  16. data/lib/axlsx/drawing/pie_3D_chart.rb +59 -0
  17. data/lib/axlsx/drawing/pie_series.rb +88 -0
  18. data/lib/axlsx/drawing/scaling.rb +53 -0
  19. data/lib/axlsx/drawing/series.rb +70 -0
  20. data/lib/axlsx/drawing/title.rb +69 -0
  21. data/lib/axlsx/drawing/two_cell_anchor.rb +88 -0
  22. data/lib/axlsx/drawing/val_axis.rb +34 -0
  23. data/lib/axlsx/drawing/view_3D.rb +72 -0
  24. data/lib/axlsx/package.rb +181 -0
  25. data/lib/axlsx/rels/relationship.rb +43 -0
  26. data/lib/axlsx/rels/relationships.rb +25 -0
  27. data/lib/axlsx/stylesheet/border.rb +52 -0
  28. data/lib/axlsx/stylesheet/border_pr.rb +65 -0
  29. data/lib/axlsx/stylesheet/cell_alignment.rb +96 -0
  30. data/lib/axlsx/stylesheet/cell_protection.rb +33 -0
  31. data/lib/axlsx/stylesheet/cell_style.rb +60 -0
  32. data/lib/axlsx/stylesheet/color.rb +57 -0
  33. data/lib/axlsx/stylesheet/fill.rb +31 -0
  34. data/lib/axlsx/stylesheet/font.rb +128 -0
  35. data/lib/axlsx/stylesheet/gradient_fill.rb +70 -0
  36. data/lib/axlsx/stylesheet/gradient_stop.rb +31 -0
  37. data/lib/axlsx/stylesheet/num_fmt.rb +61 -0
  38. data/lib/axlsx/stylesheet/pattern_fill.rb +64 -0
  39. data/lib/axlsx/stylesheet/styles.rb +296 -0
  40. data/lib/axlsx/stylesheet/table_style.rb +44 -0
  41. data/lib/axlsx/stylesheet/table_style_element.rb +66 -0
  42. data/lib/axlsx/stylesheet/table_styles.rb +39 -0
  43. data/lib/axlsx/stylesheet/xf.rb +117 -0
  44. data/lib/axlsx/util/constants.rb +189 -0
  45. data/lib/axlsx/util/simple_typed_list.rb +150 -0
  46. data/lib/axlsx/util/validators.rb +132 -0
  47. data/lib/axlsx/workbook/workbook.rb +130 -0
  48. data/lib/axlsx/workbook/worksheet/cell.rb +184 -0
  49. data/lib/axlsx/workbook/worksheet/row.rb +92 -0
  50. data/lib/axlsx/workbook/worksheet/worksheet.rb +194 -0
  51. data/lib/schema/dc.xsd +118 -0
  52. data/lib/schema/dcmitype.xsd +50 -0
  53. data/lib/schema/dcterms.xsd +331 -0
  54. data/lib/schema/dml-chart.xsd +1499 -0
  55. data/lib/schema/dml-chartDrawing.xsd +146 -0
  56. data/lib/schema/dml-compatibility.xsd +14 -0
  57. data/lib/schema/dml-diagram.xsd +1091 -0
  58. data/lib/schema/dml-lockedCanvas.xsd +11 -0
  59. data/lib/schema/dml-main.xsd +3048 -0
  60. data/lib/schema/dml-picture.xsd +23 -0
  61. data/lib/schema/dml-spreadsheetDrawing.xsd +185 -0
  62. data/lib/schema/dml-wordprocessingDrawing.xsd +185 -0
  63. data/lib/schema/opc-contentTypes.xsd +42 -0
  64. data/lib/schema/opc-coreProperties.xsd +50 -0
  65. data/lib/schema/opc-digSig.xsd +49 -0
  66. data/lib/schema/opc-relationships.xsd +33 -0
  67. data/lib/schema/pml.xsd +1676 -0
  68. data/lib/schema/shared-additionalCharacteristics.xsd +28 -0
  69. data/lib/schema/shared-bibliography.xsd +144 -0
  70. data/lib/schema/shared-commonSimpleTypes.xsd +166 -0
  71. data/lib/schema/shared-customXmlDataProperties.xsd +25 -0
  72. data/lib/schema/shared-customXmlSchemaProperties.xsd +18 -0
  73. data/lib/schema/shared-documentPropertiesCustom.xsd +59 -0
  74. data/lib/schema/shared-documentPropertiesExtended.xsd +56 -0
  75. data/lib/schema/shared-documentPropertiesVariantTypes.xsd +195 -0
  76. data/lib/schema/shared-math.xsd +582 -0
  77. data/lib/schema/shared-relationshipReference.xsd +25 -0
  78. data/lib/schema/sml.xsd +4430 -0
  79. data/lib/schema/vml-main.xsd +569 -0
  80. data/lib/schema/vml-officeDrawing.xsd +509 -0
  81. data/lib/schema/vml-presentationDrawing.xsd +12 -0
  82. data/lib/schema/vml-spreadsheetDrawing.xsd +108 -0
  83. data/lib/schema/vml-wordprocessingDrawing.xsd +96 -0
  84. data/lib/schema/wml.xsd +3644 -0
  85. data/lib/schema/xml.xsd +117 -0
  86. data/test/content_type/tc_content_type.rb +81 -0
  87. data/test/content_type/tc_content_type.rb~ +81 -0
  88. data/test/content_type/tc_default.rb +40 -0
  89. data/test/content_type/tc_default.rb~ +40 -0
  90. data/test/content_type/tc_override.rb +40 -0
  91. data/test/content_type/tc_override.rb~ +40 -0
  92. data/test/doc_props/tc_app.rb +19 -0
  93. data/test/doc_props/tc_app.rb~ +19 -0
  94. data/test/doc_props/tc_core.rb +34 -0
  95. data/test/drawing/tc_axis.rb +39 -0
  96. data/test/drawing/tc_axis.rb~ +0 -0
  97. data/test/drawing/tc_bar_3D_chart.rb +66 -0
  98. data/test/drawing/tc_bar_3D_chart.rb~ +4 -0
  99. data/test/drawing/tc_bar_series.rb +34 -0
  100. data/test/drawing/tc_bar_series.rb~ +31 -0
  101. data/test/drawing/tc_cat_axis.rb +32 -0
  102. data/test/drawing/tc_cat_axis.rb~ +39 -0
  103. data/test/drawing/tc_chart.rb +59 -0
  104. data/test/drawing/tc_chart.rb~ +58 -0
  105. data/test/drawing/tc_drawing.rb +71 -0
  106. data/test/drawing/tc_graphic_frame.rb +26 -0
  107. data/test/drawing/tc_graphic_frame.rb~ +21 -0
  108. data/test/drawing/tc_marker.rb +45 -0
  109. data/test/drawing/tc_marker.rb~ +26 -0
  110. data/test/drawing/tc_pie_3D_chart.rb +33 -0
  111. data/test/drawing/tc_pie_3D_chart.rb~ +58 -0
  112. data/test/drawing/tc_pie_series.rb +35 -0
  113. data/test/drawing/tc_pie_series.rb~ +26 -0
  114. data/test/drawing/tc_scaling.rb +37 -0
  115. data/test/drawing/tc_scaling.rb~ +45 -0
  116. data/test/drawing/tc_series.rb +24 -0
  117. data/test/drawing/tc_series.rb~ +31 -0
  118. data/test/drawing/tc_title.rb +34 -0
  119. data/test/drawing/tc_title.rb~ +37 -0
  120. data/test/drawing/tc_two_cell_anchor.rb +37 -0
  121. data/test/drawing/tc_two_cell_anchor.rb~ +35 -0
  122. data/test/drawing/tc_val_axis.rb +20 -0
  123. data/test/drawing/tc_val_axis.rb~ +32 -0
  124. data/test/drawing/tc_view_3D.rb +46 -0
  125. data/test/drawing/tc_view_3D.rb~ +37 -0
  126. data/test/rels/tc_relationship.rb +16 -0
  127. data/test/rels/tc_relationship.rb~ +39 -0
  128. data/test/rels/tc_relationships.rb +32 -0
  129. data/test/rels/tc_relationships.rb~ +37 -0
  130. data/test/stylesheet/tc_border.rb +38 -0
  131. data/test/stylesheet/tc_border.rb~ +31 -0
  132. data/test/stylesheet/tc_border_pr.rb +33 -0
  133. data/test/stylesheet/tc_border_pr.rb~ +31 -0
  134. data/test/stylesheet/tc_cell_alignment.rb +77 -0
  135. data/test/stylesheet/tc_cell_alignment.rb~ +38 -0
  136. data/test/stylesheet/tc_cell_protection.rb +30 -0
  137. data/test/stylesheet/tc_cell_protection.rb~ +77 -0
  138. data/test/stylesheet/tc_cell_style.rb +58 -0
  139. data/test/stylesheet/tc_cell_style.rb~ +30 -0
  140. data/test/stylesheet/tc_color.rb +38 -0
  141. data/test/stylesheet/tc_color.rb~ +38 -0
  142. data/test/stylesheet/tc_fill.rb +19 -0
  143. data/test/stylesheet/tc_fill.rb~ +19 -0
  144. data/test/stylesheet/tc_font.rb +114 -0
  145. data/test/stylesheet/tc_font.rb~ +19 -0
  146. data/test/stylesheet/tc_gradient_fill.rb +65 -0
  147. data/test/stylesheet/tc_gradient_fill.rb~ +114 -0
  148. data/test/stylesheet/tc_gradient_stop.rb +32 -0
  149. data/test/stylesheet/tc_gradient_stop.rb~ +65 -0
  150. data/test/stylesheet/tc_num_fmt.rb +31 -0
  151. data/test/stylesheet/tc_num_fmt.rb~ +32 -0
  152. data/test/stylesheet/tc_pattern_fill.rb +38 -0
  153. data/test/stylesheet/tc_pattern_fill.rb~ +31 -0
  154. data/test/stylesheet/tc_styles.rb +64 -0
  155. data/test/stylesheet/tc_table_style.rb +37 -0
  156. data/test/stylesheet/tc_table_style.rb~ +38 -0
  157. data/test/stylesheet/tc_table_style_element.rb +37 -0
  158. data/test/stylesheet/tc_table_style_element.rb~ +37 -0
  159. data/test/stylesheet/tc_table_styles.rb +30 -0
  160. data/test/stylesheet/tc_table_styles.rb~ +37 -0
  161. data/test/stylesheet/tc_xf.rb +121 -0
  162. data/test/stylesheet/tc_xf.rb~ +30 -0
  163. data/test/tc_app.rb~ +19 -0
  164. data/test/tc_border_pr.rb~ +21 -0
  165. data/test/tc_package.rb +70 -0
  166. data/test/tc_package.rb~ +64 -0
  167. data/test/tc_pie_3D_chart.rb~ +66 -0
  168. data/test/tc_relationships.rb~ +37 -0
  169. data/test/tc_series.rb~ +31 -0
  170. data/test/tc_styles.rb~ +64 -0
  171. data/test/tc_validators.rb~ +77 -0
  172. data/test/tc_worksheet.rb~ +85 -0
  173. data/test/util/tc_simple_typed_list.rb +66 -0
  174. data/test/util/tc_validators.rb +76 -0
  175. data/test/workbook/tc_workbook.rb +53 -0
  176. data/test/workbook/worksheet/tc_cell.rb +78 -0
  177. data/test/workbook/worksheet/tc_row.rb +30 -0
  178. data/test/workbook/worksheet/tc_worksheet.rb +85 -0
  179. metadata +378 -0
@@ -0,0 +1,91 @@
1
+ module Axlsx
2
+ # A BarSeries defines the title, data and labels for bar charts
3
+ # @note The recommended way to manage series is to use Chart#add_series
4
+ # @see Worksheet#add_chart
5
+ # @see Chart#add_series
6
+ class BarSeries < Series
7
+
8
+
9
+ # The data for this series.
10
+ # @return [Array, SimpleTypedList]
11
+ attr_reader :data
12
+
13
+ # The labels for this series.
14
+ # @return [Array, SimpleTypedList]
15
+ attr_reader :labels
16
+
17
+ # The shabe of the bars or columns
18
+ # must be one of [:percentStacked, :clustered, :standard, :stacked]
19
+ # @return [Symbol]
20
+ attr_accessor :shape
21
+
22
+ # Creates a new series
23
+ # @option options [Array, SimpleTypedList] data
24
+ # @option options [Array, SimpleTypedList] labels
25
+ # @option options [String] title
26
+ # @option options [String] shape
27
+ # @param [Chart] chart
28
+ def initialize(chart, options={})
29
+ @shape = :box
30
+ super(chart, options)
31
+ self.data = options[:data] || []
32
+ self.labels = options[:labels] || []
33
+ end
34
+
35
+ def shape=(v)
36
+ RestrictionValidator.validate "BarSeries.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v
37
+ @shape = v
38
+ end
39
+
40
+ # Serializes the series
41
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
42
+ # @return [String]
43
+ def to_xml(xml)
44
+ super(xml) do |xml|
45
+ if !labels.empty?
46
+ xml.send('c:cat') {
47
+ xml.send('c:strRef') {
48
+ xml.send('c:f', Axlsx::cell_range(labels))
49
+ xml.send('c:strCache') {
50
+ xml.send('c:ptCount', :val=>labels.size)
51
+ labels.each_with_index do |cell, index|
52
+ v = cell.is_a?(Cell) ? cell.value : cell
53
+ xml.send('c:pt', :idx=>index) {
54
+ xml.send('c:v', v)
55
+ }
56
+ end
57
+ }
58
+ }
59
+ }
60
+ end
61
+ xml.send('c:val') {
62
+ xml.send('c:numRef') {
63
+ xml.send('c:f', Axlsx::cell_range(data))
64
+ xml.send('c:numCache') {
65
+ xml.send('c:formatCode', 'General')
66
+ xml.send('c:ptCount', :val=>data.size)
67
+ data.each_with_index do |cell, index|
68
+ v = cell.is_a?(Cell) ? cell.value : cell
69
+ xml.send('c:pt', :idx=>index) {
70
+ xml.send('c:v', v)
71
+ }
72
+ end
73
+ }
74
+ }
75
+ }
76
+ end
77
+ end
78
+
79
+
80
+ private
81
+
82
+
83
+ # assigns the data for this series
84
+ def data=(v) DataTypeValidator.validate "Series.data", [Array, SimpleTypedList], v; @data = v; end
85
+
86
+ # assigns the labels for this series
87
+ def labels=(v) DataTypeValidator.validate "Series.labels", [Array, SimpleTypedList], v; @labels = v; end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,58 @@
1
+ module Axlsx
2
+ #A CatAxis object defines a chart category axis
3
+ class CatAxis < Axis
4
+ # From the docs: This element specifies that this axis is a date or text axis based on the data that is used for the axis labels, not a specific choice.
5
+ # @return [Boolean]
6
+ attr_accessor :auto
7
+
8
+ # specifies how the perpendicular axis is crossed
9
+ # must be one of [:ctr, :l, :r]
10
+ # @return [Symbol]
11
+ attr_accessor :lblAlgn
12
+
13
+ # The offset of the labels
14
+ # must be between a string between 0 and 1000
15
+ # @return [Integer]
16
+ attr_accessor :lblOffset
17
+
18
+ # regex for validating label offset
19
+ LBL_OFFSET_REGEX = /0*(([0-9])|([1-9][0-9])|([1-9][0-9][0-9])|1000)%/
20
+
21
+ # Creates a new CatAxis object
22
+ # @param [Integer] axId the id of this axis
23
+ # @param [Integer] crossAx the id of the perpendicular axis
24
+ # @option options [Symbol] axPos
25
+ # @option options [Symbol] tickLblPos
26
+ # @option options [Symbol] crosses
27
+ # @option options [Boolean] auto
28
+ # @option options [Symbol] lblAlgn
29
+ # @option options [Integer] lblOffset
30
+ def initialize(axId, crossAx, options={})
31
+ super(axId, crossAx, options)
32
+ self.auto = true
33
+ self.lblAlgn = :ctr
34
+ self.lblOffset = "100%"
35
+ options.each do |o|
36
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
37
+ end
38
+ end
39
+
40
+ def auto=(v) Axlsx::validate_boolean(v); @auto = v; end
41
+ def lblAlgn=(v) RestrictionValidator.validate "#{self.class}.lblAlgn", [:ctr, :l, :r], v; @lblAlgn = v; end
42
+ def lblOffset=(v) RegexValidator.validate "#{self.class}.lblOffset", LBL_OFFSET_REGEX, v; @lblOffset = v; end
43
+
44
+ # Serializes the category axis
45
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
46
+ # @return [String]
47
+ def to_xml(xml)
48
+ xml.send('c:catAx') {
49
+ super(xml)
50
+ xml.send('c:auto', :val=>@auto)
51
+ xml.send('c:lblAlgn', :val=>@lblAlgn)
52
+ xml.send('c:lblOffset', :val=>@lblOffset)
53
+ }
54
+ end
55
+ end
56
+
57
+
58
+ end
@@ -0,0 +1,120 @@
1
+ module Axlsx
2
+ # A Chart is the superclass for specific charts
3
+ # @note Worksheet#add_chart is the recommended way to create charts for your worksheets.
4
+ class Chart
5
+
6
+ # The title object for the chart.
7
+ # @return [Title]
8
+ attr_accessor :title
9
+
10
+ # The 3D view properties for the chart
11
+ attr_accessor :view3D
12
+
13
+ # A reference to the graphic frame that owns this chart
14
+ # @return [GraphicFrame]
15
+ attr_reader :graphic_frame
16
+
17
+ # A collection of series objects that are applied to the chart
18
+ # @return [SimpleTypedList]
19
+ attr_reader :series
20
+
21
+ # The type of series to use for this chart
22
+ # @return [Series]
23
+ attr_reader :series_type
24
+
25
+ # The index of this chart in the workbooks charts collection
26
+ # @return [Integer]
27
+ attr_reader :index
28
+
29
+ # The part name for this chart
30
+ # @return [String]
31
+ attr_reader :pn
32
+
33
+ #TODO data labels!
34
+ #attr_accessor :dLabls
35
+
36
+ # The starting marker for this chart
37
+ # @return [Marker]
38
+ attr_reader :start_at
39
+
40
+ # The ending marker for this chart
41
+ # @return [Marker]
42
+ attr_reader :end_at
43
+
44
+ # Show the legend in the chart
45
+ # @return [Boolean]
46
+ attr_accessor :show_legend
47
+
48
+ # Creates a new chart object
49
+ # @param [GraphicalFrame] frame The frame that holds this chart.
50
+ # @option options [Cell, String] title
51
+ # @option options [Boolean] show_legend
52
+ def initialize(frame, options={})
53
+ @graphic_frame=frame
54
+ @graphic_frame.anchor.drawing.worksheet.workbook.charts << self
55
+ @series = SimpleTypedList.new Series
56
+ @show_legend = true
57
+ @series_type = Series
58
+ options.each do |o|
59
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
60
+ end
61
+ yield self if block_given?
62
+ end
63
+
64
+ def index
65
+ @graphic_frame.anchor.drawing.worksheet.workbook.charts.index(self)
66
+ end
67
+
68
+ def pn
69
+ "#{CHART_PN % (index+1)}"
70
+ end
71
+
72
+ def view3D=(v) DataTypeValidator.validate "#{self.class}.view3D", View3D, v; @view3D = v; end
73
+
74
+ def title=(v)
75
+ v = Title.new(v) if v.is_a?(String) || v.is_a?(Cell)
76
+ DataTypeValidator.validate "#{self.class}.title", Title, v
77
+ @title = v
78
+ end
79
+
80
+ def show_legend=(v) Axlsx::validate_boolean(v); @show_legend = v; end
81
+
82
+ # Adds a new series to the chart's series collection.
83
+ # @return [Series]
84
+ # @see Series
85
+ def add_series(options={})
86
+ @series_type.new(self, options)
87
+ @series.last
88
+ end
89
+
90
+ # Chart Serialization
91
+ # serializes the chart
92
+ def to_xml
93
+ builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
94
+ xml.send('c:chartSpace',:'xmlns:c' => XML_NS_C, :'xmlns:a' => XML_NS_A) {
95
+ xml.send('c:chart') {
96
+ @title.to_xml(xml) unless @title.nil?
97
+ @view3D.to_xml(xml) unless @view3D.nil?
98
+ xml.send('c:plotArea') {
99
+ xml.send('c:layout')
100
+ yield xml if block_given?
101
+ }
102
+ if @show_legend
103
+ xml.send('c:legend') {
104
+ xml.send('c:legendPos', :val => "r")
105
+ xml.send('c:layout')
106
+ }
107
+ end
108
+ }
109
+ }
110
+ end
111
+ builder.to_xml
112
+ end
113
+
114
+ private
115
+
116
+ def start_at=(v) DataTypeValidator.validate "#{self.class}.start_at", Marker, v; @start_at = v; end
117
+ def end_at=(v) DataTypeValidator.validate "#{self.class}.end_at", Marker, v; @end_at = v; end
118
+
119
+ end
120
+ end
@@ -0,0 +1,121 @@
1
+ module Axlsx
2
+ require 'axlsx/drawing/series.rb'
3
+ require 'axlsx/drawing/pie_series.rb'
4
+ require 'axlsx/drawing/bar_series.rb'
5
+
6
+ require 'axlsx/drawing/axis.rb'
7
+ require 'axlsx/drawing/cat_axis.rb'
8
+ require 'axlsx/drawing/val_axis.rb'
9
+ require 'axlsx/drawing/view_3D.rb'
10
+ require 'axlsx/drawing/scaling.rb'
11
+ require 'axlsx/drawing/title.rb'
12
+
13
+ require 'axlsx/drawing/graphic_frame.rb'
14
+ require 'axlsx/drawing/marker.rb'
15
+ require 'axlsx/drawing/two_cell_anchor.rb'
16
+
17
+ require 'axlsx/drawing/chart.rb'
18
+ require 'axlsx/drawing/pie_3D_chart.rb'
19
+ require 'axlsx/drawing/bar_3D_chart.rb'
20
+
21
+ # A Drawing is a canvas for charts. Each worksheet has a single drawing that can specify multiple anchors which reference charts.
22
+ # @note The recommended way to manage drawings is to use the Worksheet.add_chart method, specifying the chart class, start and end marker locations.
23
+ # @see Worksheet#add_chart
24
+ # @see TwoCellAnchor
25
+ # @see Chart
26
+ class Drawing
27
+
28
+ # The worksheet that owns the drawing
29
+ # @return [Worksheet]
30
+ attr_reader :worksheet
31
+
32
+
33
+ # A collection of anchors for this drawing
34
+ # @return [SimpleTypedList]
35
+ attr_reader :anchors
36
+
37
+ # An array of charts that are associated with this drawing's anchors
38
+ # @return [Array]
39
+ attr_reader :charts
40
+
41
+ # The index of this drawing in the owning workbooks's drawings collection.
42
+ # @return [Integer]
43
+ attr_reader :index
44
+
45
+ # The relation reference id for this drawing
46
+ # @return [String]
47
+ attr_reader :rId
48
+
49
+ # The part name for this drawing
50
+ # @return [String]
51
+ attr_reader :pn
52
+
53
+ # The relational part name for this drawing
54
+ # @return [String]
55
+ attr_reader :rels_pn
56
+
57
+ # The drawing's relationships.
58
+ # @return [Relationships]
59
+ attr_reader :relationships
60
+
61
+ # Creates a new Drawing object
62
+ # @param [Worksheet] worksheet The worksheet that owns this drawing
63
+ def initialize(worksheet)
64
+ DataTypeValidator.validate "Drawing.worksheet", Worksheet, worksheet
65
+ @worksheet = worksheet
66
+ @worksheet.workbook.drawings << self
67
+ @anchors = SimpleTypedList.new TwoCellAnchor
68
+ end
69
+
70
+
71
+ # Adds a chart to the drawing.
72
+ # @note The recommended way to manage charts is to use Worksheet.add_chart.
73
+ # @param [Chart] chart_type The class of the chart to be added to the drawing
74
+ # @param [Hash] options
75
+ def add_chart(chart_type, options={})
76
+ DataTypeValidator.validate "Drawing.chart_type", Chart, chart_type
77
+ TwoCellAnchor.new(self, chart_type, options)
78
+ @anchors.last.graphic_frame.chart
79
+ end
80
+
81
+ def charts
82
+ @anchors.map { |a| a.graphic_frame.chart }
83
+ end
84
+
85
+ def index
86
+ @worksheet.workbook.drawings.index(self)
87
+ end
88
+
89
+ def rId
90
+ "rId#{index+1}"
91
+ end
92
+
93
+ def pn
94
+ "#{DRAWING_PN % (index+1)}"
95
+ end
96
+
97
+ def rels_pn
98
+ "#{DRAWING_RELS_PN % (index+1)}"
99
+ end
100
+
101
+ def relationships
102
+ r = Relationships.new
103
+ @anchors.each do |anchor|
104
+ chart = anchor.graphic_frame.chart
105
+ r << Relationship.new(CHART_R, "../#{chart.pn}")
106
+ end
107
+ r
108
+ end
109
+
110
+ # Serializes the drawing
111
+ # @return [String]
112
+ def to_xml
113
+ builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
114
+ xml.send('xdr:wsDr', :'xmlns:xdr'=>XML_NS_XDR, :'xmlns:a'=>XML_NS_A) {
115
+ anchors.each {|anchor| anchor.to_xml(xml) }
116
+ }
117
+ end
118
+ builder.to_xml
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,55 @@
1
+ module Axlsx
2
+ # A graphic frame defines a container for a chart object
3
+ # @note The recommended way to manage charts is Worksheet#add_chart
4
+ # @see Worksheet#add_chart
5
+ class GraphicFrame
6
+
7
+ #TODO add in support for other graphical objects, primarily images.
8
+
9
+ # A reference to the chart object associated with this frame
10
+ # @return [Chart]
11
+ attr_reader :chart
12
+
13
+ # A anchor that holds this frame
14
+ # @return [TwoCellAnchor]
15
+ attr_reader :anchor
16
+
17
+ # The relationship id for this graphic
18
+ # @return [String]
19
+ attr_reader :rId
20
+
21
+ # Creates a new GraphicFrame object
22
+ # @param [TwoCellAnchor] anchor
23
+ # @param [Class] chart_type
24
+ def initialize(anchor, chart_type, options)
25
+ @anchor = anchor
26
+ @chart = chart_type.new(self, options)
27
+ end
28
+
29
+ def rId
30
+ "rId#{@anchor.index+1}"
31
+ end
32
+
33
+ # Serializes the graphic frame
34
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
35
+ # @return [String]
36
+ def to_xml(xml)
37
+ xml.send('xdr:graphicFrame') {
38
+ xml.send('xdr:nvGraphicFramePr') {
39
+ xml.send('xdr:cNvPr', :id=>2, :name=>chart.title)
40
+ xml.send('xdr:cNvGraphicFramePr')
41
+ }
42
+ xml.send('xdr:xfrm') {
43
+ xml.send('a:off', :x=>0, :y=>0)
44
+ xml.send('a:ext', :cx=>0, :cy=>0)
45
+ }
46
+ xml.send('a:graphic') {
47
+ xml.send('a:graphicData', :uri=>XML_NS_C) {
48
+ xml.send('c:chart', :'xmlns:c'=>XML_NS_C, :'xmlns:r'=>XML_NS_R, :'r:id'=>rId)
49
+ }
50
+ }
51
+ }
52
+
53
+ end
54
+ end
55
+ end