All files / app/codeCharta/util/algorithm/treeMapLayout treeMapGenerator.ts

98.14% Statements 106/108
91.66% Branches 55/60
100% Functions 14/14
98.11% Lines 104/106

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 28952x 52x   52x       52x 52x 52x 52x 52x 52x   52x 44x 44x 44x 44x   44x 3x     3x       3x     3x 3x     3x   3x                                   41x 41x 41x 283x   41x                               5x   5x 14x 14x   14x         46x 46x       46x 46x 46x 46x     46x 46x 46x 46x   46x 46x   46x           2x 2x   2x                                           2x         5x           90x       3x 3x 3x 3x       55x 55x 55x 55x 55x         55x 14x 14x   41x 41x     55x 55x     346x 93x 51x   93x 42x                   55x 55x     55x         95x 52x           95x   93x   51x         42x 42x         2x     55x   346x               58x 58x 58x 395x     395x             58x       18x       52x           349x       349x 2x     347x 59x 59x 59x   59x 1x       58x   288x    
import { hierarchy, HierarchyNode, HierarchyRectangularNode, treemap } from "d3-hierarchy"
import { TreeMapHelper, treeMapSize } from "./treeMapHelper"
import { CodeMapNode, DynamicSettings, Node, NodeMetricData, CcState } from "../../../codeCharta.model"
import { getMapResolutionScaleFactor, isLeaf } from "../../codeMapHelper"
 
export type SquarifiedTreeMap = { treeMap: HierarchyRectangularNode<CodeMapNode>; height: number; width: number }
 
const PADDING_SCALING_FACTOR = 0.4
const DEFAULT_PADDING_FLOOR_LABEL_FROM_LEVEL_1 = 120
const DEFAULT_PADDING_FLOOR_LABEL_FROM_LEVEL_2 = 95
const DEFAULT_ROOT_FLOOR_LABEL_SCALING = 0.035
const DEFAULT_SUB_FLOOR_LABEL_SCALING = 0.028
export const HIERARCHY_LEVELS_WITH_LABLES_UPPER_BOUNDARY = 3
 
export function createTreemapNodes(map: CodeMapNode, state: CcState, metricData: NodeMetricData[], isDeltaState: boolean): Node[] {
    const mapSizeResolutionScaling = getMapResolutionScaleFactor(state.files)
    const maxHeight = metricData.find(x => x.name === state.dynamicSettings.heightMetric)?.maxValue * mapSizeResolutionScaling
    const maxWidth = metricData.find(x => x.name === state.dynamicSettings.areaMetric)?.maxValue * mapSizeResolutionScaling
    const heightScale = (treeMapSize * 2) / maxHeight
 
    if (hasFixedFolders(map)) {
        const hierarchyNode = hierarchy(map)
 
        // Base root folder has width: 100px and length: 100px
        const nodes: Node[] = [TreeMapHelper.buildRootFolderForFixedFolders(hierarchyNode.data, heightScale, state, isDeltaState)] // nosonar
 
        // Multiply mapSize of (default) 250px by 2 = 500px and add the total margin
        const totalMapSize =
            treeMapSize * 2 + getEstimatedNodesPerSide(hierarchyNode) * (state.dynamicSettings.margin / PADDING_SCALING_FACTOR)
 
        // than divide through the root folder width and length to get a scale factor for calculation for all following nodes.
        const scaleLength = totalMapSize / nodes[0].width
        const scaleWidth = totalMapSize / nodes[0].length
 
        // Scale the 100x100 root folder to be bigger and to match fixed/estimated totalMapSize
        scaleRoot(nodes[0], scaleLength, scaleWidth)
 
        return [
            ...nodes,
            ...buildSquarifiedTreeMapsForFixedFolders(
                hierarchyNode,
                state,
                scaleLength,
                scaleWidth,
                0,
                0,
                heightScale,
                maxHeight,
                maxWidth,
                isDeltaState,
                mapSizeResolutionScaling
            )
        ]
    }
 
    const squarifiedTreeMap = getSquarifiedTreeMap(map, state, mapSizeResolutionScaling, maxWidth)
    const nodes: Node[] = []
    for (const squarifiedNode of squarifiedTreeMap.treeMap) {
        nodes.push(TreeMapHelper.buildNodeFrom(squarifiedNode, heightScale, maxHeight, state, isDeltaState))
    }
    return nodes
}
 
function buildSquarifiedTreeMapsForFixedFolders(
    hierarchyNode: HierarchyNode<CodeMapNode>,
    state: CcState,
    scaleLength: number,
    scaleWidth: number,
    offsetX0: number,
    offsetY0: number,
    heightScale: number,
    maxHeight: number,
    maxWidth: number,
    isDeltaState: boolean,
    mapSizeResolutionScaling: number
) {
    const nodes = []
 
    for (const fixedFolder of hierarchyNode.children) {
        const fixedPosition = fixedFolder.data.fixedPosition
        const squarified = getSquarifiedTreeMap(fixedFolder.data, state, mapSizeResolutionScaling, maxWidth)
 
        for (const squarifiedNode of squarified.treeMap.descendants()) {
            // squarified.width/height is a sum of the fixedPosition.width/height and applied margins
            // The following scaling factors are used later to calculate out the original percentage fixed width/height.
            // Example: Width and Height fixed at 20. Added margins 42px (absolute value) for each = 62px
            //  20/62 = 0.322...       62 x 0.322... = 20 :)
            const scaleX = fixedPosition.width / squarified.width
            const scaleY = fixedPosition.height / squarified.height
 
            // Scale the fixed (percentage) positions of a node by the right scale factor, so that it will be placed properly.
            // The squarifiedNode coordinates x0, x1, y0, y1 are already assigned with positions from the treemap algorithm.
            squarifiedNode.x0 = (squarifiedNode.x0 * scaleX + fixedPosition.left) * scaleWidth
            squarifiedNode.x1 = (squarifiedNode.x1 * scaleX + fixedPosition.left) * scaleWidth
            squarifiedNode.y0 = (squarifiedNode.y0 * scaleY + fixedPosition.top) * scaleLength
            squarifiedNode.y1 = (squarifiedNode.y1 * scaleY + fixedPosition.top) * scaleLength
 
            // Add x and y (absolute px) offsets from parent fixed folder, if any.
            squarifiedNode.x0 += offsetX0
            squarifiedNode.x1 += offsetX0
            squarifiedNode.y0 += offsetY0
            squarifiedNode.y1 += offsetY0
 
            const node = TreeMapHelper.buildNodeFrom(squarifiedNode, heightScale, maxHeight, state, isDeltaState)
            nodes.push(node)
 
            if (hasFixedFolders(fixedFolder.data)) {
                // Imagine the parent Folder has absolute px-width of 341px
                // We need to calculate a scale factor for the fixed child to transform a relative percentage fixed width/height to px.
                // Example: 20% of 341px:
                //  fixedWidth=20, parent Fixed Folder width: 341px => 20 * (scaleLength=341/100)
                // The original scaleWidth/scaleLength must be retained for the case that you have more than one fixedFolder on the same level.
                const childRelativeLengthScale = node.length / 100
                const childRelativeWidthScale = node.width / 100
 
                Array.prototype.push.apply(
                    nodes,
                    buildSquarifiedTreeMapsForFixedFolders(
                        fixedFolder,
                        state,
                        childRelativeLengthScale,
                        childRelativeWidthScale,
                        squarifiedNode.x0,
                        squarifiedNode.y0,
                        heightScale,
                        maxHeight,
                        maxWidth,
                        isDeltaState,
                        mapSizeResolutionScaling
                    )
                )
 
                // the break is actually needed!
                // the inner for-loop loops over the (parent) fixedFolder and its descendants.
                // if a direct child is a fixedFolder as well it will be handled by the recursive function call.
                // In this case, we must break the inner loop after handling the fixedFolder child recursively
                // and therefore prevent that the fixed child will be added as a node twice.
                break
            }
        }
    }
 
    return nodes
}
 
function hasFixedFolders(map: CodeMapNode) {
    // What if the second child of root is the first fixed folder?
    // Assumption: all children of the root folder require the fixedPosition attribute.
    return Boolean(map.children[0]?.fixedPosition)
}
 
function scaleRoot(root: Node, scaleLength: number, scaleWidth: number) {
    root.x0 *= scaleWidth
    root.y0 *= scaleLength
    root.width *= scaleWidth
    root.length *= scaleLength
}
 
function getSquarifiedTreeMap(map: CodeMapNode, state: CcState, mapSizeResolutionScaling: number, maxWidth: number): SquarifiedTreeMap {
    const hierarchyNode = hierarchy(map)
    const nodesPerSide = getEstimatedNodesPerSide(hierarchyNode)
    const { enableFloorLabels, experimentalFeaturesEnabled } = state.appSettings
    const { margin } = state.dynamicSettings
    const padding = margin * PADDING_SCALING_FACTOR * mapSizeResolutionScaling
 
    let mapWidth
    let mapHeight
 
    if (map.fixedPosition !== undefined) {
        mapWidth = map.fixedPosition.width
        mapHeight = map.fixedPosition.height
    } else {
        mapWidth = treeMapSize * 2
        mapHeight = treeMapSize * 2
    }
 
    let addedLabelSpace = 0
    hierarchyNode.eachAfter(node => {
        // Precalculate the needed paddings for the floor folder labels to be able to expand the default map size
        // TODO fix estimation, estimation of added label space is inaccurate
        if (!isLeaf(node) && enableFloorLabels) {
            if (node.depth === 0) {
                addedLabelSpace += DEFAULT_PADDING_FLOOR_LABEL_FROM_LEVEL_1
            }
            if (node.depth > 0 && node.depth < HIERARCHY_LEVELS_WITH_LABLES_UPPER_BOUNDARY) {
                addedLabelSpace += DEFAULT_PADDING_FLOOR_LABEL_FROM_LEVEL_2
            }
        }
    })
 
    // nodesPerSide is just an estimation.
    // We do not know the exact amount,
    // because the treemap algorithm is/must be executed with an initial width and height afterwards.
    // TODO If it is wrong some buildings might be cut off.
    // Use mapSizeResolutionScaling to scale down the pixels need for rendering of the map (width and height size)
    const width = (mapWidth + nodesPerSide * margin + addedLabelSpace) * mapSizeResolutionScaling
    const height = (mapHeight + nodesPerSide * margin + addedLabelSpace) * mapSizeResolutionScaling
 
    let rootNode
    const treeMap = treemap<CodeMapNode>()
        .size([width, height])
        .paddingOuter(padding)
        .paddingInner(padding)
        .paddingRight(node => {
            if (!rootNode && node.parent === null) {
                rootNode = node
            }
 
            // TODO This will not work for FixedFolders
            // it seems that depth property is missing in that case
            // so the default padding will be added, which is fine though.
            if (rootNode && enableFloorLabels) {
                // Start the labels at level 1 not 0 because the root folder should not be labeled
                if (node.depth === 0) {
                    // Add a big padding for the first folder level (the font is bigger than in deeper levels)
                    return Math.max(
                        (rootNode.x1 - rootNode.x0) * DEFAULT_ROOT_FLOOR_LABEL_SCALING,
                        DEFAULT_PADDING_FLOOR_LABEL_FROM_LEVEL_1
                    )
                }
                if (node.depth > 0 && node.depth < HIERARCHY_LEVELS_WITH_LABLES_UPPER_BOUNDARY) {
                    return Math.max((rootNode.x1 - rootNode.x0) * DEFAULT_SUB_FLOOR_LABEL_SCALING, DEFAULT_PADDING_FLOOR_LABEL_FROM_LEVEL_2)
                }
            }
 
            // add treemap algorithm default padding otherwise
            return padding
        })
 
    return {
        treeMap: treeMap(
            hierarchyNode.sum(node => calculateAreaValue(node, state, maxWidth, experimentalFeaturesEnabled) * mapSizeResolutionScaling)
        ),
        height,
        width
    }
}
 
function getEstimatedNodesPerSide(hierarchyNode: HierarchyNode<CodeMapNode>) {
    let totalNodes = 0
    let blacklistedNodes = 0
    hierarchyNode.each(({ data }) => {
        Iif (data.isExcluded || data.isFlattened) {
            blacklistedNodes++
        }
        totalNodes++
    })
 
    // What does this line do?
    // Imagine a 3x3 grid of 9 nodes
    // 3 nodes are placed on the x-axis and 3 on the y-axis = 6
    // The calculated value is probably used to calculate the total margin which extends length and width of the map.
    return 2 * Math.sqrt(totalNodes - blacklistedNodes)
}
 
function isOnlyVisibleInComparisonMap(node: CodeMapNode, dynamicSettings: DynamicSettings) {
    return node.attributes[dynamicSettings.areaMetric] === 0 && node.deltas[dynamicSettings.heightMetric] < 0
}
 
// Only exported for testing.
export function calculateAreaValue(
    node: CodeMapNode,
    { dynamicSettings, appSettings, fileSettings }: CcState,
    maxWidth: number,
    experimentalFeaturesEnabled: boolean
) {
    Iif (node.isExcluded) {
        return 0
    }
 
    if (node.deltas && isOnlyVisibleInComparisonMap(node, dynamicSettings)) {
        return Math.abs(node.deltas[dynamicSettings.areaMetric])
    }
 
    if (isLeaf(node) && node.attributes?.[dynamicSettings.areaMetric]) {
        const areaMetric = dynamicSettings.areaMetric
        const attributeDescriptors = fileSettings.attributeDescriptors
        const isAttributeDirectionInversed = attributeDescriptors[areaMetric]?.direction === 1
 
        if (isAttributeDirectionInversed) {
            return appSettings.invertArea
                ? node.attributes[dynamicSettings.areaMetric]
                : maxWidth - node.attributes[dynamicSettings.areaMetric]
        }
        return appSettings.invertArea ? maxWidth - node.attributes[dynamicSettings.areaMetric] : node.attributes[dynamicSettings.areaMetric]
    }
    return experimentalFeaturesEnabled ? 0.5 : 0
}