Skip to content

Commit

Permalink
prepares pre-calculation of segments
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Feb 27, 2024
1 parent f335c88 commit 463d093
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
53 changes: 51 additions & 2 deletions dcm/dcm_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@author: wf
"""
import copy
from typing import List, Optional, Tuple
from typing import Dict, List, Optional, Tuple

from dcm.dcm_core import (
CompetenceElement,
Expand Down Expand Up @@ -293,7 +293,6 @@ def generate_pie_elements(
parent_element: CompetenceElement,
learner: Learner,
segment: DonutSegment,
symmetry_level: int = 1,
):
"""
generate the pie elements (donut segments) for the subelements
Expand Down Expand Up @@ -363,6 +362,55 @@ def generate_pie_elements(
learner=learner,
segment=sub_segment,
)

def calculate_segments(self, ct: CompetenceTree, tree_segment: DonutSegment) -> Dict[str, DonutSegment]:
"""
Pre-calculate the donut segments for each level of the competence tree.
Args:
ct: The competence tree for which segments are to be calculated.
tree_segment: The initial segment representing the whole competence tree.
Returns:
A dictionary where keys are element paths and values are the corresponding DonutSegment objects.
"""
level_segments = {}

def calculate_for_element(element, parent_segment, level=0):
# Calculate segment size based on the number of elements at this level
elements = getattr(element, self.levels[level], [])
total_elements = len(elements)
if total_elements == 0:
return

angle_per_element = (parent_segment.end_angle - parent_segment.start_angle) / total_elements
start_angle = parent_segment.start_angle

for i, sub_element in enumerate(elements):
end_angle = start_angle + angle_per_element
# Create a new segment for this element
segment = DonutSegment(
cx=tree_segment.cx,
cy=tree_segment.cy,
inner_radius=tree_segment.inner_radius,
outer_radius=tree_segment.outer_radius,
start_angle=start_angle,
end_angle=end_angle,
text_mode=tree_segment.text_mode,
)
# Store the segment with its corresponding path as the key
level_segments[sub_element.path] = segment

# Recurse for sub-elements if not at the last level
if level + 1 < len(self.levels):
calculate_for_element(sub_element, segment, level + 1)

start_angle = end_angle

# Start the calculation with the root of the competence tree
calculate_for_element(ct, tree_segment)

return level_segments

def generate_svg_markup(
self,
Expand Down Expand Up @@ -416,6 +464,7 @@ def generate_svg_markup(
segment = DonutSegment(
cx=self.cx, cy=self.cy, inner_radius=0, outer_radius=self.tree_radius
)
self.calculate_segments(competence_tree,segment)
self.generate_pie_elements(
level=0,
svg=svg,
Expand Down
4 changes: 4 additions & 0 deletions dcm/dcm_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,27 +284,31 @@ def update_paths(self):
self.path = self.id
self.total_levels = 1
self.elements_by_path = {self.path: self}
self.elements_by_level = {"aspect": [], "area": [], "facet": []} # Reset for re-calculation
# Loop through each competence aspect and set their paths and parent references
for aspect in self.aspects:
aspect.competence_tree = self
aspect.path = f"{self.id}/{aspect.id}"
self.elements_by_path[aspect.path] = aspect
self.total_elements["aspects"] = self.total_elements["aspects"] + 1
self.total_levels = 2
self.elements_by_level["aspect"].append(aspect)
for area in aspect.areas:
self.total_levels = 3
area.competence_tree = self
area.aspect = aspect
area.path = f"{self.id}/{aspect.id}/{area.id}"
self.elements_by_path[area.path] = area
self.total_elements["areas"] = self.total_elements["areas"] + 1
self.elements_by_level["area"].append(area)
for facet in area.facets:
self.total_levels = 4
facet.competence_tree = self
facet.area = area
facet.path = f"{self.id}/{aspect.id}/{area.id}/{facet.id}"
self.elements_by_path[facet.path] = facet
self.total_elements["facets"] = self.total_elements["facets"] + 1
self.elements_by_level["facet"].append(facet)

@classmethod
def required_keys(cls) -> Tuple:
Expand Down

0 comments on commit 463d093

Please sign in to comment.