summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2011-11-21 23:42:03 +0900
committerRandy Morgan <[email protected]>2011-11-21 23:42:03 +0900
commit11729af6e1dc9492ba1fadb3681b885a4ff7dbbb (patch)
treec47d5e007b9711066bc46be5914fe73e75bace6c
parente53a0b7223e531046dd263ff5686d6e34196512a (diff)
downloadcaxlsx-11729af6e1dc9492ba1fadb3681b885a4ff7dbbb.tar.gz
caxlsx-11729af6e1dc9492ba1fadb3681b885a4ff7dbbb.zip
Adding in support for line charts, style attribute for all charts and minor bug fixes.
-rw-r--r--README.md20
-rw-r--r--assets/example.rb13
-rw-r--r--lib/axlsx/drawing/bar_3D_chart.rb4
-rw-r--r--lib/axlsx/drawing/bar_series.rb1
-rw-r--r--lib/axlsx/drawing/chart.rb11
-rw-r--r--lib/axlsx/drawing/drawing.rb6
-rw-r--r--lib/axlsx/drawing/line_3D_chart.rb93
-rw-r--r--lib/axlsx/drawing/line_series.rb76
-rw-r--r--lib/axlsx/drawing/pie_3D_chart.rb2
-rw-r--r--lib/axlsx/drawing/ser_axis.rb42
-rw-r--r--lib/axlsx/drawing/series.rb16
-rw-r--r--lib/axlsx/drawing/series_title.rb22
-rw-r--r--lib/axlsx/drawing/title.rb3
-rw-r--r--test/drawing/tc_bar_series.rb2
-rw-r--r--test/drawing/tc_chart.rb12
-rw-r--r--test/drawing/tc_line_3d_chart.rb48
-rw-r--r--test/drawing/tc_line_series.tc34
-rw-r--r--test/drawing/tc_pie_series.rb2
-rw-r--r--test/drawing/tc_ser_axis.rb23
-rw-r--r--test/drawing/tc_series.rb2
-rw-r--r--test/drawing/tc_series_title.rb34
-rw-r--r--test/tc_package.rb28
22 files changed, 458 insertions, 36 deletions
diff --git a/README.md b/README.md
index 6b65c77a..6d1ffd1e 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@ Axlsx: Office Open XML Spreadsheet Generation
**Copyright**: 2011
**License**: MIT License
**Latest Version**: 1.0.4
-**Release Date**: November 20th 2011
+**Release Date**: November 21st 2011
Synopsis
--------
@@ -115,6 +115,20 @@ Usage
puts error.inspect
end
+#Generating A Line Chart
+
+ p = Axlsx::Package.new
+ p.workbook.add_worksheet do |sheet|
+ sheet.add_row ["First", 1, 5, 7, 9]
+ sheet.add_row ["Second", 5, 2, 14, 9]
+ sheet.add_chart(Axlsx::Line3DChart, :start_at => [0,2], :end_at => [10, 15], :title=>"example 6: Line Chart") do |chart|
+ chart.add_series :data=>sheet.rows.first.cells[(1..-1)], :title=> sheet.rows.first.cells.first
+ chart.add_series :data=>sheet.rows.last.cells[(1..-1)], :title=> sheet.rows.last.cells.first
+ end
+ end
+ p.serialize("example6.xlsx")
+
+
### Documentation
This gem is 100% documented with YARD, an exceptional documentation library. To see documentation for this, and all the gems installed on your system use:
@@ -134,7 +148,9 @@ Changelog
- altered package to accept a filename string for serialization instead of a File object.
- Updated specs to conform
- More examples for readme
-
+- **October.21.11**: 1.05
+ - Added support for line charts
+ - Updated examples and readme
Copyright
---------
diff --git a/assets/example.rb b/assets/example.rb
index b3fe6f54..fd02598f 100644
--- a/assets/example.rb
+++ b/assets/example.rb
@@ -31,6 +31,7 @@
end
p.serialize("example3.xlsx")
+
#Using Custom Styles
p = Axlsx::Package.new
@@ -68,3 +69,15 @@
puts error.inspect
end
+#Generating A Line Chart
+
+ p = Axlsx::Package.new
+ p.workbook.add_worksheet do |sheet|
+ sheet.add_row ["First", 1, 5, 7, 9]
+ sheet.add_row ["Second", 5, 2, 14, 9]
+ sheet.add_chart(Axlsx::Line3DChart, :start_at => [0,2], :end_at => [10, 15], :title=>"example 6: Line Chart") do |chart|
+ chart.add_series :data=>sheet.rows.first.cells[(1..-1)], :title=> sheet.rows.first.cells.first
+ chart.add_series :data=>sheet.rows.last.cells[(1..-1)], :title=> sheet.rows.last.cells.first
+ end
+ end
+ p.serialize("example6.xlsx")
diff --git a/lib/axlsx/drawing/bar_3D_chart.rb b/lib/axlsx/drawing/bar_3D_chart.rb
index 2bb7d88b..1d7e2bba 100644
--- a/lib/axlsx/drawing/bar_3D_chart.rb
+++ b/lib/axlsx/drawing/bar_3D_chart.rb
@@ -77,8 +77,6 @@ module Axlsx
# @option options [String] gapDepth
# @option options [Symbol] shape
def initialize(frame, options={})
- super(frame, options)
- @series_type = BarSeries
@barDir = :bar
@grouping = :clustered
@catAxId = rand(8 ** 8)
@@ -86,6 +84,8 @@ module Axlsx
@catAxis = CatAxis.new(@catAxId, @valAxId)
@valAxis = ValAxis.new(@valAxId, @catAxId)
@view3D = View3D.new(:rAngAx=>1)
+ super(frame, options)
+ @series_type = BarSeries
end
def barDir=(v)
diff --git a/lib/axlsx/drawing/bar_series.rb b/lib/axlsx/drawing/bar_series.rb
index cc2bbf57..0f46af15 100644
--- a/lib/axlsx/drawing/bar_series.rb
+++ b/lib/axlsx/drawing/bar_series.rb
@@ -73,6 +73,7 @@ module Axlsx
}
}
}
+ xml.send('c:shape', :val=>@shape)
end
end
diff --git a/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb
index 764e90e4..69932842 100644
--- a/lib/axlsx/drawing/chart.rb
+++ b/lib/axlsx/drawing/chart.rb
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
module Axlsx
# A Chart is the superclass for specific charts
# @note Worksheet#add_chart is the recommended way to create charts for your worksheets.
@@ -7,6 +8,11 @@ module Axlsx
# @return [Title]
attr_accessor :title
+ # The style for the chart.
+ # see ECMA Part 1 ยง21.2.2.196
+ # @return [Integer]
+ attr_accessor :style
+
# The 3D view properties for the chart
attr_accessor :view3D
@@ -50,6 +56,7 @@ module Axlsx
# @option options [Cell, String] title
# @option options [Boolean] show_legend
def initialize(frame, options={})
+ @style = 2
@graphic_frame=frame
@graphic_frame.anchor.drawing.worksheet.workbook.charts << self
@series = SimpleTypedList.new Series
@@ -79,6 +86,8 @@ module Axlsx
def show_legend=(v) Axlsx::validate_boolean(v); @show_legend = v; end
+ def style=(v) DataTypeValidator "Chart.style", Integer, v, lambda { |v| v >= 1 && v <= 48 }; @style = v; end
+
# Adds a new series to the chart's series collection.
# @return [Series]
# @see Series
@@ -92,6 +101,8 @@ module Axlsx
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.send('c:date1904', :val=>Axlsx::Workbook.date1904)
+ xml.send('c:style', :val=>style)
xml.send('c:chart') {
@title.to_xml(xml) unless @title.nil?
@view3D.to_xml(xml) unless @view3D.nil?
diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb
index 69c0d9cc..83902143 100644
--- a/lib/axlsx/drawing/drawing.rb
+++ b/lib/axlsx/drawing/drawing.rb
@@ -1,14 +1,17 @@
module Axlsx
+ require 'axlsx/drawing/title.rb'
+ require 'axlsx/drawing/series_title.rb'
require 'axlsx/drawing/series.rb'
require 'axlsx/drawing/pie_series.rb'
require 'axlsx/drawing/bar_series.rb'
+ require 'axlsx/drawing/line_series.rb'
require 'axlsx/drawing/axis.rb'
+ require 'axlsx/drawing/ser_axis.rb'
require 'axlsx/drawing/cat_axis.rb'
require 'axlsx/drawing/val_axis.rb'
require 'axlsx/drawing/view_3D.rb'
require 'axlsx/drawing/scaling.rb'
- require 'axlsx/drawing/title.rb'
require 'axlsx/drawing/graphic_frame.rb'
require 'axlsx/drawing/marker.rb'
@@ -17,6 +20,7 @@ module Axlsx
require 'axlsx/drawing/chart.rb'
require 'axlsx/drawing/pie_3D_chart.rb'
require 'axlsx/drawing/bar_3D_chart.rb'
+ require 'axlsx/drawing/line_3D_chart.rb'
# A Drawing is a canvas for charts. Each worksheet has a single drawing that can specify multiple anchors which reference charts.
# @note The recommended way to manage drawings is to use the Worksheet.add_chart method, specifying the chart class, start and end marker locations.
diff --git a/lib/axlsx/drawing/line_3D_chart.rb b/lib/axlsx/drawing/line_3D_chart.rb
new file mode 100644
index 00000000..39500f75
--- /dev/null
+++ b/lib/axlsx/drawing/line_3D_chart.rb
@@ -0,0 +1,93 @@
+module Axlsx
+
+ # The Line3DChart is a three dimentional line chart (who would have guessed?) that you can add to your worksheet.
+ # @example Creating a chart
+ # # This example creates a line in a single sheet.
+ # require "rubygems" # if that is your preferred way to manage gems!
+ # require "axlsx"
+ #
+ # p = Axlsx::Package.new
+ # ws = p.workbook.add_worksheet
+ # ws.add_row :values => ["This is a chart with no data in the sheet"]
+ #
+ # chart = ws.add_chart(Axlsx::Line3DChart, :start_at=> [0,1], :end_at=>[0,6], :title=>"Most Popular Pets")
+ # chart.add_series :data => [1, 9, 10], :labels => ["Slimy Reptiles", "Fuzzy Bunnies", "Rottweiler"]
+ #
+ # @see Worksheet#add_chart
+ # @see Worksheet#add_row
+ # @see Chart#add_series
+ # @see Series
+ # @see Package#serialize
+ class Line3DChart < Chart
+
+ # the category axis
+ # @return [CatAxis]
+ attr_reader :catAxis
+
+ # the category axis
+ # @return [ValAxis]
+ attr_reader :valAxis
+
+ # the category axis
+ # @return [Axis]
+ attr_reader :serAxis
+
+ # space between bar or column clusters, as a percentage of the bar or column width.
+ # @return [String]
+ attr_accessor :gapDepth
+
+ #grouping for a column, line, or area chart.
+ # must be one of [:percentStacked, :clustered, :standard, :stacked]
+ # @return [Symbol]
+ attr_accessor :grouping
+
+ # validation regex for gap amount percent
+ GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/
+
+ # Creates a new line chart object
+ # @param [GraphicFrame] frame The workbook that owns this chart.
+ # @option options [Symbol] grouping
+ # @option options [String] gapDepth
+ def initialize(frame, options={})
+ @grouping = :standard
+ @catAxId = rand(8 ** 8)
+ @valAxId = rand(8 ** 8)
+ @serAxId = rand(8 ** 8)
+ @catAxis = CatAxis.new(@catAxId, @valAxId)
+ @valAxis = ValAxis.new(@valAxId, @catAxId)
+ @serAxis = SerAxis.new(@serAxId, @valAxId)
+ @view3D = View3D.new(:perspective=>30)
+ super(frame, options)
+ @series_type = LineSeries
+ end
+
+ def grouping=(v)
+ RestrictionValidator.validate "Bar3DChart.grouping", [:percentStacked, :standard, :stacked], v
+ @grouping = v
+ end
+
+ def gapDepth=(v)
+ RegexValidator.validate "Bar3DChart.gapWidth", GAP_AMOUNT_PERCENT, v
+ @gapDepth=(v)
+ end
+
+ # Serializes the bar chart
+ # @return [String]
+ def to_xml
+ super() do |xml|
+ xml.send('c:line3DChart') {
+ xml.send('c:grouping', :val=>grouping)
+ xml.send('c:varyColors', :val=>1)
+ @series.each { |ser| ser.to_xml(xml) }
+ xml.send('c:gapDepth', :val=>@gapDepth) unless @gapDepth.nil?
+ xml.send('c:axId', :val=>@catAxId)
+ xml.send('c:axId', :val=>@valAxId)
+ xml.send('c:axId', :val=>@serAxId)
+ }
+ @catAxis.to_xml(xml)
+ @valAxis.to_xml(xml)
+ @serAxis.to_xml(xml)
+ end
+ end
+ end
+end
diff --git a/lib/axlsx/drawing/line_series.rb b/lib/axlsx/drawing/line_series.rb
new file mode 100644
index 00000000..db509d38
--- /dev/null
+++ b/lib/axlsx/drawing/line_series.rb
@@ -0,0 +1,76 @@
+module Axlsx
+ # A LineSeries defines the title, data and labels for line charts
+ # @note The recommended way to manage series is to use Chart#add_series
+ # @see Worksheet#add_chart
+ # @see Chart#add_series
+ class LineSeries < Series
+
+ # The data for this series.
+ # @return [Array, SimpleTypedList]
+ attr_reader :data
+
+ # 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
+ # @param [Chart] chart
+ def initialize(chart, options={})
+ super(chart, options)
+ self.data = options[:data] || []
+ self.labels = options[:labels] || []
+ 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|
+ 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
diff --git a/lib/axlsx/drawing/pie_3D_chart.rb b/lib/axlsx/drawing/pie_3D_chart.rb
index b6b4ff1b..cf835bfd 100644
--- a/lib/axlsx/drawing/pie_3D_chart.rb
+++ b/lib/axlsx/drawing/pie_3D_chart.rb
@@ -39,8 +39,8 @@ module Axlsx
# @param [Workbook] workbook The workbook that owns this chart.
# @option options [Cell, String] title
def initialize(workbook, options={})
- super(workbook, options)
# this charts series type
+ super(workbook, options)
@series_type = PieSeries
@view3D = View3D.new(:rotX => 30, :perspective => 30)
end
diff --git a/lib/axlsx/drawing/ser_axis.rb b/lib/axlsx/drawing/ser_axis.rb
new file mode 100644
index 00000000..091a7d64
--- /dev/null
+++ b/lib/axlsx/drawing/ser_axis.rb
@@ -0,0 +1,42 @@
+module Axlsx
+ #A SarAxis object defines a series axis
+ class SerAxis < Axis
+
+ # @return [Boolean]
+ attr_accessor :tickLblSkip
+
+ # @return [Boolean]
+ attr_accessor :tickMarkSkip
+
+ # Creates a new SerAxis object
+ # @param [Integer] axId the id of this axis
+ # @param [Integer] crossAx the id of the perpendicular axis
+ # @option options [Symbol] axPos
+ # @option options [Symbol] tickLblPos
+ # @option options [Symbol] crosses
+ # @option options [Boolean] tickLblSkip
+ # @option options [Symbol] tickMarkSkip
+ def initialize(axId, crossAx, options={})
+ super(axId, crossAx, options)
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ def tickLblSkip=(v) Axlsx::validate_boolean(v); @tickLblSkip = v; end
+ def tickMarkSkip=(v) Axlsx::validate_boolean(v); @tickMarkSkip = v; end
+
+ # Serializes the series axis
+ # @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:serAx') {
+ super(xml)
+ xml.send('c:tickLblSkip', :val=>@tickLblSkip) unless @tickLblSkip.nil?
+ xml.send('c:tickMarkSkip', :val=>@tickMarkSkip) unless @tickMarkSkip.nil?
+ }
+ end
+ end
+
+
+end
diff --git a/lib/axlsx/drawing/series.rb b/lib/axlsx/drawing/series.rb
index ef446921..51957acc 100644
--- a/lib/axlsx/drawing/series.rb
+++ b/lib/axlsx/drawing/series.rb
@@ -18,7 +18,7 @@ module Axlsx
attr_accessor :order
# The title of the series
- # @return [String]
+ # @return [SeriesTitle]
attr_accessor :title
# Creates a new series
@@ -44,8 +44,12 @@ module Axlsx
@order || index
end
- def title=(v) Axlsx::validate_string(v); @title = v; end
-
+ def title=(v)
+ v = SeriesTitle.new(v) if v.is_a?(String) || v.is_a?(Cell)
+ DataTypeValidator.validate "#{self.class}.title", SeriesTitle, v
+ @title = v
+ end
+
private
# assigns the chart for this series
@@ -57,10 +61,8 @@ module Axlsx
def to_xml(xml)
xml.send('c:ser') {
xml.send('c:idx', :val=>index)
- xml.send('c:order', :val=>order || index)
- xml.send('c:tx') {
- xml.send('c:v', self.title)
- }
+ xml.send('c:order', :val=>order || index)
+ title.to_xml(xml) unless title.nil?
yield xml if block_given?
}
end
diff --git a/lib/axlsx/drawing/series_title.rb b/lib/axlsx/drawing/series_title.rb
new file mode 100644
index 00000000..32b5b102
--- /dev/null
+++ b/lib/axlsx/drawing/series_title.rb
@@ -0,0 +1,22 @@
+module Axlsx
+ # A series title is a Title with a slightly different serialization
+ class SeriesTitle < Title
+
+ # Serializes the series title
+ # @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:tx') {
+ xml.send('c:strRef') {
+ xml.send('c:f', range)
+ xml.send('c:strCache') {
+ xml.send('c:ptCount', :val=>1)
+ xml.send('c:pt', :idx=>0) {
+ xml.send('c:v', @text)
+ }
+ }
+ }
+ }
+ end
+ end
+end
diff --git a/lib/axlsx/drawing/title.rb b/lib/axlsx/drawing/title.rb
index 496f5f7b..bdbf0e5c 100644
--- a/lib/axlsx/drawing/title.rb
+++ b/lib/axlsx/drawing/title.rb
@@ -32,7 +32,6 @@ module Axlsx
end
# Not implemented at this time.
- #def tx=(v) DataTypeValidator.validate 'Title.tx', Tx, v; @tx=v; end
#def layout=(v) DataTypeValidator.validate 'Title.layout', Layout, v; @layout = v; end
#def overlay=(v) Axlsx::validate_boolean v; @overlay=v; end
#def spPr=(v) DataTypeValidator.validate 'Title.spPr', SpPr, v; @spPr = v; end
@@ -55,7 +54,7 @@ module Axlsx
}
}
end
-
+
private
# returns the excel style abslute reference for the title when title is a Cell object
diff --git a/test/drawing/tc_bar_series.rb b/test/drawing/tc_bar_series.rb
index 1f0800f1..afd6004a 100644
--- a/test/drawing/tc_bar_series.rb
+++ b/test/drawing/tc_bar_series.rb
@@ -11,7 +11,7 @@ class TestBarSeries < Test::Unit::TestCase
end
def test_initialize
- assert_equal(@series.title, "bob", "series title has been applied")
+ assert_equal(@series.title.text, "bob", "series title has been applied")
assert_equal(@series.data, [0,1,2], "data option applied")
assert_equal(@series.labels, ["zero", "one","two"], "labels option applied")
assert_equal(@series.shape, :box, "series shape has been applied")
diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb
index 8e26a832..586df2c5 100644
--- a/test/drawing/tc_chart.rb
+++ b/test/drawing/tc_chart.rb
@@ -28,19 +28,21 @@ class TestChart < Test::Unit::TestCase
assert_equal(@chart.title.cell, @row.cells.first)
end
+ def test_style
+ assert_raise(ArgumentError) { @chart.style = 49 }
+ assert_nothing_raised { @chart.style = 2 }
+ assert_equal(@chart.style, 2)
+ end
+
def test_add_series
s = @chart.add_series :data=>[0,1,2,3], :labels => ["one", 1, "anything"], :title=>"bob"
assert_equal(@chart.series.last, s, "series has been added to chart series collection")
- assert_equal(s.title, "bob", "series title has been applied")
+ assert_equal(s.title.text, "bob", "series title has been applied")
assert_equal(s.data, [0,1,2,3], "data option applied")
assert_equal(s.labels, ["one",1,"anything"], "labels option applied")
end
- def test_create_range
-
- end
-
def test_pn
assert_equal(@chart.pn, "charts/chart1.xml")
end
diff --git a/test/drawing/tc_line_3d_chart.rb b/test/drawing/tc_line_3d_chart.rb
new file mode 100644
index 00000000..5c4a3f26
--- /dev/null
+++ b/test/drawing/tc_line_3d_chart.rb
@@ -0,0 +1,48 @@
+require 'test/unit'
+require 'axlsx.rb'
+
+class TestLine3DChart < Test::Unit::TestCase
+
+ def setup
+ @p = Axlsx::Package.new
+ ws = @p.workbook.add_worksheet
+ @row = ws.add_row ["one", 1, Time.now]
+ @chart = ws.add_chart Axlsx::Line3DChart, :title => "fishery"
+ end
+
+ def teardown
+ end
+
+ def test_initialization
+ assert_equal(@chart.grouping, :standard, "grouping defualt incorrect")
+ assert_equal(@chart.series_type, Axlsx::LineSeries, "series type incorrect")
+ assert(@chart.catAxis.is_a?(Axlsx::CatAxis), "category axis not created")
+ assert(@chart.valAxis.is_a?(Axlsx::ValAxis), "value access not created")
+ assert(@chart.serAxis.is_a?(Axlsx::SerAxis), "value access not created")
+ end
+
+ def test_grouping
+ assert_raise(ArgumentError, "require valid grouping") { @chart.grouping = :inverted }
+ assert_nothing_raised("allow valid grouping") { @chart.grouping = :stacked }
+ assert(@chart.grouping == :stacked)
+ end
+
+ def test_gapDepth
+ assert_raise(ArgumentError, "require valid gapDepth") { @chart.gapDepth = 200 }
+ assert_nothing_raised("allow valid gapDepth") { @chart.gapDepth = "200%" }
+ assert(@chart.gapDepth == "200%")
+ end
+
+
+ def test_to_xml
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD))
+ doc = Nokogiri::XML(@chart.to_xml)
+ errors = []
+ schema.validate(doc).each do |error|
+ errors.push error
+ puts error.message
+ end
+ assert(errors.empty?, "error free validation")
+ end
+
+end
diff --git a/test/drawing/tc_line_series.tc b/test/drawing/tc_line_series.tc
new file mode 100644
index 00000000..54cbc256
--- /dev/null
+++ b/test/drawing/tc_line_series.tc
@@ -0,0 +1,34 @@
+require 'test/unit'
+require 'axlsx.rb'
+
+class TestLineSeries < Test::Unit::TestCase
+
+ def setup
+ p = Axlsx::Package.new
+ @ws = p.workbook.add_worksheet :name=>"hmmm"
+ chart = @ws.drawing.add_chart Axlsx::Line3DChart, :title => "fishery"
+ @series = chart.add_series :data=>[0,1,2], :labels=>["zero", "one", "two"], :title=>"bob"
+ end
+
+ def test_initialize
+ assert_equal(@series.title.text, "bob", "series title has been applied")
+ assert_equal(@series.data, [0,1,2], "data option applied")
+ assert_equal(@series.labels, ["zero", "one","two"], "labels option applied")
+ assert_equal(@series.shape, :box, "series shape has been applied")
+ end
+
+ def test_data
+ assert_equal(@series.data, [0,1,2])
+ end
+
+ def test_labels
+ assert_equal(@series.labels, ["zero", "one", "two"])
+ end
+
+ def test_shape
+ assert_raise(ArgumentError, "require valid shape") { @series.shape = :teardropt }
+ assert_nothing_raised("allow valid shape") { @series.shape = :cone }
+ assert(@series.shape == :cone)
+ end
+
+end
diff --git a/test/drawing/tc_pie_series.rb b/test/drawing/tc_pie_series.rb
index e7c5e83b..53799c72 100644
--- a/test/drawing/tc_pie_series.rb
+++ b/test/drawing/tc_pie_series.rb
@@ -11,7 +11,7 @@ class TestPieSeries < Test::Unit::TestCase
end
def test_initialize
- assert_equal(@series.title, "bob", "series title has been applied")
+ assert_equal(@series.title.text, "bob", "series title has been applied")
assert_equal(@series.data, [0,1,2], "data option applied")
assert_equal(@series.labels, ["zero", "one","two"], "labels option applied")
assert_equal(@series.explosion, nil, "series shape has been applied")
diff --git a/test/drawing/tc_ser_axis.rb b/test/drawing/tc_ser_axis.rb
new file mode 100644
index 00000000..1882e1f1
--- /dev/null
+++ b/test/drawing/tc_ser_axis.rb
@@ -0,0 +1,23 @@
+require 'test/unit'
+require 'axlsx.rb'
+
+class TestSerAxis < Test::Unit::TestCase
+ def setup
+ @axis = Axlsx::SerAxis.new 12345, 54321
+ end
+ def teardown
+ end
+
+ def test_tickLblSkip
+ assert_raise(ArgumentError, "requires valid tickLblSkip") { @axis.tickLblSkip = :my_eyes }
+ assert_nothing_raised("accepts valid tickLblSkip") { @axis.tickLblSkip = false }
+ assert_equal(@axis.tickLblSkip, false)
+ end
+
+ def test_tickMarkSkip
+ assert_raise(ArgumentError, "requires valid tickMarkSkip") { @axis.tickMarkSkip = :my_eyes }
+ assert_nothing_raised("accepts valid tickMarkSkip") { @axis.tickMarkSkip = false }
+ assert_equal(@axis.tickMarkSkip, false)
+ end
+
+end
diff --git a/test/drawing/tc_series.rb b/test/drawing/tc_series.rb
index b94ec308..cbbd93d4 100644
--- a/test/drawing/tc_series.rb
+++ b/test/drawing/tc_series.rb
@@ -11,7 +11,7 @@ class TestSeries < Test::Unit::TestCase
end
def test_initialize
- assert_equal(@series.title, "bob", "series title has been applied")
+ assert_equal(@series.title.text, "bob", "series title has been applied")
assert_equal(@series.order, @series.index, "order is index by default")
assert_equal(@series.index, @series.chart.series.index(@series), "index is applied")
end
diff --git a/test/drawing/tc_series_title.rb b/test/drawing/tc_series_title.rb
new file mode 100644
index 00000000..f10370b7
--- /dev/null
+++ b/test/drawing/tc_series_title.rb
@@ -0,0 +1,34 @@
+require 'test/unit'
+require 'axlsx.rb'
+
+class TestSeriesTitle < Test::Unit::TestCase
+ def setup
+ @p = Axlsx::Package.new
+ ws = @p.workbook.add_worksheet
+ @row = ws.add_row ["one", 1, Time.now]
+ @title = Axlsx::SeriesTitle.new
+ @chart = ws.add_chart Axlsx::Bar3DChart
+ end
+
+ def teardown
+ end
+
+ def test_initialization
+ assert(@title.text == "")
+ assert(@title.cell == nil)
+ end
+
+ def test_text
+ assert_raise(ArgumentError, "text must be a string") { @title.text = 123 }
+ @title.cell = @row.cells.first
+ @title.text = "bob"
+ assert(@title.cell == nil, "setting title with text clears the cell")
+ end
+
+ def test_cell
+ assert_raise(ArgumentError, "cell must be a Cell") { @title.cell = "123" }
+ @title.cell = @row.cells.first
+ assert(@title.text == "one")
+ end
+
+end
diff --git a/test/tc_package.rb b/test/tc_package.rb
index 32aabe9d..65cdc3ac 100644
--- a/test/tc_package.rb
+++ b/test/tc_package.rb
@@ -16,19 +16,21 @@ class TestPackage < Test::Unit::TestCase
assert(Axlsx::Package.new.workbook.worksheets.size == 0, 'Workbook should not have sheets by default')
end
- def test_serialization
- fname = 'test_serialization.xlsx'
- assert_nothing_raised do
- if File.writable?(fname)
- z= @package.serialize(fname)
- zf = Zip::ZipFile.open(fname)
- @package.send(:parts).each{ |part| zf.get_entry(part[:entry]) }
- File.delete(fname)
- else
- puts "Skipping write to disk as write permission is not granted to this user"
- end
- end
- end
+ # TODO this test needs better file access validation!
+ # as does serialization!
+ # def test_serialization
+ # fname = 'test_serialization.xlsx'
+ # assert_nothing_raised do
+ # if File::writable?(fname)
+ # z= @package.serialize(fname)
+ # zf = Zip::ZipFile.open(fname)
+ # @package.send(:parts).each{ |part| zf.get_entry(part[:entry]) }
+ # File.delete(fname)
+ # else
+ # puts "Skipping write to disk as write permission is not granted to this user"
+ # end
+ # end
+ # end
def test_validation
assert_equal(@package.validate.size, 0, @package.validate)