Skip to content

Autoresize HTML Labels #247

Description

@ppisecky

Following the stashed HtmlLabel story I tried to implement a similar use case. By default it doesn't seem like cells resize correctly according to the content of the label div or do it at the right times when I set autosize to true as follows:

this.graph.setAutoSizeCells(true);
this.graph.autoSizeCellsOnAdd = true;

I figured that the label size might not be calculating correctly so I overrode the getPreferredSizeForCell method:

const oldGetPreferredSizeForCell = this.graph.getPreferredSizeForCell;
      this.graph.getPreferredSizeForCell = function (cell) {
        const result = oldGetPreferredSizeForCell.apply(this, arguments);

        if (result != null && cell._htmlLabel) {
          result.width = Math.max(
            result.width,
            cell._htmlLabel.offsetWidth,
            cell._htmlLabel.getBoundingClientRect().width
          );
          result.height = Math.max(
            result.height,
            cell._htmlLabel.offsetHeight,
            cell._htmlLabel.getBoundingClientRect().height
          );
        }

        return result;
};

With this approach:

  1. the labels still don't get sized correctly during initiation (I imagine it's something to do with when convertValueToString gets executed) so I have to manually call updateCellSize on every cell after setting up the graph
  2. And the size still seems to get miscalculated on some cells for some reason.

Is there a recommended approach for this use case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions