summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStefan <[email protected]>2022-05-23 12:52:53 +0200
committerStefan Daschek <[email protected]>2022-06-01 09:55:27 +0200
commit35ed793586a57837b2e0319e57b5c763f9f4ade4 (patch)
tree46fe4d8701c12e5a66f7fdf40aac7e504d9411bf
parent2a5c58faffb690831911f3799e8c097c6fdef793 (diff)
downloadcaxlsx-35ed793586a57837b2e0319e57b5c763f9f4ade4.tar.gz
caxlsx-35ed793586a57837b2e0319e57b5c763f9f4ade4.zip
Implement “plot visible only” setting for charts
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.
-rw-r--r--lib/axlsx/drawing/chart.rb13
-rw-r--r--test/drawing/tc_chart.rb15
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 << '<c:overlay val="0"/>'
str << '</c:legend>'
end
- str << '<c:plotVisOnly val="1"/>'
+ str << ('<c:plotVisOnly val="' << @plot_visible_only.to_s << '"/>')
str << ('<c:dispBlanksAs val="' << display_blanks_as.to_s << '"/>')
str << '<c:showDLblsOverMax val="1"/>'
str << '</c:chart>'
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