Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/pptx/chart/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ def _iter_flattened_categories(self):
levels = self.levels
if not levels:
return
leaf_level, remaining_levels = levels[0], levels[1:]
for category in leaf_level:
remaining_levels = levels[1:]
# Match each parent to its leaf slots, including the empty ones. Without
# a parent level or a point count, use the explicit leaf points.
leaves = self if remaining_levels and len(self) else levels[0]
for category in leaves:
yield self._parentage((category,), remaining_levels)

def _parentage(self, categories, levels):
Expand Down
5 changes: 2 additions & 3 deletions src/pptx/oxml/chart/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ def cat_pts(self):
Only those in the first ``<c:lvl>`` element are included in the case
of multi-level categories.
"""
cat_pts = self.xpath("./c:ser[1]/c:cat//c:lvl[1]/c:pt")
if not cat_pts:
cat_pts = self.xpath("./c:ser[1]/c:cat//c:pt")
leaf_levels = self.xpath("./c:ser[1]/c:cat//c:lvl[1]")
cat_pts = leaf_levels[0].pt_lst if leaf_levels else self.xpath("./c:ser[1]/c:cat//c:pt")

cat_pt_dict = dict((pt.idx, pt) for pt in cat_pts)

Expand Down
30 changes: 30 additions & 0 deletions tests/chart/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@ def it_provides_a_flattened_representation(self, flat_fixture):
flattened_labels = categories.flattened_labels
assert flattened_labels == expected_values

@pytest.mark.parametrize(
("missing", "expected_labels"),
[
((1, 2), ("SF", "", "", "Albany")),
((0, 1, 2, 3), ("", "", "", "")),
],
)
def it_preserves_blank_leaf_labels_in_hierarchical_categories(self, missing, expected_labels):
chart = parse_xml(snippet_seq("cat-labels")[1])
leaf_level = chart.cat.lvls[0]
for point in leaf_level.pt_lst:
if point.idx in missing:
leaf_level.remove(point)

categories = Categories(chart)
assert tuple(categories) == expected_labels
assert categories.flattened_labels == tuple(zip(("CA", "CA", "NY", "NY"), expected_labels))

def it_flattens_explicit_categories_when_the_cache_omits_the_count(self):
chart = parse_xml(snippet_seq("cat-labels")[1])
count = chart.xpath(".//c:ptCount")[0]
count.getparent().remove(count)

assert Categories(chart).flattened_labels == (
("CA", "SF"),
("CA", "LA"),
("NY", "NY"),
("NY", "Albany"),
)

def it_provides_access_to_its_levels(self, levels_fixture):
categories, CategoryLevel_, calls, expected_levels = levels_fixture
levels = categories.levels
Expand Down