Class: Axlsx::PieSeries

Inherits:
Series
  • Object
show all
Defined in:
lib/axlsx/drawing/pie_series.rb

Overview

Note:

The recommended way to manage series is to use Chart#add_series

A PieSeries defines the title, data and labels for pie charts

Instance Attribute Summary collapse

Attributes inherited from Series

#chart, #index, #order, #title

Instance Method Summary collapse

Constructor Details

#initialize(chart, options = {}) ⇒ PieSeries

Creates a new series

Parameters:

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

    a customizable set of options

Options Hash (options):



28
29
30
31
32
# File 'lib/axlsx/drawing/pie_series.rb', line 28

def initialize(chart, options={})
  super(chart, options)
  self.data = options[:data]  || []
  self.labels = options[:labels] || []
end

Instance Attribute Details

#dataArray, SimpleTypedList

The data for this series.

Returns:



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

def data
  @data
end

#explosionArray, SimpleTypedList

The explosion for this series

Returns:



20
21
22
# File 'lib/axlsx/drawing/pie_series.rb', line 20

def explosion
  @explosion
end

#labelsArray, SimpleTypedList

The labels for this series.

Returns:



15
16
17
# File 'lib/axlsx/drawing/pie_series.rb', line 15

def labels
  @labels
end

Instance Method Details

#to_xml(xml) ⇒ String

Serializes the series

Parameters:

  • xml (Nokogiri::XML::Builder)

    The document builder instance this objects xml will be added to.

Returns:

  • (String)


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

def to_xml(xml)
  super(xml) do  |xml|
    xml.send('c:explosion', :val=>@explosion) unless @explosion.nil?
    if !labels.empty?
      xml.send('c:cat') {
        xml.send('c:strRef') {
          xml.send('c:f', Axlsx::cell_range(labels))
          xml.send('c:strCache') {
            xml.send('c:ptCount', :val=>labels.size)
            labels.each_with_index do |cell, index|
              v = cell.is_a?(Cell) ? cell.value : cell
              xml.send('c:pt', :idx=>index) {
                xml.send('c:v', v)
              }                          
            end
          }
        }
      }
    end
    xml.send('c:val') {
      xml.send('c:numRef') {
        xml.send('c:f', Axlsx::cell_range(data))
        xml.send('c:numCache') {
          xml.send('c:formatCode', 'General')
          xml.send('c:ptCount', :val=>data.size)
          data.each_with_index do |cell, index|
            v = cell.is_a?(Cell) ? cell.value : cell
            xml.send('c:pt', :idx=>index) {
              xml.send('c:v', v) 
            }
          end
        }                        
      }
    }
    
  end      
end