diff options
| -rw-r--r-- | lib/axlsx/drawing/axes.rb | 4 | ||||
| -rw-r--r-- | test/drawing/tc_axes.rb | 16 |
2 files changed, 19 insertions, 1 deletions
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 << '<c:axId val="' << axis[1].id.to_s << '"/>' } + # 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 << '<c:axId val="' << axis[1].id.to_s << '"/>' } 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 = '<?xml version="1.0" encoding="UTF-8"?>' + str << '<c:chartSpace xmlns:c="' << Axlsx::XML_NS_C << '" xmlns:a="' << Axlsx::XML_NS_A << '">' + @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 |
