You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Incremental Tree中动态调整节点大小及优化文本位置

Alright, let's break down how to solve your two key requirements for your GoJS incremental tree—dynamic node sizing based on the share value, and positioning text inside nodes with overflow allowed. I'll walk you through the modifications step by step.

1. Dynamic Node Sizing Using share as a Percentage

First, we'll set a default base size for your nodes, then bind the shape's dimensions to the share value to scale it proportionally. Let's use a default 40x40 circle (you can tweak this to fit your needs):

  • Add default width and height to the circle shape
  • Use a binding function to calculate the scaled size from the share value (0 = default size, 1 = 100% of default, 0.5 = 50%, etc.)
  • Add boundary checks to avoid tiny or oversized nodes

2. Place Text Inside Nodes with Overflow Allowed

Your original template places the shape and text side-by-side. We'll switch the inner panel to a Spot layout to overlay the text directly on the circle, then configure the text block to allow overflow beyond the shape's bounds.

Full Updated Node Template

Here's the revised code that combines both fixes:

myDiagram.nodeTemplate = $(go.Node, "Spot", {
  selectionObjectName: "PANEL",
  isTreeExpanded: true,
  isTreeLeaf: true
},
// Inner panel changed to Spot to overlay text on the circle
$(go.Panel, "Spot", { name: "PANEL" },
  $(go.Shape, "Circle", {
    fill: "whitesmoke",
    stroke: "black",
    width: 40, // Default base width
    height: 40 // Default base height
  },
  // Random color binding (kept from your original code)
  new go.Binding("fill", "color", () => go.Brush.randomColor()),
  // Dynamic size binding based on share value
  new go.Binding("size", "share", function(share) {
    const defaultSize = new go.Size(40, 40);
    // Use default size if share is 0
    if (share === 0) return defaultSize;
    // Ensure scale stays between 10% and 100% to avoid extreme sizes
    const scale = Math.max(0.1, Math.min(1, share));
    return new go.Size(defaultSize.width * scale, defaultSize.height * scale);
  })),
  $(go.TextBlock, {
    font: "6pt sans-serif",
    alignment: go.Spot.Center, // Center text on the circle
    overflow: go.TextBlock.OverflowVisible, // Allow text to spill outside the shape
    wrap: go.TextBlock.None // Disable wrapping to keep text intact
  },
  new go.Binding("text", "text"))
),
// Keep your original TreeExpanderButton
$("TreeExpanderButton", {
  width: 20,
  height: 20,
  alignment: go.Spot.TopRight,
  alignmentFocus: go.Spot.Center
}, { visible: true })
); // end Node

Quick Usage Notes

  • When adding node data, set share to a value between 0 and 1 (e.g., share: 0.7 = 70% of default size). If your share values are whole percentages (like 50 for 50%), adjust the binding function to divide by 100: const scale = Math.max(0.1, Math.min(1, share / 100));
  • All nodes will automatically resize once added to the model—no extra steps needed after inserting all data. If you update a node's share later, call myDiagram.model.setDataProperty(nodeData, "share", newValue) to trigger a size refresh.

内容的提问来源于stack exchange,提问作者Hemant Sisodia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:05:07