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 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

Instance Method Summary collapse

Constructor Details

#initialize(worksheet) ⇒ Drawing

Creates a new Drawing object

Parameters:

  • worksheet (Worksheet)

    The worksheet that owns this drawing



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

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)


53
54
55
# File 'lib/axlsx/drawing/drawing.rb', line 53

def anchors
  @anchors
end

#worksheetWorksheet (readonly)

The worksheet that owns the drawing

Returns:



48
49
50
# File 'lib/axlsx/drawing/drawing.rb', line 48

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.



75
76
77
78
# File 'lib/axlsx/drawing/drawing.rb', line 75

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

#add_image(options = {}) ⇒ Object

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



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

def add_image(options={})
  OneCellAnchor.new(self, options)
  @anchors.last.object
end

#chartsArray

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

Returns:

  • (Array)


82
83
84
85
# File 'lib/axlsx/drawing/drawing.rb', line 82

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

An array of hyperlink objects associated with this drawings images

Returns:

  • (Array)


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

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)


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

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)


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

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

#pnString

The part name for this drawing

Returns:

  • (String)


115
116
117
# File 'lib/axlsx/drawing/drawing.rb', line 115

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

#relationshipsRelationships

The drawing’s relationships.

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/axlsx/drawing/drawing.rb', line 127

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_pnString

The relational part name for this drawing

Returns:

  • (String)


121
122
123
# File 'lib/axlsx/drawing/drawing.rb', line 121

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

#rIdString

The relation reference id for this drawing

Returns:

  • (String)


109
110
111
# File 'lib/axlsx/drawing/drawing.rb', line 109

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

#to_xml_string(str = '') ⇒ String

Serializes the object

Parameters:

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

Returns:

  • (String)


144
145
146
147
148
149
# File 'lib/axlsx/drawing/drawing.rb', line 144

def to_xml_string(str = '')
  str << '<?xml version="1.0" encoding="UTF-8"?>'
  str << '<xdr:wsDr xmlns:xdr="' << XML_NS_XDR << '" xmlns:a="' << XML_NS_A << '" xmlns:c="' << XML_NS_C << '">'
  anchors.each { |anchor| anchor.to_xml_string(str) }
  str << '</xdr:wsDr>'
end