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)
-
- (SimpleTypedList) anchors
readonly
A collection of anchors for this drawing only TwoCellAnchors are supported in this version.
-
- (Array) charts
readonly
An array of charts that are associated with this drawing’s anchors.
-
- (Integer) index
readonly
The index of this drawing in the owning workbooks’s drawings collection.
-
- (String) pn
readonly
The part name for this drawing.
-
- (Relationships) relationships
readonly
The drawing’s relationships.
-
- (String) rels_pn
readonly
The relational part name for this drawing.
-
- (String) rId
readonly
The relation reference id for this drawing.
-
- (Worksheet) worksheet
readonly
The worksheet that owns the drawing.
Instance Method Summary (collapse)
-
- (Object) add_chart(chart_type, options = {})
Adds a chart to the drawing.
-
- (Drawing) initialize(worksheet)
constructor
Creates a new Drawing object.
-
- (String) to_xml
Serializes the drawing.
Constructor Details
- (Drawing) initialize(worksheet)
Creates a new Drawing object
71 72 73 74 75 76 |
# File 'lib/axlsx/drawing/drawing.rb', line 71 def initialize(worksheet) DataTypeValidator.validate "Drawing.worksheet", Worksheet, worksheet @worksheet = worksheet @worksheet.workbook.drawings << self @anchors = SimpleTypedList.new TwoCellAnchor end |
Instance Attribute Details
- (SimpleTypedList) anchors (readonly)
A collection of anchors for this drawing only TwoCellAnchors are supported in this version
43 44 45 |
# File 'lib/axlsx/drawing/drawing.rb', line 43 def anchors @anchors end |
- (Array) charts (readonly)
An array of charts that are associated with this drawing’s anchors
47 48 49 |
# File 'lib/axlsx/drawing/drawing.rb', line 47 def charts @anchors.map { |a| a.graphic_frame.chart } end |
- (Integer) index (readonly)
The index of this drawing in the owning workbooks’s drawings collection.
51 52 53 |
# File 'lib/axlsx/drawing/drawing.rb', line 51 def index @worksheet.workbook.drawings.index(self) end |
- (String) pn (readonly)
The part name for this drawing
59 60 61 |
# File 'lib/axlsx/drawing/drawing.rb', line 59 def pn "#{DRAWING_PN % (index+1)}" end |
- (Relationships) relationships (readonly)
The drawing’s relationships.
67 68 69 70 71 72 73 74 |
# File 'lib/axlsx/drawing/drawing.rb', line 67 def relationships r = Relationships.new @anchors.each do |anchor| chart = anchor.graphic_frame.chart r << Relationship.new(CHART_R, "../#{chart.pn}") end r end |
- (String) rels_pn (readonly)
The relational part name for this drawing
63 64 65 |
# File 'lib/axlsx/drawing/drawing.rb', line 63 def rels_pn "#{DRAWING_RELS_PN % (index+1)}" end |
- (String) rId (readonly)
The relation reference id for this drawing
55 56 57 |
# File 'lib/axlsx/drawing/drawing.rb', line 55 def rId "rId#{index+1}" end |
- (Worksheet) worksheet (readonly)
The worksheet that owns the drawing
38 39 40 |
# File 'lib/axlsx/drawing/drawing.rb', line 38 def worksheet @worksheet end |
Instance Method Details
- (Object) add_chart(chart_type, options = {})
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.
82 83 84 85 |
# File 'lib/axlsx/drawing/drawing.rb', line 82 def add_chart(chart_type, ={}) TwoCellAnchor.new(self, chart_type, ) @anchors.last.graphic_frame.chart end |
- (String) to_xml
Serializes the drawing
118 119 120 121 122 123 124 125 |
# File 'lib/axlsx/drawing/drawing.rb', line 118 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) { anchors.each {|anchor| anchor.to_xml(xml) } } end builder.to_xml end |