From 3c8831827fc789cda2fe5333910710f4d75547d5 Mon Sep 17 00:00:00 2001 From: Stefan Date: Sun, 15 May 2022 19:16:55 +0200 Subject: Completely hide chart titles if blank MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change, a chart with a blank title would show up in Excel with a default title like “Chart title” or “Diagrammtitel”. It was not possible to completely hide a chart title: Setting it to an empty space would override Excel’s default, but the empty title did still occupy vertical space. Per OOXML standard the `title` element is optional, so omitting it if the title is blank looks like the right thing to do and seems to work correctly in Excel. --- lib/axlsx/drawing/chart.rb | 2 +- test/drawing/tc_chart.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb index d3a7e9ff..99b62b64 100644 --- a/lib/axlsx/drawing/chart.rb +++ b/lib/axlsx/drawing/chart.rb @@ -189,7 +189,7 @@ module Axlsx str << ('') str << ('') str << '' - @title.to_xml_string str + @title.to_xml_string(str) unless @title.empty? str << ('') @view_3D.to_xml_string(str) if @view_3D str << '' diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb index 302d13cd..58622621 100644 --- a/test/drawing/tc_chart.rb +++ b/test/drawing/tc_chart.rb @@ -1,3 +1,5 @@ +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../" + require 'tc_helper.rb' class TestChart < Test::Unit::TestCase @@ -25,6 +27,8 @@ class TestChart < Test::Unit::TestCase @chart.title = @row.cells.first assert_equal(@chart.title.text, "one", "the title text was set via cell reference") assert_equal(@chart.title.cell, @row.cells.first) + @chart.title = "" + assert(@chart.title.empty?) end def test_style @@ -121,4 +125,14 @@ class TestChart < Test::Unit::TestCase 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 + + def test_to_xml_string_for_title + @chart.title = "foobar" + doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal("foobar", doc.xpath("//c:title//c:tx//a:t").text) + + @chart.title = "" + doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal(0, doc.xpath("//c:title").size) + end end -- cgit v1.2.3