Hi,\nI am starting to learn how to use this powerful framework and I could not achieve a very simple problem : constrain a cell into its parent's cell.
\nI have something like that in my javascript code (javascript only) based on the hello world example

graph.constrainChildren = true;  \ngraph.defaultOverlap = 0;  \nvar parent = graph.getDefaultParent();  \ngraph.getModel().beginUpdate();  \ntry  \n{  \nvar v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 200, 200);  \nvar v2 = graph.insertVertex(v1, null, 'World!', 30, 30, 80, 30);  \n}  \nfinally  \n{  \ngraph.getModel().endUpdate();  \n}\n

And with this few lines of code I can still move the inner vertex (v2) outside its parent (v1).
\nWhat am I doing wrong ?

asked 05 Apr '11, 01:08

ppRaistlin's gravatar image

accept rate: 0%

edited 05 Apr '11, 04:35

Gaudenz's gravatar image


Use mxGraphHandler.prototype.removeCellsFromParent = false or override mxGraphHandler.prototype.shouldRemoveCellsFromParent.


answered 05 Apr '11, 01:17

Gaudenz's gravatar image

accept rate: 39%

Thanks for your answer. It seems it s still not working on my simple example. I guess I am doing something wrong:


var graph = new mxGraph(container);  nvar graphHandler = new mxGraphHandler(graph);  ngraphHandler.removeCellsFromParent = false;nvar parent = graph.getDefaultParent();  ngraph.getModel().beginUpdate();  ntry  n{  n  var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 200, 200);  n  var v2 = graph.insertVertex(v1, null, 'World!', 30, 30, 80, 30);  n}  nfinally  n{  n  graph.getModel().endUpdate();  n}n

(05 Apr '11, 01:35) ppRaistlin

Use the code as shown, eg. mxGraphHandler.prototype.removeCellsFromParent = false before creating the graph instance or else use graph.graphHandler.removeCellsFromParent = false.

(05 Apr '11, 01:37) Gaudenz

Of course. My mistake. Thanks for your help.\nJust a question: what the use of the function mxGraph.prototype.constrainChild \nIt says : "Keeps the given cell inside the bounds returned by getCellContainmentArea for its parent"\nand for getCellContainmentArea it says "Returns the mxRectangle inside which a cell is to be kept".
\nDoes that mean that in order to use constrainChild you have to override getCellContainmentArea adding the code to return the correct bound rectangle of the parent ?

(05 Apr '11, 01:50) ppRaistlin

No, the default implementation of getCellContainmentArea already does that.

(05 Apr '11, 04:08) Gaudenz

Ok Thanks. So what is the difference between mxGraph.prototype.constrainChild ("Keeps the given cell inside the bounds returned by getCellContainmentArea for its parent" - that doesn t work in my exemple) and mxGraphHandler.prototype.removeCellsFromParent("Specifies if cells may be moved out of their parents. Default is true." - that is actually working) ?

(05 Apr '11, 04:33) ppRaistlin

RemoveCellsFromParent moves it out of the parent before it can be constrained.

(05 Apr '11, 04:36) Gaudenz
showing 5 of 6 show all
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 05 Apr '11, 01:08

Seen: 3,151 times

Last updated: 05 Apr '11, 04:38