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 method.
A Drawing is a canvas for charts. Each worksheet has a single drawing that manages anchors. The anchors reference the charts via graphical frames. This is not a trivial relationship so please do follow the advice in the note. see README 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 = {}) ⇒ Object
Adds an image to the chart.
-
#charts ⇒ Array
An array of charts that are associated with this drawing’s anchors.
-
#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.
-
#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.
-
#rId ⇒ String
The relation reference id for this drawing.
-
#to_xml ⇒ String
Serializes the drawing.
Constructor Details
#initialize(worksheet) ⇒ Drawing
Creates a new Drawing object
53 54 55 56 57 58 |
# File 'lib/axlsx/drawing/drawing.rb', line 53 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
49 50 51 |
# File 'lib/axlsx/drawing/drawing.rb', line 49 def anchors @anchors end |
#worksheet ⇒ Worksheet (readonly)
The worksheet that owns the drawing
44 45 46 |
# File 'lib/axlsx/drawing/drawing.rb', line 44 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.
71 72 73 74 |
# File 'lib/axlsx/drawing/drawing.rb', line 71 def add_chart(chart_type, ={}) TwoCellAnchor.new(self, ) @anchors.last.add_chart(chart_type, ) end |
#add_image(options = {}) ⇒ Object
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
63 64 65 66 |
# File 'lib/axlsx/drawing/drawing.rb', line 63 def add_image(={}) OneCellAnchor.new(self, ) @anchors.last.object end |
#charts ⇒ Array
An array of charts that are associated with this drawing’s anchors
78 79 80 81 |
# File 'lib/axlsx/drawing/drawing.rb', line 78 def charts charts = @anchors.select { |a| a.object.is_a?(GraphicFrame) } charts.map { |a| a.object.chart } end |
#hyperlinks ⇒ Array
An array of hyperlink objects associated with this drawings images
85 86 87 88 |
# File 'lib/axlsx/drawing/drawing.rb', line 85 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
92 93 94 95 |
# File 'lib/axlsx/drawing/drawing.rb', line 92 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.
99 100 101 |
# File 'lib/axlsx/drawing/drawing.rb', line 99 def index @worksheet.workbook.drawings.index(self) end |
#pn ⇒ String
The part name for this drawing
111 112 113 |
# File 'lib/axlsx/drawing/drawing.rb', line 111 def pn "#{DRAWING_PN % (index+1)}" end |
#relationships ⇒ Relationships
The drawing’s relationships.
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/axlsx/drawing/drawing.rb', line 123 def relationships r = Relationships.new charts.each do |chart| r << Relationship.new(CHART_R, "../#{chart.pn}") end images.each do |image| r << Relationship.new(IMAGE_R, "../#{image.pn}") end hyperlinks.each do |hyperlink| r << Relationship.new(HYPERLINK_R, hyperlink.href, :target_mode => :External) end r end |
#rels_pn ⇒ String
The relational part name for this drawing
117 118 119 |
# File 'lib/axlsx/drawing/drawing.rb', line 117 def rels_pn "#{DRAWING_RELS_PN % (index+1)}" end |
#rId ⇒ String
The relation reference id for this drawing
105 106 107 |
# File 'lib/axlsx/drawing/drawing.rb', line 105 def rId "rId#{index+1}" end |
#to_xml ⇒ String
Serializes the drawing
139 140 141 142 143 144 145 146 |
# File 'lib/axlsx/drawing/drawing.rb', line 139 def to_xml builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| xml.send('xdr:wsDr', :'xmlns:xdr'=>XML_NS_XDR, :'xmlns:a'=>XML_NS_A, :'xmlns:c'=>XML_NS_C) { anchors.each {|anchor| anchor.to_xml(xml) } } end builder.to_xml(:save_with => 0) end |