Class: Writexlsx::Shape
- Inherits:
-
Object
- Object
- Writexlsx::Shape
- Includes:
- Utility::ChartFormatting
- Defined in:
- lib/write_xlsx/shape.rb
Overview
Shape - A class for writing Excel shapes.
Used in conjunction with WriteXLSX.
Copyright 2000-2012, John McNamara, [email protected] Converted to ruby by Hideo NAKAMURA, [email protected]
Constant Summary
Constants included from Utility::ChartFormatting
Utility::ChartFormatting::PATTERN_TYPES
Instance Attribute Summary collapse
-
#adjustments ⇒ Object
Returns the value of attribute adjustments.
-
#align ⇒ Object
readonly
Returns the value of attribute align.
-
#anchor ⇒ Object
readonly
Returns the value of attribute anchor.
-
#column_end ⇒ Object
Returns the value of attribute column_end.
-
#column_start ⇒ Object
Returns the value of attribute column_start.
-
#connect ⇒ Object
Returns the value of attribute connect.
-
#drawing ⇒ Object
readonly
Returns the value of attribute drawing.
-
#edit_as ⇒ Object
readonly
Returns the value of attribute edit_as.
-
#element ⇒ Object
Returns the value of attribute element.
-
#end ⇒ Object
Returns the value of attribute end.
-
#end_index ⇒ Object
Returns the value of attribute end_index.
-
#end_side ⇒ Object
Returns the value of attribute end_side.
-
#fill ⇒ Object
readonly
Returns the value of attribute fill.
-
#flip_h ⇒ Object
Returns the value of attribute flip_h.
-
#flip_v ⇒ Object
Returns the value of attribute flip_v.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#height ⇒ Object
Returns the value of attribute height.
-
#height_emu ⇒ Object
Returns the value of attribute height_emu.
-
#id ⇒ Object
Returns the value of attribute id.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#line_type ⇒ Object
Returns the value of attribute line_type.
-
#line_weight ⇒ Object
Returns the value of attribute line_weight.
-
#name ⇒ Object
Returns the value of attribute name.
-
#palette ⇒ Object
Returns the value of attribute palette.
-
#rotation ⇒ Object
Returns the value of attribute rotation.
-
#row_end ⇒ Object
Returns the value of attribute row_end.
-
#row_start ⇒ Object
Returns the value of attribute row_start.
-
#scale_x ⇒ Object
Returns the value of attribute scale_x.
-
#scale_y ⇒ Object
Returns the value of attribute scale_y.
-
#start ⇒ Object
Returns the value of attribute start.
-
#start_index ⇒ Object
Returns the value of attribute start_index.
-
#start_side ⇒ Object
Returns the value of attribute start_side.
-
#stencil ⇒ Object
Returns the value of attribute stencil.
-
#text ⇒ Object
Returns the value of attribute text.
-
#tx_box ⇒ Object
readonly
Returns the value of attribute tx_box.
-
#type ⇒ Object
Returns the value of attribute type.
-
#valign ⇒ Object
readonly
Returns the value of attribute valign.
-
#width ⇒ Object
Returns the value of attribute width.
-
#width_emu ⇒ Object
Returns the value of attribute width_emu.
-
#x1 ⇒ Object
Returns the value of attribute x1.
-
#x2 ⇒ Object
Returns the value of attribute x2.
-
#x_abs ⇒ Object
Returns the value of attribute x_abs.
-
#x_offset ⇒ Object
Returns the value of attribute x_offset.
-
#y1 ⇒ Object
Returns the value of attribute y1.
-
#y2 ⇒ Object
Returns the value of attribute y2.
-
#y_abs ⇒ Object
Returns the value of attribute y_abs.
-
#y_offset ⇒ Object
Returns the value of attribute y_offset.
Instance Method Summary collapse
-
#auto_locate_connectors(shapes, shape_hash) ⇒ Object
Re-size connector shapes if they are connected to other shapes.
-
#calc_position_emus(worksheet) ⇒ Object
Calculate the vertices that define the position of a shape object within the worksheet in EMUs.
- #dimensions ⇒ Object
-
#initialize(properties = {}) ⇒ Shape
constructor
A new instance of Shape.
- #set_position(row_start, column_start, x_offset, y_offset, x_scale, y_scale, anchor) ⇒ Object
- #set_properties(properties) ⇒ Object
-
#validate(index) ⇒ Object
Check shape attributes to ensure they are valid.
Methods included from Utility::ChartFormatting
#color, #dash_types, #fill_properties, #layout_properties, #legend_properties, #line_fill_properties, #line_properties, #palette_color_from_index, #pattern_properties, #value_or_raise, #write_a_solid_fill, #write_a_srgb_clr
Constructor Details
#initialize(properties = {}) ⇒ Shape
Returns a new instance of Shape.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/write_xlsx/shape.rb', line 30 def initialize(properties = {}) @writer = Package::XMLWriterSimple.new init_basic_attributes init_connection_attributes init_appearance_attributes init_position_attributes set_properties(properties) end |
Instance Attribute Details
#adjustments ⇒ Object
Returns the value of attribute adjustments.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def adjustments @adjustments end |
#align ⇒ Object (readonly)
Returns the value of attribute align.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def align @align end |
#anchor ⇒ Object (readonly)
Returns the value of attribute anchor.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def anchor @anchor end |
#column_end ⇒ Object
Returns the value of attribute column_end.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def column_end @column_end end |
#column_start ⇒ Object
Returns the value of attribute column_start.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def column_start @column_start end |
#connect ⇒ Object
Returns the value of attribute connect.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def connect @connect end |
#drawing ⇒ Object (readonly)
Returns the value of attribute drawing.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def drawing @drawing end |
#edit_as ⇒ Object (readonly)
Returns the value of attribute edit_as.
19 20 21 |
# File 'lib/write_xlsx/shape.rb', line 19 def edit_as @edit_as end |
#element ⇒ Object
Returns the value of attribute element.
27 28 29 |
# File 'lib/write_xlsx/shape.rb', line 27 def element @element end |
#end ⇒ Object
Returns the value of attribute end.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def end @end end |
#end_index ⇒ Object
Returns the value of attribute end_index.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def end_index @end_index end |
#end_side ⇒ Object
Returns the value of attribute end_side.
28 29 30 |
# File 'lib/write_xlsx/shape.rb', line 28 def end_side @end_side end |
#fill ⇒ Object (readonly)
Returns the value of attribute fill.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def fill @fill end |
#flip_h ⇒ Object
Returns the value of attribute flip_h.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def flip_h @flip_h end |
#flip_v ⇒ Object
Returns the value of attribute flip_v.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def flip_v @flip_v end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def format @format end |
#height ⇒ Object
Returns the value of attribute height.
26 27 28 |
# File 'lib/write_xlsx/shape.rb', line 26 def height @height end |
#height_emu ⇒ Object
Returns the value of attribute height_emu.
27 28 29 |
# File 'lib/write_xlsx/shape.rb', line 27 def height_emu @height_emu end |
#id ⇒ Object
Returns the value of attribute id.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def id @id end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def line @line end |
#line_type ⇒ Object
Returns the value of attribute line_type.
27 28 29 |
# File 'lib/write_xlsx/shape.rb', line 27 def line_type @line_type end |
#line_weight ⇒ Object
Returns the value of attribute line_weight.
27 28 29 |
# File 'lib/write_xlsx/shape.rb', line 27 def line_weight @line_weight end |
#name ⇒ Object
Returns the value of attribute name.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def name @name end |
#palette ⇒ Object
Returns the value of attribute palette.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def palette @palette end |
#rotation ⇒ Object
Returns the value of attribute rotation.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def rotation @rotation end |
#row_end ⇒ Object
Returns the value of attribute row_end.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def row_end @row_end end |
#row_start ⇒ Object
Returns the value of attribute row_start.
24 25 26 |
# File 'lib/write_xlsx/shape.rb', line 24 def row_start @row_start end |
#scale_x ⇒ Object
Returns the value of attribute scale_x.
26 27 28 |
# File 'lib/write_xlsx/shape.rb', line 26 def scale_x @scale_x end |
#scale_y ⇒ Object
Returns the value of attribute scale_y.
26 27 28 |
# File 'lib/write_xlsx/shape.rb', line 26 def scale_y @scale_y end |
#start ⇒ Object
Returns the value of attribute start.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def start @start end |
#start_index ⇒ Object
Returns the value of attribute start_index.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def start_index @start_index end |
#start_side ⇒ Object
Returns the value of attribute start_side.
28 29 30 |
# File 'lib/write_xlsx/shape.rb', line 28 def start_side @start_side end |
#stencil ⇒ Object
Returns the value of attribute stencil.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def stencil @stencil end |
#text ⇒ Object
Returns the value of attribute text.
23 24 25 |
# File 'lib/write_xlsx/shape.rb', line 23 def text @text end |
#tx_box ⇒ Object (readonly)
Returns the value of attribute tx_box.
20 21 22 |
# File 'lib/write_xlsx/shape.rb', line 20 def tx_box @tx_box end |
#type ⇒ Object
Returns the value of attribute type.
22 23 24 |
# File 'lib/write_xlsx/shape.rb', line 22 def type @type end |
#valign ⇒ Object (readonly)
Returns the value of attribute valign.
21 22 23 |
# File 'lib/write_xlsx/shape.rb', line 21 def valign @valign end |
#width ⇒ Object
Returns the value of attribute width.
26 27 28 |
# File 'lib/write_xlsx/shape.rb', line 26 def width @width end |
#width_emu ⇒ Object
Returns the value of attribute width_emu.
27 28 29 |
# File 'lib/write_xlsx/shape.rb', line 27 def width_emu @width_emu end |
#x1 ⇒ Object
Returns the value of attribute x1.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def x1 @x1 end |
#x2 ⇒ Object
Returns the value of attribute x2.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def x2 @x2 end |
#x_abs ⇒ Object
Returns the value of attribute x_abs.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def x_abs @x_abs end |
#x_offset ⇒ Object
Returns the value of attribute x_offset.
26 27 28 |
# File 'lib/write_xlsx/shape.rb', line 26 def x_offset @x_offset end |
#y1 ⇒ Object
Returns the value of attribute y1.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def y1 @y1 end |
#y2 ⇒ Object
Returns the value of attribute y2.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def y2 @y2 end |
#y_abs ⇒ Object
Returns the value of attribute y_abs.
25 26 27 |
# File 'lib/write_xlsx/shape.rb', line 25 def y_abs @y_abs end |
#y_offset ⇒ Object
Returns the value of attribute y_offset.
26 27 28 |
# File 'lib/write_xlsx/shape.rb', line 26 def y_offset @y_offset end |
Instance Method Details
#auto_locate_connectors(shapes, shape_hash) ⇒ Object
Re-size connector shapes if they are connected to other shapes.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/write_xlsx/shape.rb', line 112 def auto_locate_connectors(shapes, shape_hash) # Valid connector shapes. connector_shapes = { straightConnector: 1, Connector: 1, bentConnector: 1, curvedConnector: 1, line: 1 } shape_base = @type.chop.to_sym # Remove the number of segments from end of type. @connect = connector_shapes[shape_base] ? 1 : 0 return if @connect == 0 # Both ends have to be connected to size it. return if @start == 0 && @end == 0 # Both ends need to provide info about where to connect. return if @start_side == 0 && @end_side == 0 sid = @start eid = @end slink_id = shape_hash[sid] || 0 sls = shapes.fetch(slink_id, Shape.new) elink_id = shape_hash[eid] || 0 els = shapes.fetch(elink_id, Shape.new) # Assume shape connections are to the middle of an object, and # not a corner (for now). connect_type = @start_side + @end_side smidx = sls.x_offset + (sls.width / 2) emidx = els.x_offset + (els.width / 2) smidy = sls.y_offset + (sls.height / 2) emidy = els.y_offset + (els.height / 2) if connect_type == 'bt' sy = sls.y_offset + sls.height ey = els.y_offset @width = (emidx - smidx).to_i.abs @x_offset = [smidx, emidx].min.to_i @height = (els.y_offset - (sls.y_offset + sls.height)).to_i.abs @y_offset = [sls.y_offset + sls.height, els.y_offset].min.to_i @flip_h = smidx < emidx ? 1 : 0 @rotation = 90 if sy > ey @flip_v = 1 # Create 3 adjustments for an end shape vertically above a # start @ Adjustments count from the upper left object. @adjustments = [-10, 50, 110] if @adjustments.empty? @type = 'bentConnector5' end elsif connect_type == 'rl' @width = (els.x_offset - (sls.x_offset + sls.width)).to_i.abs @height = (emidy - smidy).to_i.abs @x_offset = [sls.x_offset + sls.width, els.x_offset].min @y_offset = [smidy, emidy].min @flip_h = 1 if smidx < emidx && smidy > emidy @flip_h = 1 if smidx > emidx && smidy < emidy if smidx > emidx # Create 3 adjustments for an end shape to the left of a # start @ @adjustments = [-10, 50, 110] if @adjustments.empty? @type = 'bentConnector5' end end end |
#calc_position_emus(worksheet) ⇒ Object
Calculate the vertices that define the position of a shape object within the worksheet in EMUs. Save the vertices with the object.
The vertices are expressed as English Metric Units (EMUs). There are 12,700 EMUs per point. Therefore, 12,700 * 3 /4 = 9,525 EMUs per pixel.
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 |
# File 'lib/write_xlsx/shape.rb', line 64 def calc_position_emus(worksheet) c_start, r_start, xx1, yy1, c_end, r_end, xx2, yy2, x_abslt, y_abslt = worksheet.position_object_pixels( @column_start, @row_start, @x_offset, @y_offset, @width * @scale_x, @height * @scale_y ) # Now that x2/y2 have been calculated with a potentially negative # width/height we use the absolute value and convert to EMUs. @width_emu = (@width * 9_525).abs.to_i @height_emu = (@height * 9_525).abs.to_i @column_start = c_start.to_i @row_start = r_start.to_i @column_end = c_end.to_i @row_end = r_end.to_i # Convert the pixel values to EMUs. See above. @x1 = (xx1 * 9_525).to_i @y1 = (yy1 * 9_525).to_i @x2 = (xx2 * 9_525).to_i @y2 = (yy2 * 9_525).to_i @x_abs = (x_abslt * 9_525).to_i @y_abs = (y_abslt * 9_525).to_i end |
#dimensions ⇒ Object
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/write_xlsx/shape.rb', line 198 def dimensions [ @column_start, @row_start, @x1, @y1, @column_end, @row_end, @x2, @y2, @x_abs, @y_abs, @width_emu, @height_emu ] end |
#set_position(row_start, column_start, x_offset, y_offset, x_scale, y_scale, anchor) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/write_xlsx/shape.rb', line 96 def set_position(row_start, column_start, x_offset, y_offset, x_scale, y_scale, anchor) @row_start = row_start @column_start = column_start @x_offset = x_offset || 0 @y_offset = y_offset || 0 @anchor = anchor || 1 # Override shape scale if supplied as an argument. Otherwise, use the # existing shape scale factors. @scale_x = x_scale if x_scale @scale_y = y_scale if y_scale end |
#set_properties(properties) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/write_xlsx/shape.rb', line 41 def set_properties(properties) # Override default properties with passed arguments properties.each do |key, value| # Strip leading "-" from Tk style properties e.g. -color => 'red'. instance_variable_set("@#{key}", value) end end |
#validate(index) ⇒ Object
Check shape attributes to ensure they are valid.
192 193 194 195 196 |
# File 'lib/write_xlsx/shape.rb', line 192 def validate(index) raise "Shape #{index} (#{@type}) alignment (#{@align}) not in ['l', 'ctr', 'r', 'just']\n" unless %w[l ctr r just].include?(@align) raise "Shape #{index} (#{@type}) vertical alignment (#{@valign}) not in ['t', 'ctr', 'v']\n" unless %w[t ctr b].include?(@valign) end |