diff options
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | lib/axlsx/drawing/bar_3D_chart.rb | 13 | ||||
| -rw-r--r-- | lib/axlsx/drawing/bar_chart.rb | 23 | ||||
| -rw-r--r-- | test/drawing/tc_bar_3D_chart.rb | 37 | ||||
| -rw-r--r-- | test/drawing/tc_bar_chart.rb | 25 |
5 files changed, 48 insertions, 51 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d9c3fc45..3558c392 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ CHANGELOG - **Unreleased** - [PR #107](https://github.com/caxlsx/caxlsx/pull/107) - Add overlap to bar charts + - [PR #108](https://github.com/caxlsx/caxlsx/pull/108) - Fix gap depth and gap depth validators for bar charts and 3D bar charts - **March.27.21**: 3.1.0 - [PR #95](https://github.com/caxlsx/caxlsx/pull/95) - Replace mimemagic with marcel diff --git a/lib/axlsx/drawing/bar_3D_chart.rb b/lib/axlsx/drawing/bar_3D_chart.rb index 7d44eca6..ae6c2007 100644 --- a/lib/axlsx/drawing/bar_3D_chart.rb +++ b/lib/axlsx/drawing/bar_3D_chart.rb @@ -31,17 +31,17 @@ module Axlsx alias :barDir :bar_dir # space between bar or column clusters, as a percentage of the bar or column width. - # @return [String] + # @return [Integer] attr_reader :gap_depth alias :gapDepth :gap_depth # space between bar or column clusters, as a percentage of the bar or column width. - # @return [String] + # @return [Integer] def gap_width @gap_width ||= 150 end alias :gapWidth :gap_width - + #grouping for a column, line, or area chart. # must be one of [:percentStacked, :clustered, :standard, :stacked] # @return [Symbol] @@ -56,9 +56,6 @@ module Axlsx @shape ||= :box end - # 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 bar chart object # @param [GraphicFrame] frame The workbook that owns this chart. # @option options [Cell, String] title @@ -102,14 +99,14 @@ module Axlsx # space between bar or column clusters, as a percentage of the bar or column width. def gap_width=(v) - RegexValidator.validate "Bar3DChart.gap_width", GAP_AMOUNT_PERCENT, v + RangeValidator.validate "Bar3DChart.gap_width", 0, 500, v @gap_width=(v) end alias :gapWidth= :gap_width= # space between bar or column clusters, as a percentage of the bar or column width. def gap_depth=(v) - RegexValidator.validate "Bar3DChart.gap_didth", GAP_AMOUNT_PERCENT, v + RangeValidator.validate "Bar3DChart.gap_depth", 0, 500, v @gap_depth=(v) end alias :gapDepth= :gap_depth= diff --git a/lib/axlsx/drawing/bar_chart.rb b/lib/axlsx/drawing/bar_chart.rb index cb596fce..67787361 100644 --- a/lib/axlsx/drawing/bar_chart.rb +++ b/lib/axlsx/drawing/bar_chart.rb @@ -31,12 +31,7 @@ module Axlsx alias :barDir :bar_dir # space between bar or column clusters, as a percentage of the bar or column width. - # @return [String] - attr_reader :gap_depth - alias :gapDepth :gap_depth - - # space between bar or column clusters, as a percentage of the bar or column width. - # @return [String] + # @return [Integer] def gap_width @gap_width ||= 150 end @@ -62,9 +57,6 @@ module Axlsx @shape ||= :box end - # 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 bar chart object # @param [GraphicFrame] frame The workbook that owns this chart. # @option options [Cell, String] title @@ -72,12 +64,11 @@ module Axlsx # @option options [Symbol] bar_dir # @option options [Symbol] grouping # @option options [String] gap_width - # @option options [String] gap_depth # @option options [Symbol] shape # @see Chart def initialize(frame, options={}) @vary_colors = true - @gap_width, @gap_depth, @overlap, @shape = nil, nil, nil, nil + @gap_width, @overlap, @shape = nil, nil, nil super(frame, options) @series_type = BarSeries @d_lbls = nil @@ -100,18 +91,11 @@ module Axlsx # space between bar or column clusters, as a percentage of the bar or column width. def gap_width=(v) - RegexValidator.validate "BarChart.gap_width", GAP_AMOUNT_PERCENT, v + RangeValidator.validate "BarChart.gap_width", 0, 500, v @gap_width=(v) end alias :gapWidth= :gap_width= - # space between bar or column clusters, as a percentage of the bar or column width. - def gap_depth=(v) - RegexValidator.validate "BarChart.gap_didth", GAP_AMOUNT_PERCENT, v - @gap_depth=(v) - end - alias :gapDepth= :gap_depth= - def overlap=(v) RangeValidator.validate "BarChart.overlap", -100, 100, v @overlap=(v) @@ -137,7 +121,6 @@ module Axlsx @d_lbls.to_xml_string(str) if @d_lbls str << ('<c:overlap val="' << @overlap.to_s << '"/>') unless @overlap.nil? str << ('<c:gapWidth val="' << @gap_width.to_s << '"/>') unless @gap_width.nil? - str << ('<c:gapDepth val="' << @gap_depth.to_s << '"/>') unless @gap_depth.nil? str << ('<c:shape val="' << @shape.to_s << '"/>') unless @shape.nil? axes.to_xml_string(str, :ids => true) str << '</c:barChart>' diff --git a/test/drawing/tc_bar_3D_chart.rb b/test/drawing/tc_bar_3D_chart.rb index 0cae7af6..b7a1eca4 100644 --- a/test/drawing/tc_bar_3D_chart.rb +++ b/test/drawing/tc_bar_3D_chart.rb @@ -32,18 +32,19 @@ class TestBar3DChart < Test::Unit::TestCase assert(@chart.grouping == :standard) end + def test_gap_width + assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = -1 } + assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = 501 } + assert_nothing_raised("allow valid gapWidth") { @chart.gap_width = 200 } + assert_equal(@chart.gap_width, 200, 'gap width is incorrect') + end - def test_gapWidth - assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = 200 } - assert_nothing_raised("allow valid gapWidth") { @chart.gap_width = "200%" } - assert(@chart.gap_width == "200%") - end - - def test_gapDepth - assert_raise(ArgumentError, "require valid gap_depth") { @chart.gap_depth = 200 } - assert_nothing_raised("allow valid gap_depth") { @chart.gap_depth = "200%" } - assert(@chart.gap_depth == "200%") - end + def test_gap_depth + assert_raise(ArgumentError, "require valid gap_depth") { @chart.gap_depth = -1 } + assert_raise(ArgumentError, "require valid gap_depth") { @chart.gap_depth = 501 } + assert_nothing_raised("allow valid gap_depth") { @chart.gap_depth = 200 } + assert_equal(@chart.gap_depth, 200, 'gap depth is incorrect') + end def test_shape assert_raise(ArgumentError, "require valid shape") { @chart.shape = :star } @@ -68,4 +69,18 @@ class TestBar3DChart < Test::Unit::TestCase val_axis_position = str.index(@chart.axes[:val_axis].id.to_s) assert(cat_axis_position < val_axis_position, "cat_axis must occur earlier than val_axis in the XML") end + + def test_to_xml_string_has_gap_depth + gap_depth_value = rand(0..500) + @chart.gap_depth = gap_depth_value + doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal(doc.xpath("//c:bar3DChart/c:gapDepth").first.attribute('val').value, gap_depth_value.to_s) + end + + def test_to_xml_string_has_gap_width + gap_width_value = rand(0..500) + @chart.gap_width = gap_width_value + doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal(doc.xpath("//c:bar3DChart/c:gapWidth").first.attribute('val').value, gap_width_value.to_s) + end end diff --git a/test/drawing/tc_bar_chart.rb b/test/drawing/tc_bar_chart.rb index d064e5be..c8bdc8d0 100644 --- a/test/drawing/tc_bar_chart.rb +++ b/test/drawing/tc_bar_chart.rb @@ -32,18 +32,12 @@ class TestBarChart < Test::Unit::TestCase assert(@chart.grouping == :standard) end - - def test_gapWidth - assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = 200 } - assert_nothing_raised("allow valid gapWidth") { @chart.gap_width = "200%" } - assert(@chart.gap_width == "200%") - end - - def test_gapDepth - assert_raise(ArgumentError, "require valid gap_depth") { @chart.gap_depth = 200 } - assert_nothing_raised("allow valid gap_depth") { @chart.gap_depth = "200%" } - assert(@chart.gap_depth == "200%") - end + def test_gap_width + assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = -1 } + assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = 501 } + assert_nothing_raised("allow valid gap width") { @chart.gap_width = 200 } + assert_equal(@chart.gap_width, 200, 'gap width is incorrect') + end def test_overlap assert_raise(ArgumentError, "require valid overlap") { @chart.overlap = -101 } @@ -76,6 +70,13 @@ class TestBarChart < Test::Unit::TestCase assert(cat_axis_position < val_axis_position, "cat_axis must occur earlier than val_axis in the XML") end + def test_to_xml_string_has_gap_width + gap_width_value = rand(0..500) + @chart.gap_width = gap_width_value + doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal(doc.xpath("//c:barChart/c:gapWidth").first.attribute('val').value, gap_width_value.to_s) + end + def test_to_xml_string_has_overlap overlap_value = rand(-100..100) @chart.overlap = overlap_value |
