Class: Axlsx::Chart

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

Overview

Note:

Worksheet#add_chart is the recommended way to create charts for your worksheets.

A Chart is the superclass for specific charts

See Also:

  • for examples

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsParser

#parse_options

Constructor Details

#initialize(frame, options = {}) {|_self| ... } ⇒ Chart

Creates a new chart object

Parameters:

  • frame (GraphicalFrame)

    The frame that holds this chart.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • title (Cell, String)
  • show_legend (Boolean)
  • legend_position (Symbol)
  • start_at (Array|String|Cell)

    The X, Y coordinates defining the top left corner of the chart.

  • end_at (Array|String|Cell)

    The X, Y coordinates defining the bottom right corner of the chart.

  • plot_visible_only (Boolean) — default: true

    Whether only data from visible cells should be plotted.

  • rounded_corners (Boolean) — default: true

    Whether the chart area shall have rounded corners.

Yields:

  • (_self)

Yield Parameters:

  • _self (Axlsx::Chart)

    the object that the method was called on



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/axlsx/drawing/chart.rb', line 19

def initialize(frame, options = {})
  @style = 18
  @view_3D = nil
  @graphic_frame = frame
  @graphic_frame.anchor.drawing.worksheet.workbook.charts << self
  @series = SimpleTypedList.new Series
  @show_legend = true
  @legend_position = :r
  @display_blanks_as = :gap
  @series_type = Series
  @title = Title.new
  @bg_color = nil
  @plot_visible_only = true
  @rounded_corners = true
  parse_options options
  start_at(*options[:start_at]) if options[:start_at]
  end_at(*options[:end_at]) if options[:end_at]
  yield self if block_given?
end

Instance Attribute Details

#bg_colorString

Background color for the chart

Returns:

  • (String)


106
107
108
# File 'lib/axlsx/drawing/chart.rb', line 106

def bg_color
  @bg_color
end

#display_blanks_asSymbol

How to display blank values Options are

  • gap: Display nothing
  • span: Not sure what this does
  • zero: Display as if the value were zero, not blank Default :gap (although this really should vary by chart type and grouping)

Returns:

  • (Symbol)


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

def display_blanks_as
  @display_blanks_as
end

#graphic_frameGraphicFrame (readonly)

A reference to the graphic frame that owns this chart

Returns:



45
46
47
# File 'lib/axlsx/drawing/chart.rb', line 45

def graphic_frame
  @graphic_frame
end

#legend_positionSymbol

Note:

The following are allowed :b :l :r :t :tr

Set the location of the chart's legend

Returns:

  • (Symbol)

    The position of this legend



93
94
95
# File 'lib/axlsx/drawing/chart.rb', line 93

def legend_position
  @legend_position
end

#plot_visible_onlyBoolean

Whether only data from visible cells should be plotted.

Returns:

  • (Boolean)


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

def plot_visible_only
  @plot_visible_only
end

#rounded_cornersBoolean

Whether the chart area shall have rounded corners.

Returns:

  • (Boolean)


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

def rounded_corners
  @rounded_corners
end

#seriesSimpleTypedList (readonly)

A collection of series objects that are applied to the chart

Returns:

  • (SimpleTypedList)


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

def series
  @series
end

#series_typeSeries (readonly)

The type of series to use for this chart.

Returns:



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

def series_type
  @series_type
end

#show_legendBoolean

Show the legend in the chart

Returns:

  • (Boolean)


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

def show_legend
  @show_legend
end

#styleInteger

The style for the chart. see ECMA Part 1 §21.2.2.196

Returns:

  • (Integer)


78
79
80
# File 'lib/axlsx/drawing/chart.rb', line 78

def style
  @style
end

#titleTitle

The title object for the chart.

Returns:



73
74
75
# File 'lib/axlsx/drawing/chart.rb', line 73

def title
  @title
end

#vary_colorsBoolean

Indicates that colors should be varied by datum

Returns:

  • (Boolean)


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

def vary_colors
  @vary_colors
end

#view_3DObject Also known as: view3D

The 3D view properties for the chart



40
41
42
# File 'lib/axlsx/drawing/chart.rb', line 40

def view_3D
  @view_3D
end

Instance Method Details

#add_series(options = {}) ⇒ Series

Adds a new series to the chart's series collection.

Returns:

See Also:



199
200
201
202
# File 'lib/axlsx/drawing/chart.rb', line 199

def add_series(options = {})
  @series_type.new(self, options)
  @series.last
end

#d_lblsObject

TODO: data labels!



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

def d_lbls
  @d_lbls ||= DLbls.new(self.class)
end

#end_at(x = 10, y = 10) ⇒ Marker

This is a short cut method to set the end anchor position If you need finer granularity in positioning use graphic_frame.anchor.to.colOff / rowOff

Parameters:

  • x (Integer) (defaults to: 10)

    The column - default 10

  • y (Integer) (defaults to: 10)

    The row - default 10

Returns:

See Also:



310
311
312
# File 'lib/axlsx/drawing/chart.rb', line 310

def end_at(x = 10, y = 10)
  @graphic_frame.anchor.end_at(x, y)
end

#fromObject

Note:

This will be disconinued in version 2.0.0. please use the start_at method

backwards compatibility to allow chart.to and chart.from access to anchor markers



192
193
194
# File 'lib/axlsx/drawing/chart.rb', line 192

def from
  @graphic_frame.anchor.from
end

#indexInteger

The index of this chart in the workbooks charts collection

Returns:

  • (Integer)


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

def index
  @graphic_frame.anchor.drawing.worksheet.workbook.charts.index(self)
end

#pnString

The part name for this chart

Returns:

  • (String)


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

def pn
  format(CHART_PN, index + 1)
end

#relationshipRelationship

The relationship object for this chart.

Returns:



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

def relationship
  Relationship.new(self, CHART_R, "../#{pn}")
end

#start_at(x = 0, y = 0) ⇒ Marker

This is a short cut method to set the anchor start marker position If you need finer granularity in positioning use

This helper method acceps a fairly wide range of inputs exampled below

reference or cell to use in setting the start marker position.

Examples:


start_at 0, 5 # The anchor start marker is set to 6th row of
the first column

start_at [0, 5] # The anchor start marker is set to start on the 6th row
of the first column

start_at "C1" # The anchor start marker is set to start on the first row
of the third column

start_at sheet.rows.first.cells.last # The anchor start
marker is set to the location of a specific cell.

Parameters:

  • x (Array|String|Cell) (defaults to: 0)

    the column, coordinates, string

  • y (Integer) (defaults to: 0)

    The row

Returns:



299
300
301
# File 'lib/axlsx/drawing/chart.rb', line 299

def start_at(x = 0, y = 0)
  @graphic_frame.anchor.start_at(x, y)
end

#title_size=(v) ⇒ Object

The size of the Title object of the chart.

Parameters:

  • v (String)

    The size for the title object

See Also:



149
150
151
# File 'lib/axlsx/drawing/chart.rb', line 149

def title_size=(v)
  @title.text_size = v unless v.to_s.empty?
end

#toObject

Note:

This will be disconinued in version 2.0.0. Please use the end_at method

backwards compatibility to allow chart.to and chart.from access to anchor markers



186
187
188
# File 'lib/axlsx/drawing/chart.rb', line 186

def to
  @graphic_frame.anchor.to
end

#to_xml_string(str = +'')) ⇒ String

Serializes the object

Parameters:

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

Returns:

  • (String)


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/axlsx/drawing/chart.rb', line 229

def to_xml_string(str = +'')
  str << '<?xml version="1.0" encoding="UTF-8"?>'
  str << '<c:chartSpace xmlns:c="' << XML_NS_C << '" xmlns:a="' << XML_NS_A << '" xmlns:r="' << XML_NS_R << '">'
  str << '<c:date1904 val="' << Axlsx::Workbook.date1904.to_s << '"/>'
  str << '<c:roundedCorners val="' << rounded_corners.to_s << '"/>'
  str << '<c:style val="' << style.to_s << '"/>'
  str << '<c:chart>'
  @title.to_xml_string(str) unless @title.empty?
  str << '<c:autoTitleDeleted val="' << @title.nil?.to_s << '"/>'
  @view_3D.to_xml_string(str) if @view_3D
  str << '<c:floor><c:thickness val="0"/></c:floor>'
  str << '<c:sideWall><c:thickness val="0"/></c:sideWall>'
  str << '<c:backWall><c:thickness val="0"/></c:backWall>'
  str << '<c:plotArea>'
  str << '<c:layout/>'
  yield if block_given?
  str << '</c:plotArea>'
  if @show_legend
    str << '<c:legend>'
    str << '<c:legendPos val="' << @legend_position.to_s << '"/>'
    str << '<c:layout/>'
    str << '<c:overlay val="0"/>'
    str << '</c:legend>'
  end
  str << '<c:plotVisOnly val="' << @plot_visible_only.to_s << '"/>'
  str << '<c:dispBlanksAs val="' << display_blanks_as.to_s << '"/>'
  str << '<c:showDLblsOverMax val="1"/>'
  str << '</c:chart>'
  if bg_color
    str << '<c:spPr>'
    str << '<a:solidFill>'
    str << '<a:srgbClr val="' << bg_color << '"/>'
    str << '</a:solidFill>'
    str << '<a:ln>'
    str << '<a:noFill/>'
    str << '</a:ln>'
    str << '</c:spPr>'
  end
  str << '<c:printSettings>'
  str << '<c:headerFooter/>'
  str << '<c:pageMargins b="1.0" l="0.75" r="0.75" t="1.0" header="0.5" footer="0.5"/>'
  str << '<c:pageSetup/>'
  str << '</c:printSettings>'
  str << '</c:chartSpace>'
end