module Axlsx # A Series defines the title, data and labels for chart data. # @note The recommended way to manage series is to use Chart#add_series # @see Worksheet#add_chart # @see Chart#add_series class Series # The series title. # @return [String] attr_accessor :title # The chart that owns this series # @return [Chart] attr_reader :chart # The data for this series. # @return [Array, SimpleTypedList] attr_reader :data # The index of this series in the chart's series. # @return [Integer] attr_reader :index # The labels for this series. # @return [Array, SimpleTypedList] attr_reader :labels # Creates a new series # @option options [Array, SimpleTypedList] data # @option options [Array, SimpleTypedList] labels # @option options [String] title # @param [Chart] chart def initialize(chart, options={}) self.chart = chart @chart.series << self self.data = options[:data] || [] self.labels = options[:labels] || [] @title = options[:title] || '' end # Serializes the series # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] def to_xml(xml) xml.send('c:ser') { xml.send('c:idx', :val=>index) xml.send('c:order', :val=>index) xml.send('c:tx') { xml.send('c:v', self.title) } if !labels.empty? xml.send('c:cat') { xml.send('c:strRef') { xml.send('c:f', 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', 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 def index @chart.series.index(self) end private # determines the cell range for the items provided def range(items) return "" unless items.first.is_a? Cell "#{items.first.row.worksheet.name}!" + "#{items.first.r_abs}:#{items.last.r_abs}" end # assigns the data for this series def data=(v) DataTypeValidator.validate "Series.data", [Array, SimpleTypedList], v; @data = v; end # assigns the labels for this series def labels=(v) DataTypeValidator.validate "Series.labels", [Array, SimpleTypedList], v; @labels = v; end # assigns the chart for this series def chart=(v) DataTypeValidator.validate "Series.chart", [Chart, Pie3DChart], v; @chart = v; end end end