class Axlsx::Drawing
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. @note The recommended way to manage drawings is to use the Worksheet.add_chart
and Worksheet.add_image
methods. @see Worksheet#add_chart
@see Worksheet#add_image
@see Chart
see examples/example.rb for an example of how to create a chart.
Attributes
A collection of anchors for this drawing only TwoCellAnchors are supported in this version @return [SimpleTypedList]
The worksheet that owns the drawing @return [Worksheet]
Public Class Methods
Creates a new Drawing
object @param [Worksheet] worksheet The worksheet that owns this drawing
# File lib/axlsx/drawing/drawing.rb, line 73 def initialize(worksheet) DataTypeValidator.validate "Drawing.worksheet", Worksheet, worksheet @worksheet = worksheet @worksheet.workbook.drawings << self @anchors = SimpleTypedList.new [TwoCellAnchor, OneCellAnchor] end
Public Instance Methods
Adds a chart to the drawing. @note The recommended way to manage charts is to use Worksheet.add_chart
. Please refer to that method for documentation. @see Worksheet#add_chart
# File lib/axlsx/drawing/drawing.rb, line 97 def add_chart(chart_type, options={}) TwoCellAnchor.new(self, options) @anchors.last.add_chart(chart_type, options) end
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. @note The recommended way to manage images is to use Worksheet.add_image
. Please refer to that method for documentation. @see Worksheet#add_image
@return [Pic]
# File lib/axlsx/drawing/drawing.rb, line 85 def add_image(options={}) if options[:end_at] TwoCellAnchor.new(self, options).add_pic(options) else OneCellAnchor.new(self, options) end @anchors.last.object end
An array of charts that are associated with this drawing's anchors @return [Array]
# File lib/axlsx/drawing/drawing.rb, line 104 def charts charts = @anchors.select { |a| a.object.is_a?(GraphicFrame) } charts.map { |a| a.object.chart } end
A list of objects this drawing holds. @return [Array]
# File lib/axlsx/drawing/drawing.rb, line 144 def child_objects charts + images + hyperlinks end
An array of hyperlink objects associated with this drawings images @return [Array]
# File lib/axlsx/drawing/drawing.rb, line 111 def hyperlinks links = self.images.select { |a| a.hyperlink.is_a?(Hyperlink) } links.map { |a| a.hyperlink } end
An array of image objects that are associated with this drawing's anchors @return [Array]
# File lib/axlsx/drawing/drawing.rb, line 118 def images images = @anchors.select { |a| a.object.is_a?(Pic) } images.map { |a| a.object } end
The index of this drawing in the owning workbooks's drawings collection. @return [Integer]
# File lib/axlsx/drawing/drawing.rb, line 125 def index @worksheet.workbook.drawings.index(self) end
The part name for this drawing @return [String]
# File lib/axlsx/drawing/drawing.rb, line 131 def pn "#{DRAWING_PN % (index+1)}" end
The drawing's relationships. @return [Relationships]
# File lib/axlsx/drawing/drawing.rb, line 150 def relationships r = Relationships.new child_objects.each { |child| r << child.relationship } r end
The relational part name for this drawing #NOTE This should be rewritten to return an Axlsx::Relationship
object. @return [String]
# File lib/axlsx/drawing/drawing.rb, line 138 def rels_pn "#{DRAWING_RELS_PN % (index+1)}" end
Serializes the object @param [String] str @return [String]
# File lib/axlsx/drawing/drawing.rb, line 159 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