blob: 997a7af0da9e6c6fd44d78c6cfbb40673044bb1e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# frozen_string_literal: true
module Axlsx
require 'axlsx/drawing/d_lbls'
require 'axlsx/drawing/title'
require 'axlsx/drawing/series_title'
require 'axlsx/drawing/series'
require 'axlsx/drawing/pie_series'
require 'axlsx/drawing/bar_series'
require 'axlsx/drawing/line_series'
require 'axlsx/drawing/scatter_series'
require 'axlsx/drawing/bubble_series'
require 'axlsx/drawing/area_series'
require 'axlsx/drawing/scaling'
require 'axlsx/drawing/axis'
require 'axlsx/drawing/str_val'
require 'axlsx/drawing/num_val'
require 'axlsx/drawing/str_data'
require 'axlsx/drawing/num_data'
require 'axlsx/drawing/num_data_source'
require 'axlsx/drawing/ax_data_source'
require 'axlsx/drawing/ser_axis'
require 'axlsx/drawing/cat_axis'
require 'axlsx/drawing/val_axis'
require 'axlsx/drawing/axes'
require 'axlsx/drawing/marker'
require 'axlsx/drawing/one_cell_anchor'
require 'axlsx/drawing/two_cell_anchor'
require 'axlsx/drawing/graphic_frame'
require 'axlsx/drawing/view_3D'
require 'axlsx/drawing/chart'
require 'axlsx/drawing/pie_3D_chart'
require 'axlsx/drawing/bar_3D_chart'
require 'axlsx/drawing/bar_chart'
require 'axlsx/drawing/line_chart'
require 'axlsx/drawing/line_3D_chart'
require 'axlsx/drawing/scatter_chart'
require 'axlsx/drawing/bubble_chart'
require 'axlsx/drawing/area_chart'
require 'axlsx/drawing/picture_locking'
require 'axlsx/drawing/pic'
require 'axlsx/drawing/hyperlink'
require 'axlsx/drawing/vml_drawing'
require 'axlsx/drawing/vml_shape'
# 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.
class Drawing
# The worksheet that owns the drawing
# @return [Worksheet]
attr_reader :worksheet
# A collection of anchors for this drawing
# only TwoCellAnchors are supported in this version
# @return [SimpleTypedList]
attr_reader :anchors
# Creates a new Drawing object
# @param [Worksheet] worksheet The worksheet that owns this drawing
def initialize(worksheet)
DataTypeValidator.validate "Drawing.worksheet", Worksheet, worksheet
@worksheet = worksheet
@worksheet.workbook.drawings << self
@anchors = SimpleTypedList.new [TwoCellAnchor, OneCellAnchor]
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]
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
# 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
def add_chart(chart_type, options = {})
TwoCellAnchor.new(self, options)
@anchors.last.add_chart(chart_type, options)
end
# An array of charts that are associated with this drawing's anchors
# @return [Array]
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
# @return [Array]
def hyperlinks
links = images.select { |a| a.hyperlink.is_a?(Hyperlink) }
links.map(&:hyperlink)
end
# An array of image objects that are associated with this drawing's anchors
# @return [Array]
def images
images = @anchors.select { |a| a.object.is_a?(Pic) }
images.map(&:object)
end
# The index of this drawing in the owning workbooks's drawings collection.
# @return [Integer]
def index
@worksheet.workbook.drawings.index(self)
end
# The part name for this drawing
# @return [String]
def pn
format(DRAWING_PN, index + 1)
end
# The relational part name for this drawing
# #NOTE This should be rewritten to return an Axlsx::Relationship object.
# @return [String]
def rels_pn
format(DRAWING_RELS_PN, index + 1)
end
# A list of objects this drawing holds.
# @return [Array]
def child_objects
charts + images + hyperlinks
end
# The drawing's relationships.
# @return [Relationships]
def relationships
r = Relationships.new
child_objects.each { |child| r << child.relationship }
r
end
# Serializes the object
# @param [String] str
# @return [String]
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
end
end
|