Class: Axlsx::Drawing
- Inherits:
-
Object
- Object
- Axlsx::Drawing
- Defined in:
- lib/axlsx/drawing/drawing.rb
Overview
The recommended way to manage drawings is to use the Worksheet.add_chart and Worksheet.add_image methods.
A Drawing is a canvas for charts and images. Each worksheet has a single drawing that manages anchors. The anchors reference the charts or images via graphical frames. This is not a trivial relationship so please do follow the advice in the note. see examples/example.rb for an example of how to create a chart.
Instance Attribute Summary collapse
-
#anchors ⇒ SimpleTypedList
readonly
A collection of anchors for this drawing only TwoCellAnchors are supported in this version.
-
#worksheet ⇒ Worksheet
readonly
The worksheet that owns the drawing.
Instance Method Summary collapse
-
#add_chart(chart_type, options = {}) ⇒ Object
Adds a chart to the drawing.
-
#add_image(options = {}) ⇒ Pic
Adds an image to the chart If th end_at option is specified we create a two cell anchor.
-
#charts ⇒ Array
An array of charts that are associated with this drawing's anchors.
-
#child_objects ⇒ Array
An ordered list of objects this drawing holds It is important that the objects are returned in the same order each time for releationship indexing in the package.
-
#hyperlinks ⇒ Array
An array of hyperlink objects associated with this drawings images.
-
#images ⇒ Array
An array of image objects that are associated with this drawing's anchors.
-
#index ⇒ Integer
The index of this drawing in the owning workbooks's drawings collection.
-
#index_of(object) ⇒ Object
The index of a chart, image or hyperlink object this drawing contains.
-
#initialize(worksheet) ⇒ Drawing
constructor
Creates a new Drawing object.
-
#pn ⇒ String
The part name for this drawing.
-
#relationships ⇒ Relationships
The drawing's relationships.
-
#rels_pn ⇒ String
The relational part name for this drawing #NOTE This should be rewritten to return an Axlsx::Relationship object.
-
#rId ⇒ String
The relation reference id for this drawing.
-
#to_xml_string(str = '') ⇒ String
Serializes the object.
Constructor Details
#initialize(worksheet) ⇒ Drawing
Creates a new Drawing object
68 69 70 71 72 73 |
# File 'lib/axlsx/drawing/drawing.rb', line 68 def initialize(worksheet) DataTypeValidator.validate "Drawing.worksheet", Worksheet, worksheet @worksheet = worksheet @worksheet.workbook.drawings << self @anchors = SimpleTypedList.new [TwoCellAnchor, OneCellAnchor] end |
Instance Attribute Details
#anchors ⇒ SimpleTypedList (readonly)
A collection of anchors for this drawing only TwoCellAnchors are supported in this version
64 65 66 |
# File 'lib/axlsx/drawing/drawing.rb', line 64 def anchors @anchors end |
#worksheet ⇒ Worksheet (readonly)
The worksheet that owns the drawing
59 60 61 |
# File 'lib/axlsx/drawing/drawing.rb', line 59 def worksheet @worksheet end |
Instance Method Details
#add_chart(chart_type, options = {}) ⇒ Object
The recommended way to manage charts is to use Worksheet.add_chart. Please refer to that method for documentation.
Adds a chart to the drawing.
92 93 94 95 |
# File 'lib/axlsx/drawing/drawing.rb', line 92 def add_chart(chart_type, ={}) TwoCellAnchor.new(self, ) @anchors.last.add_chart(chart_type, ) end |
#add_image(options = {}) ⇒ Pic
The recommended way to manage images is to use Worksheet.add_image. Please refer to that method for documentation.
Adds an image to the chart If th end_at option is specified we create a two cell anchor. By default we use a one cell anchor.
80 81 82 83 84 85 86 87 |
# File 'lib/axlsx/drawing/drawing.rb', line 80 def add_image(={}) if [:end_at] TwoCellAnchor.new(self, ).add_pic() else OneCellAnchor.new(self, ) end @anchors.last.object end |
#charts ⇒ Array
An array of charts that are associated with this drawing's anchors
99 100 101 102 |
# File 'lib/axlsx/drawing/drawing.rb', line 99 def charts charts = @anchors.select { |a| a.object.is_a?(GraphicFrame) } charts.map { |a| a.object.chart } end |
#child_objects ⇒ Array
An ordered list of objects this drawing holds It is important that the objects are returned in the same order each time for releationship indexing in the package
153 154 155 |
# File 'lib/axlsx/drawing/drawing.rb', line 153 def child_objects charts + images + hyperlinks end |
#hyperlinks ⇒ Array
An array of hyperlink objects associated with this drawings images
106 107 108 109 |
# File 'lib/axlsx/drawing/drawing.rb', line 106 def hyperlinks links = self.images.select { |a| a.hyperlink.is_a?(Hyperlink) } links.map { |a| a.hyperlink } end |
#images ⇒ Array
An array of image objects that are associated with this drawing's anchors
113 114 115 116 |
# File 'lib/axlsx/drawing/drawing.rb', line 113 def images images = @anchors.select { |a| a.object.is_a?(Pic) } images.map { |a| a.object } end |
#index ⇒ Integer
The index of this drawing in the owning workbooks's drawings collection.
120 121 122 |
# File 'lib/axlsx/drawing/drawing.rb', line 120 def index @worksheet.workbook.drawings.index(self) end |
#index_of(object) ⇒ Object
The index of a chart, image or hyperlink object this drawing contains
144 145 146 |
# File 'lib/axlsx/drawing/drawing.rb', line 144 def index_of(object) child_objects.index(object) end |
#pn ⇒ String
The part name for this drawing
132 133 134 |
# File 'lib/axlsx/drawing/drawing.rb', line 132 def pn "#{DRAWING_PN % (index+1)}" end |
#relationships ⇒ Relationships
The drawing's relationships.
159 160 161 162 163 |
# File 'lib/axlsx/drawing/drawing.rb', line 159 def relationships r = Relationships.new child_objects.each { |child| r << child.relationship } r end |
#rels_pn ⇒ String
The relational part name for this drawing
NOTE This should be rewritten to return an Axlsx::Relationship object.
139 140 141 |
# File 'lib/axlsx/drawing/drawing.rb', line 139 def rels_pn "#{DRAWING_RELS_PN % (index+1)}" end |
#rId ⇒ String
The relation reference id for this drawing
126 127 128 |
# File 'lib/axlsx/drawing/drawing.rb', line 126 def rId "rId#{index+1}" end |
#to_xml_string(str = '') ⇒ String
Serializes the object
168 169 170 171 172 173 174 |
# File 'lib/axlsx/drawing/drawing.rb', line 168 def to_xml_string(str = '') str << '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' str << '<xdr:wsDr xmlns:xdr="' << XML_NS_XDR << '" xmlns:a="' << XML_NS_A << '">' anchors.each { |anchor| anchor.to_xml_string(str) } str << '</xdr:wsDr>' end |