Hi,
I already have a tree in my diagram with a couple of nodes linked. When I click on a button I want to programmatically add another tree. How do I do that?
I tried to get a ref to GraphLinksModel but it returns undefined. I don’t want to disrupt another tree model that is already on the diagram but add to the diagram.
thanks again!
I have this:
var nodeDataArray = [
{ key: “Alpha”, modelType: “capability” }, { key: “Beta”, modelType: “capability” }, { key: “Gamma”, modelType: “capability” }, { key: “Delta”, modelType: “capability” },
];
var linkDataArray = [
{ from: “Alpha”, to: “Beta” },
{ from: “Beta”, to: “Gamma” },
{ from: “Beta”, to: “Delta” }
];
var nodeDataArray2 = [
{ key: “Epsilon”, modelType: “org” }, { key: “Zeta”, modelType: “org” }, { key: “Eta”, modelType: “org” }, { key: “Theta”, modelType: “org” }
];
var linkDataArray2 = [
{ from: “Epsilon”, to: “Zeta” },
{ from: “Epsilon”, to: “Eta” },
{ from: “Epsilon”, to: “Theta” }
];
myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
so the diagram shows one tree model and its links…all good
on a click I need to add the second tree to the diagram, so I tried this:
var model = myDiagram.model;
if (model instanceof go.GraphLinksModel) {
var arrayLength = nodeDataArray2.length;
for (var i = 0; i < arrayLength; i++) {
myDiagram.model.addNodeData(nodeDataArray2);
}
}
where it would iterate over the array and individually add the nodes, but that doesn’t seem to work…
So I tried it this way using graphs and it seems to work but is there a better way?
var nodeDataArray = [
{ key: “Alpha”, modelType: “capability” }, { key: “Beta”, modelType: “capability” }, { key: “Gamma”, modelType: “capability” }, { key: “Delta”, modelType: “capability” },
];
var linkDataArray = [
{ from: “Alpha”, to: “Beta” },
{ from: “Beta”, to: “Gamma” },
{ from: “Beta”, to: “Delta” }
];
var nodeDataArray2 = [
{ key: “Org”, modelType: “org”, isGroup: true }, { key: “Epsilon”, modelType: “org”, group: “Org” }, { key: “Zeta”, modelType: “org”, group: “Org” },
{ key: “Eta”, modelType: “org”, group: “Org” }, { key: “Theta”, modelType: “org”, group: “Org” }
];
var linkDataArray2 = [
{ from: “Epsilon”, to: “Zeta” },
{ from: “Epsilon”, to: “Eta” },
{ from: “Epsilon”, to: “Theta” }
];
myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
function nodeDoubleClick(e, obj) {
var clicked = obj.part;
if (clicked !== null) {
var model = myDiagram.model;
if (model instanceof go.GraphLinksModel) {
myDiagram.startTransaction(“make new group”);
var arrayLength = nodeDataArray2.length;
for (var i = 0; i < arrayLength; i++) {
myDiagram.model.addNodeData(nodeDataArray2);
}
var arrayLength = linkDataArray2.length;
for (var i = 0; i < arrayLength; i++) {
myDiagram.model.addLinkData(linkDataArray2);
}
myDiagram.commitTransaction(“make new group”);
}
}
}
Use Model.addNodeDataCollection and GraphLinksModel.addLinkDataCollection.
All within a transaction, of course.
Amazing!! Works perfectly! Thanks!!