From 35ed793586a57837b2e0319e57b5c763f9f4ade4 Mon Sep 17 00:00:00 2001 From: Stefan Date: Mon, 23 May 2022 12:52:53 +0200 Subject: Implement “plot visible only” setting for charts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Until now this setting was hardcoded to `true`. The setting affects whether data from hidden cells (cells width zero height or width) is used when plotting the chart. --- lib/axlsx/drawing/chart.rb | 13 ++++++++++++- test/drawing/tc_chart.rb | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb index 99b62b64..723c79d8 100644 --- a/lib/axlsx/drawing/chart.rb +++ b/lib/axlsx/drawing/chart.rb @@ -14,6 +14,7 @@ module Axlsx # @option options [Symbol] legend_position # @option options [Array|String|Cell] start_at The X, Y coordinates defining the top left corner of the chart. # @option options [Array|String|Cell] end_at The X, Y coordinates defining the bottom right corner of the chart. + # @option options [Boolean] plot_visible_only (true) Whether only data from visible cells should be plotted. def initialize(frame, options={}) @style = 18 @view_3D = nil @@ -26,6 +27,7 @@ module Axlsx @series_type = Series @title = Title.new @bg_color = nil + @plot_visible_only = true parse_options options start_at(*options[:start_at]) if options[:start_at] end_at(*options[:end_at]) if options[:end_at] @@ -98,6 +100,10 @@ module Axlsx # @return [String] attr_reader :bg_color + # Whether only data from visible cells should be plotted. + # @return [Boolean] + attr_reader :plot_visible_only + # The relationship object for this chart. # @return [Relationship] def relationship @@ -180,6 +186,11 @@ module Axlsx @bg_color = v end + # Whether only data from visible cells should be plotted. + # @param [Boolean] v + # @return [Boolean] + def plot_visible_only=(v) Axlsx::validate_boolean(v); @plot_visible_only = v; end + # Serializes the object # @param [String] str # @return [String] @@ -206,7 +217,7 @@ module Axlsx str << '' str << '' end - str << '' + str << ('') str << ('') str << '' str << '' diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb index 58622621..c3392f26 100644 --- a/test/drawing/tc_chart.rb +++ b/test/drawing/tc_chart.rb @@ -107,12 +107,19 @@ class TestChart < Test::Unit::TestCase end def test_d_lbls - + assert_equal(nil, Axlsx.instance_values_for(@chart)[:d_lbls]) @chart.d_lbls.d_lbl_pos = :t assert(@chart.d_lbls.is_a?(Axlsx::DLbls), 'DLbls instantiated on access') end + def test_plot_visible_only + assert(@chart.plot_visible_only, "default should be true") + @chart.plot_visible_only = false + assert_false(@chart.plot_visible_only) + assert_raise(ArgumentError) { @chart.plot_visible_only = "" } + end + def test_to_xml_string schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) doc = Nokogiri::XML(@chart.to_xml_string) @@ -135,4 +142,10 @@ class TestChart < Test::Unit::TestCase doc = Nokogiri::XML(@chart.to_xml_string) assert_equal(0, doc.xpath("//c:title").size) end + + def test_to_xml_string_for_plot_visible_only + assert_equal("true", Nokogiri::XML(@chart.to_xml_string).xpath("//c:plotVisOnly").attr("val").value) + @chart.plot_visible_only = false + assert_equal("false", Nokogiri::XML(@chart.to_xml_string).xpath("//c:plotVisOnly").attr("val").value) + end end -- cgit v1.2.3