Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mxGraph - How to automatically resize mxCell to content width if it exceeds the default size

How can I create an mxCell, setting the minimum width of its mxGeometry, but that would resize to the width of the html element of its value field if it is higner than the default width?

For now I have this code

var style = 'rounded=0;whiteSpace=wrap;html=1;autosize=1;resizable=0;';
var cell = new mxCell(value, new mxGeometry(0, 0, width, height), style);

But when I create this mxCell, it is always of size "width" and "height", even when the html element it contains (an h3 title) exceeds the mxRectangle. How can I fix this?

Edit : Thanks, it worked using graph.updateCellSize(). As I wanted to change the size only if it had to be higher than default, I wrote this code

this.graph.updateCellSize(cell, true);
var geom = cell.getGeometry();
geom.width = geom.width > width ? geom.width : width;

It changes the value twice, which is inefficient. I found another way using getPreferredSizeForCell

var preferred = this.graph.getPreferredSizeForCell(cell);
var current = cell.getGeometry();
current.width = preferred.width > width ? preferred.width : width;
current.height = preferred.height > height ? preferred.height : height;
like image 588
c4ffein Avatar asked Oct 16 '25 10:10

c4ffein


1 Answers

Try graph.updateCellSize(cell, true);

like image 79
user1084282 Avatar answered Oct 18 '25 23:10

user1084282



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!