From 173f1c49fb449aebc5ddf30a9387006ee54e5260 Mon Sep 17 00:00:00 2001 From: Moses Hohman Date: Sun, 23 Jun 2013 01:06:26 -0500 Subject: Fixes Issue #202 Axes are borked in Bar3DChart --- lib/axlsx/drawing/axes.rb | 4 +++- test/drawing/tc_axes.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/drawing/tc_axes.rb diff --git a/lib/axlsx/drawing/axes.rb b/lib/axlsx/drawing/axes.rb index c3a8dd85..fc440b1a 100644 --- a/lib/axlsx/drawing/axes.rb +++ b/lib/axlsx/drawing/axes.rb @@ -28,7 +28,9 @@ module Axlsx # serialized. Otherwise, each axis is serialized in full. def to_xml_string(str = '', options = {}) if options[:ids] - axes.inject(str) { |string, axis| string << '' } + # CatAxis must come first in the XML (for Microsoft Excel at least) + sorted = axes.sort_by { |name, axis| axis.kind_of?(CatAxis) ? 0 : 1 } + sorted.inject(str) { |string, axis| string << '' } else axes.each { |axis| axis[1].to_xml_string(str) } end diff --git a/test/drawing/tc_axes.rb b/test/drawing/tc_axes.rb new file mode 100644 index 00000000..caa242ad --- /dev/null +++ b/test/drawing/tc_axes.rb @@ -0,0 +1,16 @@ +require 'tc_helper.rb' + +class TestAxes < Test::Unit::TestCase + def setup + @axes = Axlsx::Axes.new(:val_axis => Axlsx::ValAxis, :cat_axis => Axlsx::CatAxis) + end + + def test_to_xml_string_just_ids + str = '' + str << '' + @axes.to_xml_string(str, :ids => true) + cat_axis_position = str.index(@axes[:cat_axis].id.to_s) + val_axis_position = str.index(@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 +end \ No newline at end of file -- cgit v1.2.3 From 15e78a467348d744ee58039b484adb8083290fb9 Mon Sep 17 00:00:00 2001 From: Moses Hohman Date: Sun, 23 Jun 2013 02:02:01 -0500 Subject: provide a better default for dispBlanksAs and allow it to be configured --- lib/axlsx/drawing/chart.rb | 17 +++++++++++++++++ lib/axlsx/util/validators.rb | 7 +++++++ test/drawing/tc_chart.rb | 14 ++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb index 79019a0a..5f7db77f 100644 --- a/lib/axlsx/drawing/chart.rb +++ b/lib/axlsx/drawing/chart.rb @@ -20,6 +20,7 @@ module Axlsx @graphic_frame.anchor.drawing.worksheet.workbook.charts << self @series = SimpleTypedList.new Series @show_legend = true + @display_blanks_as = :gap @series_type = Series @title = Title.new parse_options options @@ -70,6 +71,15 @@ module Axlsx # @return [Boolean] attr_reader :show_legend + # How to display blank values + # Options are + # * gap: Display nothing + # * span: Not sure what this does + # * zero: Display as if the value were zero, not blank + # @return [Symbol] + # Default :gap (although this really should vary by chart type and grouping) + attr_reader :display_blanks_as + # returns a relationship object for the chart # @return [Axlsx::Relationship] def relationship @@ -105,6 +115,12 @@ module Axlsx # @return [Boolean] def show_legend=(v) Axlsx::validate_boolean(v); @show_legend = v; end + # How to display blank values + # @see display_blanks_as + # @param [Symbol] v + # @return [Symbol] + def display_blanks_as=(v) Axlsx::validate_display_blanks_as(v); @display_blanks_as = v; end + # The style for the chart. # see ECMA Part 1 ยง21.2.2.196 # @param [Integer] v must be between 1 and 48 @@ -158,6 +174,7 @@ module Axlsx end str << '' str << '' + # str << '' str << '' str << '' str << '' diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb index 739a4de2..a161f0d9 100644 --- a/lib/axlsx/util/validators.rb +++ b/lib/axlsx/util/validators.rb @@ -290,4 +290,11 @@ module Axlsx def self.validate_split_state_type(v) RestrictionValidator.validate :split_state_type, [:frozen, :frozen_split, :split], v end + + # Requires that the value is a valid "display blanks as" type. + # valid types must be one of gap, span, zero + # @param [Any] v The value validated + def self.validate_display_blanks_as(v) + RestrictionValidator.validate :display_blanks_as, [:gap, :span, :zero], v + end end diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb index 03e4fd6f..50cb5d0c 100644 --- a/test/drawing/tc_chart.rb +++ b/test/drawing/tc_chart.rb @@ -45,6 +45,14 @@ class TestChart < Test::Unit::TestCase assert_equal(false, @chart.vary_colors) end + def test_display_blanks_as + assert_equal(:gap, @chart.display_blanks_as, "default is not :gap") + assert_raise(ArgumentError, "did not validate possible values") { @chart.display_blanks_as = :hole } + assert_nothing_raised { @chart.display_blanks_as = :zero } + assert_nothing_raised { @chart.display_blanks_as = :span } + assert_equal(:span, @chart.display_blanks_as) + end + def test_start_at @chart.start_at 15, 25 assert_equal(@chart.graphic_frame.anchor.from.col, 15) @@ -94,4 +102,10 @@ class TestChart < Test::Unit::TestCase assert(errors.empty?, "error free validation") end + def test_to_xml_string_for_display_blanks_as + schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) + @chart.display_blanks_as = :span + doc = Nokogiri::XML(@chart.to_xml_string) + assert(doc.xpath("//c:dispBlanksAs[@val='span']")) + end end -- cgit v1.2.3 From c560d142d36c211154f03f271747f8756e75fc63 Mon Sep 17 00:00:00 2001 From: Moses Hohman Date: Sun, 23 Jun 2013 02:17:19 -0500 Subject: fixed bad test case and bad code for dispBlanksAs (setting now actually makes it into XML) This means there are many other test cases in the suite that are not verifying what they look like they're verifying. --- lib/axlsx/drawing/chart.rb | 3 +-- test/drawing/tc_chart.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb index 5f7db77f..1da3a253 100644 --- a/lib/axlsx/drawing/chart.rb +++ b/lib/axlsx/drawing/chart.rb @@ -173,8 +173,7 @@ module Axlsx str << '' end str << '' - str << '' - # str << '' + str << '' str << '' str << '' str << '' diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb index 50cb5d0c..751d2ae4 100644 --- a/test/drawing/tc_chart.rb +++ b/test/drawing/tc_chart.rb @@ -106,6 +106,6 @@ class TestChart < Test::Unit::TestCase schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) @chart.display_blanks_as = :span doc = Nokogiri::XML(@chart.to_xml_string) - assert(doc.xpath("//c:dispBlanksAs[@val='span']")) + assert_equal("span", doc.xpath("//c:dispBlanksAs").attr("val").value, "did not use the display_blanks_as configuration") end end -- cgit v1.2.3 From c254678321c3ae620aa8a50161331d3f3c218f78 Mon Sep 17 00:00:00 2001 From: Moses Hohman Date: Thu, 27 Jun 2013 15:36:34 -0500 Subject: Fixes Issue #202 Axes are borked in Bar3DChart by requiring axis order in the constructor --- lib/axlsx/drawing/axes.rb | 6 ++++-- lib/axlsx/drawing/bar_3D_chart.rb | 4 ++-- test/drawing/tc_axes.rb | 14 +++----------- test/drawing/tc_bar_3D_chart.rb | 6 ++++++ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/axlsx/drawing/axes.rb b/lib/axlsx/drawing/axes.rb index fc440b1a..bc40e532 100644 --- a/lib/axlsx/drawing/axes.rb +++ b/lib/axlsx/drawing/axes.rb @@ -5,9 +5,11 @@ module Axlsx class Axes # @param [Hash] options options used to generate axis each key - # should be an axis name like :val_axix and its value should be the - # class of the axis type to construct. + # should be an axis name like :val_axis and its value should be the + # class of the axis type to construct. The :cat_axis, if there is one, + # must come first (we assume a Ruby 1.9+ Hash or an OrderedHash). def initialize(options={}) + raise(ArgumentError, "CatAxis must come first") if options.keys.include?(:cat_axis) && options.keys.first != :cat_axis options.each do |name, axis_class| add_axis(name, axis_class) end diff --git a/lib/axlsx/drawing/bar_3D_chart.rb b/lib/axlsx/drawing/bar_3D_chart.rb index 3315ad80..755f334c 100644 --- a/lib/axlsx/drawing/bar_3D_chart.rb +++ b/lib/axlsx/drawing/bar_3D_chart.rb @@ -142,10 +142,10 @@ module Axlsx end # A hash of axes used by this chart. Bar charts have a value and - # category axes specified via axex[:val_axes] and axes[:cat_axis] + # category axes specified via axes[:val_axes] and axes[:cat_axis] # @return [Axes] def axes - @axes ||= Axes.new(:val_axis => ValAxis, :cat_axis => CatAxis) + @axes ||= Axes.new(:cat_axis => CatAxis, :val_axis => ValAxis) end end end diff --git a/test/drawing/tc_axes.rb b/test/drawing/tc_axes.rb index caa242ad..e3c26936 100644 --- a/test/drawing/tc_axes.rb +++ b/test/drawing/tc_axes.rb @@ -1,16 +1,8 @@ require 'tc_helper.rb' class TestAxes < Test::Unit::TestCase - def setup - @axes = Axlsx::Axes.new(:val_axis => Axlsx::ValAxis, :cat_axis => Axlsx::CatAxis) - end - - def test_to_xml_string_just_ids - str = '' - str << '' - @axes.to_xml_string(str, :ids => true) - cat_axis_position = str.index(@axes[:cat_axis].id.to_s) - val_axis_position = str.index(@axes[:val_axis].id.to_s) - assert(cat_axis_position < val_axis_position, "cat_axis must occur earlier than val_axis in the XML") + def test_constructor_requires_cat_axis_first + assert_raise(ArgumentError) { Axlsx::Axes.new(:val_axis => Axlsx::ValAxis, :cat_axis => Axlsx::CatAxis) } + assert_nothing_raised { Axlsx::Axes.new(:cat_axis => Axlsx::CatAxis, :val_axis => Axlsx::ValAxis) } end end \ No newline at end of file diff --git a/test/drawing/tc_bar_3D_chart.rb b/test/drawing/tc_bar_3D_chart.rb index 3e1ff342..0cae7af6 100644 --- a/test/drawing/tc_bar_3D_chart.rb +++ b/test/drawing/tc_bar_3D_chart.rb @@ -62,4 +62,10 @@ class TestBar3DChart < Test::Unit::TestCase assert(errors.empty?, "error free validation") end + def test_to_xml_string_has_axes_in_correct_order + str = @chart.to_xml_string + cat_axis_position = str.index(@chart.axes[:cat_axis].id.to_s) + 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 end -- cgit v1.2.3