summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/drawing/pie_series.rb
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2011-11-20 23:22:04 +0900
committerRandy Morgan <[email protected]>2011-11-20 23:22:04 +0900
commite53f04284618713b0a90b7a691425c380e829476 (patch)
tree801fea138160f9af426d62bf94ad5bf97123ece9 /lib/axlsx/drawing/pie_series.rb
downloadcaxlsx-e53f04284618713b0a90b7a691425c380e829476.tar.gz
caxlsx-e53f04284618713b0a90b7a691425c380e829476.zip
first commit
Diffstat (limited to 'lib/axlsx/drawing/pie_series.rb')
-rw-r--r--lib/axlsx/drawing/pie_series.rb88
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/axlsx/drawing/pie_series.rb b/lib/axlsx/drawing/pie_series.rb
new file mode 100644
index 00000000..98b61728
--- /dev/null
+++ b/lib/axlsx/drawing/pie_series.rb
@@ -0,0 +1,88 @@
+module Axlsx
+ # A PieSeries defines the title, data and labels for pie charts
+ # @note The recommended way to manage series is to use Chart#add_series
+ # @see Worksheet#add_chart
+ # @see Chart#add_series
+ class PieSeries < Series
+
+ # The data for this series.
+ # @return [Array, SimpleTypedList]
+ attr_reader :data
+
+
+ # The labels for this series.
+ # @return [Array, SimpleTypedList]
+ attr_reader :labels
+
+
+ # The explosion for this series
+ # @return [Array, SimpleTypedList]
+ attr_accessor :explosion
+
+ # Creates a new series
+ # @option options [Array, SimpleTypedList] data
+ # @option options [Array, SimpleTypedList] labels
+ # @option options [String] title
+ # @option options [Integer] explosion
+ # @param [Chart] chart
+ def initialize(chart, options={})
+ super(chart, options)
+ self.data = options[:data] || []
+ self.labels = options[:labels] || []
+ end
+
+ def explosion=(v) Axlsx::validate_unsigned_int(v); @explosion = v; 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)
+ 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
+
+
+ private
+
+
+ # 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
+
+ end
+
+end