GoJS has one function for creating SVG: Diagram.makeSvg, which returns a new SVGElement with a representation of a GoJS Diagram. The method has a single argument, a JavaScript Object that contains several definable properties, enumerated in the documentation.
SVG export can be useful as content for a PDF, because the rendering is vector oriented instead of raster oriented. Most GoJS users who create PDFs do so by exporting Diagrams to SVG or images and place that content in their PDFs, on the server or elsewhere.
This page is almost identical to the page on Making Images, which shows how to render raster images instead of SVG elements.
Diagram.makeSvg produces a static SVG "image" -- the SVG will not be interactive. Note that exporting a diagram to SVG is different from the functionality introduced in v2.3 that allows the interactive diagram to be rendered in SVG rather than in Canvas. For more information please read: SVG Drawing Context
Below are several examples of using Diagram.makeSvg on the following diagram:
Calling makeSvg with no arguments or with an empty properties object results in a scene that is the same size as the Diagram's viewport.
myDiagram.makeSvg();
Calling makeSvg with an object that has the "scale" property set to 1 results in a scene that includes the whole diagram, not just the area visible in the viewport. However, the empty areas around the document bounds are trimmed away.
myDiagram.makeSvg({
scale: 1
});
Setting the scale property will create a scaled SVG Scene that is precisely large enough to contain the Diagram. The following SVG is created with a scale of 2.
myDiagram.makeSvg({
scale: 2
});
Note how, unlike an image, you can select the text.
The following SVG is created by setting the size option of makeSvg. Note that the canvas is scaled uniformly and any extra space is placed on the bottom or right side of the SVG.
myDiagram.makeSvg({
size: new go.Size(100,100)
});
The following SVG is also created by setting the size option of makeSvg, but only the width is set. The height will be whatever size is needed to uniformly contain the Diagram.
myDiagram.makeSvg({
size: new go.Size(100,NaN)
});
The parts option allows us to specify an Iterable collection of Parts to draw. This is useful if you only want to make an image of part of the diagram, such as a selection of nodes.
myPartsList = new go.List();
myPartsList.add(myDiagram.findNodeForKey("Beta"));
myPartsList.add(myDiagram.findNodeForKey("Delta"));
myDiagram.makeSvg({
parts: myPartsList
});
Or drawing only the links:
myDiagram.makeSvg({
parts: myDiagram.links
});
Setting both scale and size creates an image that is scaled specifically and cropped to the given size, as in the following image.
myDiagram.makeSvg({
scale: 1.5,
size: new go.Size(100,100)
});
We may want a very large, scaled image that has a limit on its size, and we can use the maxSize property to constrain one or both dimensions. The following image has a very large scale applied but is limited in size horizontally, so some horizontal cropping will occur.
The default value for maxSize is go.Size(2000, 2000)
, and specifying go.Size(600, NaN)
is equivalent to specifying
go.Size(600, 2000)
. If we wanted no cropping on the height we could instead write go.Size(600, Infinity)
.
myDiagram.makeSvg({
scale: 9,
maxSize: new go.Size(600, NaN)
});
Setting both position and size creates a diagram image that is positioned specifically and cropped to the given size. When a position is set but no scale is set, the scale defaults to 1.
myDiagram.makeSvg({
position: new go.Point(20,20),
size: new go.Size(50,50)
});
Setting the background to a CSS color string will replace the transparent Diagram background with the given color.
myDiagram.makeSvg({
size: new go.Size(NaN,250),
background: "rgba(0, 255, 0, 0.5)" // semi-transparent green background
});
In the following code we use the document bounds to split the Diagram into four equal parts, making four images out of each part. In this way we can prepare images for pagination, making a gallery, or printing purposes. The four images created are shown below.
var d = myDiagram.documentBounds;
var halfWidth = d.width / 2;
var halfHeight = d.height / 2;
svg = myDiagram.makeSvg({
position: new go.Point(d.x, d.y),
size: new go.Size(halfWidth,halfHeight)
});
addSVG(svg); // Adds the SVG to a DIV below
svg = myDiagram.makeSvg({
position: new go.Point(d.x + halfWidth, d.y),
size: new go.Size(halfWidth,halfHeight)
});
addSVG(svg);
svg = myDiagram.makeSvg({
position: new go.Point(d.x, d.y+ halfHeight),
size: new go.Size(halfWidth,halfHeight)
});
addSVG(svg);
svg = myDiagram.makeSvg({
position: new go.Point(d.x + halfWidth, d.y + halfHeight),
size: new go.Size(halfWidth,halfHeight)
});
addSVG(svg);
You can open the SVG in a new window by appending it to the DOM of a new page:
var button = document.getElementById('openSVG');
button.addEventListener('click', () => {
var newWindow = window.open("","newWindow");
if (!newWindow) return;
var newDocument = newWindow.document;
var svg = myDiagram.makeSvg({
document: newDocument, // create SVG DOM in new document context
scale: 9,
maxSize: new go.Size(600, NaN)
});
newDocument.body.appendChild(svg);
}, false);
You do not need to involve the web server if you want the user to download an SVG file. See the sample Minimal SVG. Note that that sample only downloads a single SVG file, but that file can cover the whole document.
As of 2.3.11, GoJS makeSVG and the SVG Rendering Context both set some SVG data attributes. These are:
portId
if a GraphObject.portId is set.itemIndex
if a Panel.itemIndex is set.className
if a Part.key
if a Part, if Part.key is set.fromKey
if a Link with a fromNode, the Part.key of that Node.fromPortId
if a Link.fromPortId is set.toKey
if a Link with a toNode, the Part.key of that Node.toPortId
if a Link.toPortId is set.So a Node with Node data containing { key: 'Alpha' }
would have data properties:
<g transform="matrix(1, 0, 0, 1, 0, 0)" data-class-name="Node" data-key="Alpha">
...
</g>