A chart category, primarily having a label to be displayed on the category axis, but also able to be configured in a hierarchy for support of multi-level category charts.
| 507 | |
| 508 | |
| 509 | class Category(object): |
| 510 | """ |
| 511 | A chart category, primarily having a label to be displayed on the |
| 512 | category axis, but also able to be configured in a hierarchy for support |
| 513 | of multi-level category charts. |
| 514 | """ |
| 515 | |
| 516 | def __init__(self, label, parent): |
| 517 | super(Category, self).__init__() |
| 518 | self._label = label |
| 519 | self._parent = parent |
| 520 | self._sub_categories = [] |
| 521 | |
| 522 | def add_sub_category(self, label): |
| 523 | """ |
| 524 | Return a newly created |data.Category| object having *label* and |
| 525 | appended to the end of the sub-category sequence for this category. |
| 526 | """ |
| 527 | category = Category(label, self) |
| 528 | self._sub_categories.append(category) |
| 529 | return category |
| 530 | |
| 531 | @property |
| 532 | def depth(self): |
| 533 | """ |
| 534 | The number of hierarchy levels rooted at this category node. Returns |
| 535 | 1 if this category has no sub-categories. |
| 536 | """ |
| 537 | sub_categories = self._sub_categories |
| 538 | if not sub_categories: |
| 539 | return 1 |
| 540 | first_depth = sub_categories[0].depth |
| 541 | for category in sub_categories[1:]: |
| 542 | if category.depth != first_depth: |
| 543 | raise ValueError("category depth not uniform") |
| 544 | return first_depth + 1 |
| 545 | |
| 546 | @property |
| 547 | def idx(self): |
| 548 | """ |
| 549 | The offset of this category in the overall sequence of leaf |
| 550 | categories. A non-leaf category gets the index of its first |
| 551 | sub-category. |
| 552 | """ |
| 553 | return self._parent.index(self) |
| 554 | |
| 555 | def index(self, sub_category): |
| 556 | """ |
| 557 | The offset of *sub_category* in the overall sequence of leaf |
| 558 | categories. |
| 559 | """ |
| 560 | index = self._parent.index(self) |
| 561 | for this_sub_category in self._sub_categories: |
| 562 | if sub_category is this_sub_category: |
| 563 | return index |
| 564 | index += this_sub_category.leaf_count |
| 565 | raise ValueError("sub_category not in this category") |
| 566 |
no outgoing calls
searching dependent graphs…