Class: Axlsx::Chart

Inherits:
Object
  • Object
show all
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

Direct Known Subclasses

Bar3DChart, Line3DChart, Pie3DChart

Instance Attribute Summary collapse

Instance Method Summary collapse

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)

Yields:

  • (_self)

Yield Parameters:

  • _self (Axlsx::Chart)

    the object that the method was called on



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/axlsx/drawing/chart.rb', line 44

def initialize(frame, options={})
  @style = 2
  @graphic_frame=frame
  @graphic_frame.anchor.drawing.worksheet.workbook.charts << self
  @series = SimpleTypedList.new Series
  @show_legend = true
  @series_type = Series
  @title = Title.new
  options.each do |o|
    self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
  end
  start_at(*options[:start_at]) if options[:start_at]
  end_at(*options[:end_at]) if options[:start_at]
  yield self if block_given?
end

Instance Attribute Details

#graphic_frameGraphicFrame (readonly)

A reference to the graphic frame that owns this chart

Returns:



14
15
16
# File 'lib/axlsx/drawing/chart.rb', line 14

def graphic_frame
  @graphic_frame
end

#seriesSimpleTypedList (readonly)

A collection of series objects that are applied to the chart

Returns:

  • (SimpleTypedList)


18
19
20
# File 'lib/axlsx/drawing/chart.rb', line 18

def series
  @series
end

#series_typeSeries (readonly)

The type of series to use for this chart.

Returns:



22
23
24
# File 'lib/axlsx/drawing/chart.rb', line 22

def series_type
  @series_type
end

#show_legendBoolean

Show the legend in the chart

Returns:

  • (Boolean)


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

def show_legend
  @show_legend
end

#styleInteger

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

Returns:

  • (Integer)


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

def style
  @style
end

#titleTitle

The title object for the chart.

Returns:



29
30
31
# File 'lib/axlsx/drawing/chart.rb', line 29

def title
  @title
end

#view3DObject

The 3D view properties for the chart



10
11
12
# File 'lib/axlsx/drawing/chart.rb', line 10

def view3D
  @view3D
end

Instance Method Details

#add_series(options = {}) ⇒ Series

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

Returns:

See Also:



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

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

#end_at(x, y = 0) ⇒ 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)

    The column

  • y (Integer) (defaults to: 0)

    The row

Returns:



169
170
171
172
173
# File 'lib/axlsx/drawing/chart.rb', line 169

def end_at(x, y=0)
  x, y = *parse_coord_args(x, y)
  @graphic_frame.anchor.to.col = x
  @graphic_frame.anchor.to.row = 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



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

def from
  @graphic_frame.anchor.from
end

#indexInteger

The index of this chart in the workbooks charts collection

Returns:

  • (Integer)


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

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

#pnString

The part name for this chart

Returns:

  • (String)


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

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

#start_at(x, y = 0) ⇒ Marker

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

Parameters:

  • x (Integer)

    The column

  • y (Integer) (defaults to: 0)

    The row

Returns:



157
158
159
160
161
# File 'lib/axlsx/drawing/chart.rb', line 157

def start_at(x, y=0)
  x, y = *parse_coord_args(x, y)
  @graphic_frame.anchor.from.col = x
  @graphic_frame.anchor.from.row = y
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



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

def to
  @graphic_frame.anchor.to
end

#to_xmlObject

Chart Serialization serializes the chart



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

def to_xml
  builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
    xml.send('c:chartSpace', :'xmlns:c' => XML_NS_C, :'xmlns:a' => XML_NS_A) {
      xml[:c].date1904 :val => Axlsx::Workbook.date1904
      xml[:c].style :val=>style
      xml[:c].chart {
        @title.to_xml(xml)
        xml.autoTitleDeleted :val=>0
        @view3D.to_xml(xml) unless @view3D.nil?
        
        xml.floor { xml.thickness(:val=>0) }
        xml.sideWall { xml.thickness(:val=>0) }
        xml.backWall { xml.thickness(:val=>0) }
        xml.plotArea {
          xml.layout
          yield xml if block_given?
        }
        if @show_legend
            xml.legend {
            xml.legendPos :val => "r"
            xml.layout
            xml.overlay :val => 0                  
          }
        end
        xml.plotVisOnly :val => 1
        xml.dispBlanksAs :val => :zero
        xml.showDLblsOverMax :val => 1
      }
      
    }
  end
  builder.to_xml(:save_with => 0)
end