Skip to content

Commit

Permalink
Add chart option to display N/A as empty cells.
Browse files Browse the repository at this point in the history
Feature request #1008
  • Loading branch information
jmcnamara committed Sep 17, 2023
1 parent 4b6f231 commit 3e8435f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
13 changes: 13 additions & 0 deletions dev/docs/source/chart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,19 @@ The available options are::
'span' # Blank data is connected with a line.


chart.show_na_as_empty_cell()
------------------------

.. py:function:: show_na_as_empty_cell()
Display ``#N/A`` on charts as blank/empty cells.


Display ``#N/A`` values on a chart as blank/empty cells.::

chart.show_na_as_empty_cell()


chart.show_hidden_data()
------------------------

Expand Down
48 changes: 43 additions & 5 deletions xlsxwriter/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def __init__(self, options=None):
self.x2_axis = {}
self.chart_name = ""
self.show_blanks = "gap"
self.show_hidden = 0
self.show_na_as_empty = False
self.show_hidden = False
self.show_crosses = 1
self.width = 480
self.height = 288
Expand Down Expand Up @@ -427,17 +428,29 @@ def show_blanks_as(self, option):

self.show_blanks = option

def show_na_as_empty_cell(self):
"""
Display ``#N/A`` on charts as blank/empty cells.
Args:
None.
Returns:
Nothing.
"""
self.show_na_as_empty = True

def show_hidden_data(self):
"""
Display data on charts from hidden rows or columns.
Args:
option: A string representing the display option.
None.
Returns:
Nothing.
"""
self.show_hidden = 1
self.show_hidden = True

def set_size(self, options=None):
"""
Expand Down Expand Up @@ -1638,6 +1651,10 @@ def _write_chart(self):
# Write the c:dispBlanksAs element.
self._write_disp_blanks_as()

# Write the c:extLst element.
if self.show_na_as_empty:
self._write_c_ext_lst_display_na()

self._xml_end_tag("c:chart")

def _write_disp_blanks_as(self):
Expand Down Expand Up @@ -1821,11 +1838,11 @@ def _write_ser(self, series):

# Write the c:extLst element.
if series.get("inverted_color"):
self._write_c_ext_lst(series["inverted_color"])
self._write_c_ext_lst_inverted_color(series["inverted_color"])

self._xml_end_tag("c:ser")

def _write_c_ext_lst(self, color):
def _write_c_ext_lst_inverted_color(self, color):
# Write the <c:extLst> element for the inverted fill color.

uri = "{6F2FDCE9-48DA-4B69-8628-5D25D57E5C99}"
Expand All @@ -1850,6 +1867,27 @@ def _write_c_ext_lst(self, color):
self._xml_end_tag("c:ext")
self._xml_end_tag("c:extLst")

def _write_c_ext_lst_display_na(self):
# Write the <c:extLst> element for the display NA as empty cell option.

uri = "{56B9EC1D-385E-4148-901F-78D8002777C0}"
xmlns_c_16 = "http://schemas.microsoft.com/office/drawing/2017/03/chart"

attributes1 = [
("uri", uri),
("xmlns:c16r3", xmlns_c_16),
]

attributes2 = [("val", 1)]

self._xml_start_tag("c:extLst")
self._xml_start_tag("c:ext", attributes1)
self._xml_start_tag("c16r3:dataDisplayOptions16")
self._xml_empty_tag("c16r3:dispNaAsBlank", attributes2)
self._xml_end_tag("c16r3:dataDisplayOptions16")
self._xml_end_tag("c:ext")
self._xml_end_tag("c:extLst")

def _write_idx(self, val):
# Write the <c:idx> element.

Expand Down
52 changes: 52 additions & 0 deletions xlsxwriter/test/comparison/test_chart_blank07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c), 2013-2023, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparison_test import ExcelComparisonTest
from ...workbook import Workbook


class TestCompareXLSXFiles(ExcelComparisonTest):
"""
Test file created by XlsxWriter against a file created by Excel.
"""

def setUp(self):
self.set_filename("chart_blank07.xlsx")

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()
chart = workbook.add_chart({"type": "line"})

chart.axis_ids = [45705856, 45843584]

data = [
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15],
]

worksheet.write_column("A1", data[0])
worksheet.write_column("B1", data[1])
worksheet.write_column("C1", data[2])

chart.add_series({"values": "=Sheet1!$A$1:$A$5"})
chart.add_series({"values": "=Sheet1!$B$1:$B$5"})
chart.add_series({"values": "=Sheet1!$C$1:$C$5"})

chart.show_na_as_empty_cell()

worksheet.insert_chart("E9", chart)

workbook.close()

self.assertExcelEqual()
Binary file not shown.

0 comments on commit 3e8435f

Please sign in to comment.