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:

Direct Known Subclasses

Bar3DChart, BubbleChart, LineChart, Pie3DChart, ScatterChart

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from OptionsParser

#parse_options

Constructor Details

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

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)
  • 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.

Yields:

  • (_self)

Yield Parameters:

  • _self (Axlsx::Chart)

    the object that the method was called on



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/axlsx/drawing/chart.rb', line 16

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
  @display_blanks_as = :gap
  @series_type = Series
  @title = Title.new
  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

- (Symbol) display_blanks_as

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)


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

def display_blanks_as
  @display_blanks_as
end

- (GraphicFrame) graphic_frame (readonly)

A reference to the graphic frame that owns this chart

Returns:



38
39
40
# File 'lib/axlsx/drawing/chart.rb', line 38

def graphic_frame
  @graphic_frame
end

- (SimpleTypedList) series (readonly)

A collection of series objects that are applied to the chart

Returns:

  • (SimpleTypedList)


42
43
44
# File 'lib/axlsx/drawing/chart.rb', line 42

def series
  @series
end

- (Series) series_type (readonly)

The type of series to use for this chart.

Returns:



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

def series_type
  @series_type
end

- (Boolean) show_legend

Show the legend in the chart

Returns:

  • (Boolean)


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

def show_legend
  @show_legend
end

- (Integer) style

The style for the chart. see ECMA Part 1 ยง21.2.2.196

Returns:

  • (Integer)


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

def style
  @style
end

- (Title) title

The title object for the chart.

Returns:



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

def title
  @title
end

- (Boolean) vary_colors

Indicates that colors should be varied by datum

Returns:

  • (Boolean)


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

def vary_colors
  @vary_colors
end

- (Object) view_3D Also known as: view3D

The 3D view properties for the chart



33
34
35
# File 'lib/axlsx/drawing/chart.rb', line 33

def view_3D
  @view_3D
end

Instance Method Details

- (Series) add_series(options = {})

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

Returns:

See Also:



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

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

- (Object) d_lbls

TODO data labels!



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

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

- (Marker) end_at(x = 10, y = 10)

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:

  • start_at


222
223
224
# File 'lib/axlsx/drawing/chart.rb', line 222

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

- (Object) from

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



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

def from
  @graphic_frame.anchor.from
end

- (Integer) index

The index of this chart in the workbooks charts collection

Returns:

  • (Integer)


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

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

- (String) pn

The part name for this chart

Returns:



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

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

- (Relationship) relationship

The relationship object for this chart.

Returns:



85
86
87
# File 'lib/axlsx/drawing/chart.rb', line 85

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

- (Marker) start_at(x = 0, y = 0)

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:



211
212
213
# File 'lib/axlsx/drawing/chart.rb', line 211

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

- (Object) to

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



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

def to
  @graphic_frame.anchor.to
end

- (String) to_xml_string(str = '')

Serializes the object

Parameters:

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

Returns:



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
# File 'lib/axlsx/drawing/chart.rb', line 152

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:style val="' << style.to_s << '"/>')
  str << '<c:chart>'
  @title.to_xml_string str
  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="r"/>'
    str << '<c:layout/>'
    str << '<c:overlay val="0"/>'
    str << '</c:legend>'
  end
  str << '<c:plotVisOnly val="1"/>'
  str << ('<c:dispBlanksAs val="' << display_blanks_as.to_s << '"/>')
  str << '<c:showDLblsOverMax val="1"/>'
  str << '</c:chart>'
  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