I am using this to create a simple branched diagram, with one item, branches into two, and each of those two further branch into two.

I'm running into two problems:

(1) The lines cut right through the word "apple" and "banana", I need to place a node at the right of apple and banana that connects to sub-branches. In other words, I'd rather it have a broken line, ending at left of apple and then continuing at right of apple.

(2) If you put longer text in the top category, it adjusts. And if you put longer text in the lowest branches, it adjusts, but I can't find a way to get the branches to adjust length to accommodate longer text in the middle, where apple and banana are.

I tried using different code to find textwidth, but it isn't working:

numeric text_width;
text_width = width(textext("#2"));

How can I make the diagram adjust if the words in the middle branches are longer?

--Joel

\starttext

% Define the macro outside the Metapost code block
\def\macroXa#1#2#3#4#5#6#7
{
\startMPcode
u := 1.5cm; % Reduced unit size for compact spacing

% Main node positions
z0 = (0, 0);       % Fruit position
z1 = (1u, 0);    % Horizontal segment from Fruit
z2 = (1u, 0.6u);   % Apple position
z3 = (1u, -0.6u);  % Banana position
z4 = (3u, 0.9u); % Fuji branch
z5 = (3u, 0.3u); % Gala branch
z6 = (3u, -0.3u);% Cavendish branch
z7 = (3u, -0.9u);% Plantain branch
z8 = (4u, 0.9u);   % Fuji
z9 = (4u, 0.3u);   % Gala
z10 = (4u, -0.3u); % Cavendish
z11 = (4u, -0.9u); % Plantain

% Main paths
draw z0 -- z1; % Horizontal from Fruit
draw z1 -- z2; % Line to Apple (90-degree angle)
draw z1 -- z3; % Line to Banana (90-degree angle)

% Apple sub-branches
draw z2 -- (3u, 0.6u) -- z4; % Right angle to Fuji
draw z2 -- (3u, 0.6u) -- z5; % Right angle to Gala

% Banana sub-branches
draw z3 -- (3u, -0.6u) -- z6; % Right angle to Cavendish
draw z3 -- (3u, -0.6u) -- z7; % Right angle to Plantain

% Final horizontal branches
draw z4 -- z8; % Fuji
draw z5 -- z9; % Gala
draw z6 -- z10; % Cavendish
draw z7 -- z11; % Plantain

% Add labels using macro arguments
label.lft(btex #1 etex, z0); % Fruit
label.rt(btex #2 etex, z2); % Apple
label.rt(btex #3 etex, z3); % Banana
label.rt(btex #4 etex, z8); % Fuji
label.rt(btex #5 etex, z9); % Gala
label.rt(btex #6 etex, z10); % Cavendish
label.rt(btex #7 etex, z11); % Plantain
\stopMPcode
}

% Call the macro with labels
\macroXa{Fruit}{Apple}{Banana}{Fuji}{Gala}{Cavendish}{Plantain}
\macroXa{This text is longer}{Apple}{Banana}{Fuji}{Gala}{Cavendish}{Plantain}
\macroXa{This text is longer}{Apple}{it needs to accomodate longer text here}{Fuji}{Gala}{Cavendish}{Plantain}

\stoptext