diff options
| author | Randy Morgan (@morgan_randy) <[email protected]> | 2013-06-27 17:18:57 -0700 |
|---|---|---|
| committer | Randy Morgan (@morgan_randy) <[email protected]> | 2013-06-27 17:18:57 -0700 |
| commit | 0a85cc627ea40b85e35b497a511ded50e6ef9419 (patch) | |
| tree | 8f6bbdff5b44918186afbeaf122bd560b60cf5a6 | |
| parent | 4954543cc0892008f580f05cfb810fb0986b107f (diff) | |
| parent | c254678321c3ae620aa8a50161331d3f3c218f78 (diff) | |
| download | caxlsx-0a85cc627ea40b85e35b497a511ded50e6ef9419.tar.gz caxlsx-0a85cc627ea40b85e35b497a511ded50e6ef9419.zip | |
Merge pull request #203 from moses/master
Fixes #202 Axes are borked in Bar3DChart
| -rw-r--r-- | lib/axlsx/drawing/axes.rb | 10 | ||||
| -rw-r--r-- | lib/axlsx/drawing/bar_3D_chart.rb | 4 | ||||
| -rw-r--r-- | lib/axlsx/drawing/chart.rb | 18 | ||||
| -rw-r--r-- | lib/axlsx/util/validators.rb | 7 | ||||
| -rw-r--r-- | test/drawing/tc_axes.rb | 8 | ||||
| -rw-r--r-- | test/drawing/tc_bar_3D_chart.rb | 6 | ||||
| -rw-r--r-- | test/drawing/tc_chart.rb | 14 |
7 files changed, 61 insertions, 6 deletions
diff --git a/lib/axlsx/drawing/axes.rb b/lib/axlsx/drawing/axes.rb index c3a8dd85..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 @@ -28,7 +30,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/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/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb index 79019a0a..1da3a253 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 @@ -157,7 +173,7 @@ module Axlsx str << '</c:legend>' end str << '<c:plotVisOnly val="1"/>' - str << '<c:dispBlanksAs val="zero"/>' + str << '<c:dispBlanksAs val="' << display_blanks_as.to_s << '"/>' str << '<c:showDLblsOverMax val="1"/>' str << '</c:chart>' str << '<c:printSettings>' 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_axes.rb b/test/drawing/tc_axes.rb new file mode 100644 index 00000000..e3c26936 --- /dev/null +++ b/test/drawing/tc_axes.rb @@ -0,0 +1,8 @@ +require 'tc_helper.rb' + +class TestAxes < Test::Unit::TestCase + 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 diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb index 03e4fd6f..751d2ae4 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_equal("span", doc.xpath("//c:dispBlanksAs").attr("val").value, "did not use the display_blanks_as configuration") + end end |
