Class: Axlsx::Drawing

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx/drawing/drawing.rb

Overview

Note:

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

Instance Method Summary collapse

Constructor Details

#initialize(worksheet) ⇒ Drawing

Creates a new Drawing object

Parameters:

  • worksheet (Worksheet)

    The worksheet that owns this drawing



66
67
68
69
70
71
# File 'lib/axlsx/drawing/drawing.rb', line 66

def initialize(worksheet)
  DataTypeValidator.validate "Drawing.worksheet", Worksheet, worksheet
  @worksheet = worksheet
  @worksheet.workbook.drawings << self
  @anchors = SimpleTypedList.new [TwoCellAnchor, OneCellAnchor]
end

Instance Attribute Details

#anchorsSimpleTypedList (readonly)

A collection of anchors for this drawing only TwoCellAnchors are supported in this version

Returns:

  • (SimpleTypedList)


62
63
64
# File 'lib/axlsx/drawing/drawing.rb', line 62

def anchors
  @anchors
end

#worksheetWorksheet (readonly)

The worksheet that owns the drawing

Returns:



57
58
59
# File 'lib/axlsx/drawing/drawing.rb', line 57

def worksheet
  @worksheet
end

Instance Method Details

#add_chart(chart_type, options = {}) ⇒ Object

Note:

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.



90
91
92
93
# File 'lib/axlsx/drawing/drawing.rb', line 90

def add_chart(chart_type, options={})
  TwoCellAnchor.new(self, options)
  @anchors.last.add_chart(chart_type, options)
end

#add_image(options = {}) ⇒ Pic

Note:

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.

Returns:

See Also:



78
79
80
81
82
83
84
85
# File 'lib/axlsx/drawing/drawing.rb', line 78

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

#chartsArray

An array of charts that are associated with this drawing's anchors

Returns:

  • (Array)


97
98
99
100
# File 'lib/axlsx/drawing/drawing.rb', line 97

def charts
  charts = @anchors.select { |a| a.object.is_a?(GraphicFrame) }
  charts.map { |a| a.object.chart }
end

#child_objectsArray

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

Returns:

  • (Array)


151
152
153
# File 'lib/axlsx/drawing/drawing.rb', line 151

def child_objects
  charts + images + hyperlinks
end

An array of hyperlink objects associated with this drawings images

Returns:

  • (Array)


104
105
106
107
# File 'lib/axlsx/drawing/drawing.rb', line 104

def hyperlinks
  links = self.images.select { |a| a.hyperlink.is_a?(Hyperlink) }
  links.map { |a| a.hyperlink }
end

#imagesArray

An array of image objects that are associated with this drawing's anchors

Returns:

  • (Array)


111
112
113
114
# File 'lib/axlsx/drawing/drawing.rb', line 111

def images
  images = @anchors.select { |a| a.object.is_a?(Pic) }
  images.map { |a| a.object }
end

#indexInteger

The index of this drawing in the owning workbooks's drawings collection.

Returns:

  • (Integer)


118
119
120
# File 'lib/axlsx/drawing/drawing.rb', line 118

def index
  @worksheet.workbook.drawings.index(self)
end

#index_of(object) ⇒ Object

The index of a chart, image or hyperlink object this drawing contains



142
143
144
# File 'lib/axlsx/drawing/drawing.rb', line 142

def index_of(object)
  child_objects.index(object)
end

#pnString

The part name for this drawing

Returns:

  • (String)


130
131
132
# File 'lib/axlsx/drawing/drawing.rb', line 130

def pn
  "#{DRAWING_PN % (index+1)}"
end

#relationshipsRelationships

The drawing's relationships.

Returns:



157
158
159
160
161
# File 'lib/axlsx/drawing/drawing.rb', line 157

def relationships
  r = Relationships.new
  child_objects.each { |child| r << child.relationship }
  r
end

#rels_pnString

The relational part name for this drawing

NOTE This should be rewritten to return an Axlsx::Relationship object.

Returns:

  • (String)


137
138
139
# File 'lib/axlsx/drawing/drawing.rb', line 137

def rels_pn
  "#{DRAWING_RELS_PN % (index+1)}"
end

#rIdString

The relation reference id for this drawing

Returns:

  • (String)


124
125
126
# File 'lib/axlsx/drawing/drawing.rb', line 124

def rId
  "rId#{index+1}"
end

#to_xml_string(str = '') ⇒ String

Serializes the object

Parameters:

  • str (String) (defaults to: '')

Returns:

  • (String)


166
167
168
169
170
171
172
# File 'lib/axlsx/drawing/drawing.rb', line 166

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