Calling makeSvg on Diagram returned null

Thanks. Your answer helped me a lot!

var img;
function writeJson(coll) {  // assume COLL is a collection of Parts
    var tempDiag = new go.Diagram();
    tempDiag.model = myDiagram.model.copy();  // doesn't copy data
    myDiagram.copyParts(coll, tempDiag, false);
    img = tempDiag.makeSvg({
        scale: 1,
        background: "#E9E9E9",
        maxSize: new go.Size(Infinity, Infinity),
    });
    return tempDiag.model.toJson();
  }

But img returned null. Why is this null?
tempDiag.makeImage also returned null.

any reply here plz?

Do you just want a response, or do you want an answer? Sometimes it takes time to develop an answer.

Also you never indicated whether the solution I gave to your initial question solved that problem. Or even acknowledged that I had provided something for you to use.

Here’s my answer: if you want to render a diagram, the diagram needs to be associated with an HTML Div element:

  var img;
  function writeJson(coll) {  // assume COLL is a collection of Parts
    if (!coll) coll = myDiagram.selection;
    var tempDiag = new go.Diagram();
    var dummyDiv = document.createElement("div");
    tempDiag.div = dummyDiv;
    tempDiag.model = myDiagram.model.copy();  // doesn't copy data
    myDiagram.copyParts(coll, tempDiag, false);
    img = tempDiag.makeSvg({
        scale: 1,
        background: "#E9E9E9",
        maxSize: new go.Size(Infinity, Infinity)
    });
    tempDiag.div = null;
    return tempDiag.model.toJson();
  }

Sorry, I was super busy with coding so I couldn’t reply properly.
And I tried above but tempDiag rendered node templates and links wrong.
So should we add node templates map and link templates of myDiagram to tempDiagram?

Yes, that would make sense. You can share the nodeTemplateMap and linkTemplateMap and groupTemplateMap.


I tried to save 2 nodes and 1 link.
But in thumbnail, link is broken. FYI, I added this to above function:

tempDiag.nodeTemplateMap = funnelDiagram.nodeTemplateMap;
tempDiag.linkTemplate = funnelDiagram.linkTemplate;

I don’t have any group template map or link template map. Only one link template.
Any help plz?

I think coordinate system should match on both of diagram as every node data has “loc” (location) property.
For this, I made divs of 2 diagrams ( myDiagram, tempDiag) as same size, position. But still same error.
And what properties of myDiagram should I copy to tempDiag to fix this issue?

Is there a reason you are making so much work for yourself when you could just call Diagram.makeSvg on the original diagram?

I wanna save selected elements as ‘template’. So that’s why I asked how to save selected elements as json. Now I also want to display diagram overview when I hover over that template on the list.
So I used makeSvg. But on original digram, how can I take screenshot of selected elements only?

Is there a way to temporarily backup model data and remove unselected elements from myDiagram and make image and fetch original model data so myDiagram is totally same as before?

Call Diagram.makeSvg and use the parts option.
https://gojs.net/latest/intro/makingSVG.html

LOL. Too Simple. Thanks!