summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-05-03 09:10:23 +0900
committerRandy Morgan <[email protected]>2012-05-03 09:10:23 +0900
commite622dc54069074198990beb0462da630bad38dd0 (patch)
tree4065eb11cf4dcd68440bf8347af4509c130fb11e /lib/axlsx
parent2dfdd1f26b1fc748da3b4edad9aeeaeae842aedb (diff)
downloadcaxlsx-e622dc54069074198990beb0462da630bad38dd0.tar.gz
caxlsx-e622dc54069074198990beb0462da630bad38dd0.zip
rebuild series data base objects with full implementation.
Address shape validation error.
Diffstat (limited to 'lib/axlsx')
-rw-r--r--lib/axlsx/drawing/ax_data_source.rb25
-rw-r--r--lib/axlsx/drawing/bar_series.rb10
-rw-r--r--lib/axlsx/drawing/cat_axis_data.rb34
-rw-r--r--lib/axlsx/drawing/drawing.rb15
-rw-r--r--lib/axlsx/drawing/line_series.rb8
-rw-r--r--lib/axlsx/drawing/named_axis_data.rb35
-rw-r--r--lib/axlsx/drawing/num_data.rb52
-rw-r--r--lib/axlsx/drawing/num_data_source.rb58
-rw-r--r--lib/axlsx/drawing/num_val.rb32
-rw-r--r--lib/axlsx/drawing/pie_series.rb8
-rw-r--r--lib/axlsx/drawing/scatter_series.rb4
-rw-r--r--lib/axlsx/drawing/str_data.rb42
-rw-r--r--lib/axlsx/drawing/str_val.rb33
13 files changed, 265 insertions, 91 deletions
diff --git a/lib/axlsx/drawing/ax_data_source.rb b/lib/axlsx/drawing/ax_data_source.rb
new file mode 100644
index 00000000..5bbfd55b
--- /dev/null
+++ b/lib/axlsx/drawing/ax_data_source.rb
@@ -0,0 +1,25 @@
+module Axlsx
+ # An axis data source that can contain referenced or literal strings or numbers
+ # @note only string data types are supported - mainly because we have not implemented a chart type that requires a numerical axis value
+ class AxDataSource < NumDataSource
+
+ # allowed element tag names
+ # @return [Array]
+ def self.allowed_tag_names
+ [:xVal, :cat]
+ end
+
+ # creates a new NumDataSource object
+ # @option options [Array] data An array of Cells or Numeric objects
+ # @option options [Symbol] tag_name see tag_name
+ def initialize(options={})
+ @tag_name = :cat
+ @data_type = StrData
+ @ref_tag_name = :strRef
+ super(options)
+ end
+
+ end
+
+end
+
diff --git a/lib/axlsx/drawing/bar_series.rb b/lib/axlsx/drawing/bar_series.rb
index 25523410..5863dacc 100644
--- a/lib/axlsx/drawing/bar_series.rb
+++ b/lib/axlsx/drawing/bar_series.rb
@@ -8,7 +8,7 @@ module Axlsx
# The data for this series.
- # @return [Array, SimpleTypedList]
+ # @return [NumDataSource]
attr_reader :data
# The labels for this series.
@@ -34,8 +34,8 @@ module Axlsx
@shape = :box
@colors = []
super(chart, options)
- self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil?
- self.data = NamedAxisData.new(:val, options[:data]) unless options[:data].nil?
+ self.labels = AxDataSource.new({:data => options[:labels]}) unless options[:labels].nil?
+ self.data = NumDataSource.new(options) unless options[:data].nil?
end
# @see colors
@@ -71,10 +71,10 @@ module Axlsx
private
# assigns the data for this series
- def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end
+ def data=(v) DataTypeValidator.validate "Series.data", [NumDataSource], v; @data = v; end
# assigns the labels for this series
- def labels=(v) DataTypeValidator.validate "Series.labels", [SimpleTypedList], v; @labels = v; end
+ def labels=(v) DataTypeValidator.validate "Series.labels", [AxDataSource], v; @labels = v; end
end
diff --git a/lib/axlsx/drawing/cat_axis_data.rb b/lib/axlsx/drawing/cat_axis_data.rb
deleted file mode 100644
index 52a6a542..00000000
--- a/lib/axlsx/drawing/cat_axis_data.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-# encoding: UTF-8
-module Axlsx
- # The CatAxisData class serializes the category axis data for a chart
- class CatAxisData < SimpleTypedList
-
- # Create a new CatAxisData object
- # @param [Array, SimpleTypedList] data the data for this category axis. This can be a simple array or a simple typed list of cells.
- def initialize(data=[])
- super Object
- @list.concat data if data.is_a?(Array)
- data.each { |i| @list << i } if data.is_a?(SimpleTypedList)
- end
-
- # Serializes the object
- # @param [String] str
- # @return [String]
- def to_xml_string(str = '')
- str << '<c:cat>'
- str << '<c:strRef>'
- str << '<c:f>' << Axlsx::cell_range(@list) << '</c:f>'
- str << '<c:strCache>'
- str << '<c:ptCount val="' << size.to_s << '"/>'
- each_with_index do |item, index|
- v = item.is_a?(Cell) ? item.value.to_s : item
- str << '<c:pt idx="' << index.to_s << '"><c:v>' << v.to_s << '</c:v></c:pt>'
- end
- str << '</c:strCache>'
- str << '</c:strRef>'
- str << '</c:cat>'
- end
-
- end
-
-end
diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb
index 7e734113..17b6e0b5 100644
--- a/lib/axlsx/drawing/drawing.rb
+++ b/lib/axlsx/drawing/drawing.rb
@@ -10,17 +10,18 @@ module Axlsx
require 'axlsx/drawing/scaling.rb'
require 'axlsx/drawing/axis.rb'
+
+ require 'axlsx/drawing/str_val.rb'
+ require 'axlsx/drawing/num_val.rb'
+ require 'axlsx/drawing/str_data.rb'
+ require 'axlsx/drawing/num_data.rb'
+ require 'axlsx/drawing/num_data_source.rb'
+ require 'axlsx/drawing/ax_data_source.rb'
+
require 'axlsx/drawing/ser_axis.rb'
require 'axlsx/drawing/cat_axis.rb'
require 'axlsx/drawing/val_axis.rb'
-
- #TODO rewrite these classes to fully implement:
- # CT_AxDataSource for xVal and cat(Category Axis Data)
- # CT_NumDataSource for yVal and val(Values)
- require 'axlsx/drawing/cat_axis_data.rb'
- require 'axlsx/drawing/named_axis_data.rb'
-
require 'axlsx/drawing/marker.rb'
require 'axlsx/drawing/one_cell_anchor.rb'
diff --git a/lib/axlsx/drawing/line_series.rb b/lib/axlsx/drawing/line_series.rb
index 1d785a23..017822ed 100644
--- a/lib/axlsx/drawing/line_series.rb
+++ b/lib/axlsx/drawing/line_series.rb
@@ -26,8 +26,8 @@ module Axlsx
def initialize(chart, options={})
@labels, @data = nil, nil
super(chart, options)
- @labels = CatAxisData.new(options[:labels]) unless options[:labels].nil?
- @data = NamedAxisData.new('val', options[:data]) unless options[:data].nil?
+ @labels = AxDataSource.new(:data => options[:labels]) unless options[:labels].nil?
+ @data = NumDataSource.new(options) unless options[:data].nil?
end
# @see color
@@ -54,10 +54,10 @@ module Axlsx
private
# assigns the data for this series
- def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end
+ def data=(v) DataTypeValidator.validate "Series.data", [NumDataSource], v; @data = v; end
# assigns the labels for this series
- def labels=(v) DataTypeValidator.validate "Series.labels", [SimpleTypedList], v; @labels = v; end
+ def labels=(v) DataTypeValidator.validate "Series.labels", [AxDataSource], v; @labels = v; end
end
end
diff --git a/lib/axlsx/drawing/named_axis_data.rb b/lib/axlsx/drawing/named_axis_data.rb
deleted file mode 100644
index 2272976c..00000000
--- a/lib/axlsx/drawing/named_axis_data.rb
+++ /dev/null
@@ -1,35 +0,0 @@
-# encoding: UTF-8
-module Axlsx
- # The NamedAxisData class manages the values for a chart value series.
- class NamedAxisData < CatAxisData
-
- # creates a new NamedAxisData Object
- # @param [String] name The serialized node name for the axis data object
- # @param [Array] The data to associate with the axis data object
- def initialize(name, data=[])
- super(data)
- @name = name
- end
-
- # Serializes the object
- # @param [String] str
- # @return [String]
- def to_xml_string(str = '')
- str << '<c:' << @name.to_s << '>'
- str << '<c:numRef>'
- str << '<c:f>' << Axlsx::cell_range(@list) << '</c:f>'
- str << '<c:numCache>'
- str << '<c:formatCode>General</c:formatCode>'
- str << '<c:ptCount val="' << size.to_s << '"/>'
- each_with_index do |item, index|
- v = item.is_a?(Cell) ? item.value.to_s : item
- str << '<c:pt idx="' << index.to_s << '"><c:v>' << v.to_s << '</c:v></c:pt>'
- end
- str << '</c:numCache>'
- str << '</c:numRef>'
- str << '</c:' << @name.to_s << '>'
- end
-
- end
-
-end
diff --git a/lib/axlsx/drawing/num_data.rb b/lib/axlsx/drawing/num_data.rb
new file mode 100644
index 00000000..802a0216
--- /dev/null
+++ b/lib/axlsx/drawing/num_data.rb
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+module Axlsx
+
+ #This class specifies data for a particular data point. It is used for both numCache and numLit object
+ class NumData
+
+ # A string representing the format code to apply. For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute.
+ # @return [String]
+ attr_reader :format_code
+
+ # creates a new NumVal object
+ # @option options [String] formatCode
+ # @option options [Array] :data
+ # @see StrData
+ def initialize(options={})
+ @format_code = "General"
+ @pt = SimpleTypedList.new NumVal
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ # Creates the val objects for this data set. I am not overly confident this is going to play nicely with time and data types.
+ # @param [Array] values An array of cells or values.
+ def data=(values=[])
+ @tag_name = values.first.is_a?(Cell) ? :numCache : :numLit
+ values.each do |value|
+ v = value.is_a?(Cell) ? value.value : value
+ @pt << NumVal.new(:v => v)
+ end
+ end
+
+ # @see format_code
+ def format_code=(v='General')
+ Axlsx::validate_string(v)
+ @format_code = v
+ end
+
+ # serialize the object
+ def to_xml_string(str = "")
+ str << '<c:' << @tag_name.to_s << '>'
+ str << '<c:formatCode>' << format_code.to_s << '</c:formatCode>'
+ str << '<c:ptCount val="' << @pt.size.to_s << '"/>'
+ @pt.each_with_index do |num_val, index|
+ num_val.to_xml_string index, str
+ end
+ str << '</c:' << @tag_name.to_s << '>'
+ end
+
+ end
+
+end
diff --git a/lib/axlsx/drawing/num_data_source.rb b/lib/axlsx/drawing/num_data_source.rb
new file mode 100644
index 00000000..60628883
--- /dev/null
+++ b/lib/axlsx/drawing/num_data_source.rb
@@ -0,0 +1,58 @@
+module Axlsx
+ # A numeric data source for use by charts.
+ class NumDataSource
+
+ # The tag name to use when serializing this data source.
+ # Only items defined in allowed_tag_names are allowed
+ # @return [Symbol]
+ attr_reader :tag_name
+
+ attr_reader :data
+
+ # allowed element tag names
+ # @return [Array]
+ def self.allowed_tag_names
+ [:yVal, :val]
+ end
+
+ # creates a new NumDataSource object
+ # @option options [Array] data An array of Cells or Numeric objects
+ # @option options [Symbol] tag_name see tag_name
+ def initialize(options={})
+ # override these three in child classes
+ @data_type ||= NumData
+ @tag_name ||= :val
+ @ref_tag_name ||= :numRef
+
+ @f = nil
+ @data = @data_type.new(options)
+ if options[:data] && options[:data].first.is_a?(Cell)
+ @f = Axlsx::cell_range(options[:data])
+ end
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ # sets the tag name for this data source
+ # @param [Symbol] One of the allowed_tag_names
+ def tag_name=(v)
+ Axlsx::RestrictionValidator.validate "#{self.class.name}.tag_name", self.class.allowed_tag_names, v
+ @tag_name = v
+ end
+
+ def to_xml_string(str="")
+ str << '<c:' << tag_name.to_s << '>'
+ if @f
+ str << '<c:' << @ref_tag_name.to_s << '>'
+ str << '<c:f>' << @f.to_s << '</c:f>'
+ end
+ @data.to_xml_string str
+ if @f
+ str << '</c:' << @ref_tag_name.to_s << '>'
+ end
+ str << '</c:' << tag_name.to_s << '>'
+ end
+ end
+end
+
diff --git a/lib/axlsx/drawing/num_val.rb b/lib/axlsx/drawing/num_val.rb
new file mode 100644
index 00000000..ed655732
--- /dev/null
+++ b/lib/axlsx/drawing/num_val.rb
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+module Axlsx
+
+ #This class specifies data for a particular data point.
+ class NumVal < StrVal
+
+ # A string representing the format code to apply.
+ # For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute.
+ # @return [String]
+ attr_reader :format_code
+
+ # creates a new NumVal object
+ # @option options [String] formatCode
+ # @option options [Integer] v
+ def initialize(options={})
+ @format_code = "General"
+ super(options)
+ end
+
+ # @see format_code
+ def format_code=(v)
+ Axlsx::validate_string(v)
+ @format_code = v
+ end
+
+ # serialize the object
+ def to_xml_string(idx, str = "")
+ Axlsx::validate_unsigned_int(idx)
+ str << '<c:pt idx="' << idx.to_s << '" formatCode="' << format_code << '"><c:v>' << v.to_s << '</c:v></c:pt>'
+ end
+ end
+end
diff --git a/lib/axlsx/drawing/pie_series.rb b/lib/axlsx/drawing/pie_series.rb
index 13f3cd5d..28056c43 100644
--- a/lib/axlsx/drawing/pie_series.rb
+++ b/lib/axlsx/drawing/pie_series.rb
@@ -32,8 +32,8 @@ module Axlsx
@explosion = nil
@colors = []
super(chart, options)
- self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil?
- self.data = NamedAxisData.new(:val, options[:data]) unless options[:data].nil?
+ self.labels = AxDataSource.new(:data => options[:labels]) unless options[:labels].nil?
+ self.data = NumDataSource.new(options) unless options[:data].nil?
end
# @see colors
@@ -64,10 +64,10 @@ module Axlsx
private
# assigns the data for this series
- def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end
+ def data=(v) DataTypeValidator.validate "Series.data", [NumDataSource], v; @data = v; end
# assigns the labels for this series
- def labels=(v) DataTypeValidator.validate "Series.labels", [SimpleTypedList], v; @labels = v; end
+ def labels=(v) DataTypeValidator.validate "Series.labels", [AxDataSource], v; @labels = v; end
end
diff --git a/lib/axlsx/drawing/scatter_series.rb b/lib/axlsx/drawing/scatter_series.rb
index 3f4bf289..6084c110 100644
--- a/lib/axlsx/drawing/scatter_series.rb
+++ b/lib/axlsx/drawing/scatter_series.rb
@@ -25,8 +25,8 @@ module Axlsx
def initialize(chart, options={})
@xData, @yData = nil
super(chart, options)
- @xData = NamedAxisData.new("xVal", options[:xData]) unless options[:xData].nil?
- @yData = NamedAxisData.new("yVal", options[:yData]) unless options[:yData].nil?
+ @xData = AxDataSource.new(:tag_name => :xVal, :data => options[:xData]) unless options[:xData].nil?
+ @yData = NumDataSource.new({:tag_name => :yVal, :data => options[:yData]}) unless options[:yData].nil?
end
# @see color
diff --git a/lib/axlsx/drawing/str_data.rb b/lib/axlsx/drawing/str_data.rb
new file mode 100644
index 00000000..cb4ff54f
--- /dev/null
+++ b/lib/axlsx/drawing/str_data.rb
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+module Axlsx
+
+ #This specifies the last string data used for a chart. (e.g. strLit and strCache)
+ # This class is extended for NumData to include the formatCode attribute required for numLit and numCache
+ class StrData
+
+ # creates a new StrVal object
+ # @option options [Array] :data
+ # @option options [String] :tag_name
+ def initialize(options={})
+ @tag_prefix = :str
+ @type = StrVal
+ @pt = SimpleTypedList.new(@type)
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ # Creates the val objects for this data set. I am not overly confident this is going to play nicely with time and data types.
+ # @param [Array] values An array of cells or values.
+ def data=(values=[])
+ @tag_name = values.first.is_a?(Cell) ? :strCache : :strLit
+ values.each do |value|
+ v = value.is_a?(Cell) ? value.value : value
+ @pt << @type.new(:v => v)
+ end
+ end
+
+ # serialize the object
+ def to_xml_string(str = "")
+ str << '<c:' << @tag_name.to_s << '>'
+ str << '<c:ptCount val="' << @pt.size.to_s << '"/>'
+ @pt.each_with_index do |value, index|
+ value.to_xml_string index, str
+ end
+ str << '</c:' << @tag_name.to_s << '>'
+ end
+
+ end
+
+end
diff --git a/lib/axlsx/drawing/str_val.rb b/lib/axlsx/drawing/str_val.rb
new file mode 100644
index 00000000..5f453d97
--- /dev/null
+++ b/lib/axlsx/drawing/str_val.rb
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+module Axlsx
+
+ #This class specifies data for a particular data point.
+ class StrVal
+
+ # a string value.
+ # @return [String]
+ attr_reader :v
+
+ # creates a new StrVal object
+ # @option options [String] v
+ def initialize(options={})
+ @v = ""
+ @idx = 0
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+ # @see v
+ def v=(v)
+ @v = v.to_s
+ end
+
+ # serialize the object
+ def to_xml_string(idx, str = "")
+ Axlsx::validate_unsigned_int(idx)
+ str << '<c:pt idx="' << idx.to_s << '"><c:v>' << v.to_s << '</c:v></c:pt>'
+ end
+
+ end
+
+end