Dyanamic adjustment of labels to avoid overlapping

I’ve to plot a graph, but the labels present on the edges are overlapping; in the attached image, top and middle part labels are overlapped since, edges are shorter.
I’ve tried using adjustText library, but it’s just not suited for this complexity.
Please suggest how to get around this. Solution doesn’t need to be only related to matplotlib, any other library is also fine as long as its working … If javascript has something, that is also welcomed, since the plot is been send to UI as .svg. But I can convert the whole matplotlib to JS, if any viable solution is present.

Thnks in advanced.

def format_dimension_label(dimensions):
    if isinstance(dimensions, tuple):
        of = f"OF- {int(dimensions[3])}X{int(dimensions[4])}"
        web = f"WEB- {int(dimensions[1])}thk."
        if_ = f"IF- {int(dimensions[5])}X{int(dimensions[6])}"
        return f"{of}\n{web}\n{if_}"
    else:
        return dimensions

def draw_frame(members_and_nodes, node_coordinates, member_dimension):
    fig, ax = plt.subplots(figsize=(12, 10))

    for member in members_and_nodes:
        member_no, bottom_joint, top_joint = member
        x1, y1, z1 = node_coordinates[bottom_joint]
        x2, y2, z2 = node_coordinates[top_joint]

        # Calculate the length of the member
        length = np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2)

        x_values = [x1, x2]
        y_values = [y1, y2]
        # ax.plot(x_values, y_values, "o-", label=f"Member {member_no}")
        ax.plot(x_values, y_values, "o-", color="#a9a9a9", label=f"Member {member_no}")

        dimensions = member_dimension.get(int(member_no))
        if dimensions:
            dimension_label = format_dimension_label(dimensions)
            mid_x = (x_values[0] + x_values[1]) / 2
            mid_y = (y_values[0] + y_values[1]) / 2

            ax.text(
                mid_x,
                mid_y - 0.3,
                dimension_label,
                fontsize=8,
                color="yellow",
                ha="center",
                va="center",
                bbox=dict(
                    facecolor="black",
                    alpha=0.8,
                    edgecolor="none",
                    boxstyle="round,pad=0.1",
                ),
            )

            # Display the length above the member name
            ax.text(
                mid_x,
                mid_y + 0.1,
                f"{length:.3f}",
                fontsize=8,
                color="red",
                ha="center",
                va="center",
            )

            # Calculate and display values at bottom end
            if isinstance(dimensions, tuple):
                bottom_end_value = dimensions[0] - dimensions[4] - dimensions[6]

            ax.text(
                x_values[0],
                y_values[0] + 0.14,
                f"{bottom_end_value}",
                fontsize=8,
                color="blue",
                ha="center",
                va="center",
            )

            # Calculate and display values at top end
            if isinstance(dimensions, tuple):
                top_end_value = dimensions[2] - dimensions[4] - dimensions[6]
            ax.text(
                x_values[1],
                y_values[1] - 0.14,
                f"{top_end_value}",
                fontsize=8,
                color="blue",
                ha="center",
                va="center",
            )

    ax.set_xlabel("")
    ax.set_ylabel("")
    ax.set_title("Sections properties")
    ax.grid(False)
    ax.legend().set_visible(False)

    ax.set_xticks([])
    ax.set_yticks([])

    return fig, ax