OK, here’s a modification of the TreeView sample, GoJS Tree View.
To the model I added a singleton object whose key is “_BANDS” and whose category is “BANDS”. (You can use whatever key and category name you want, of course.) This object is modified by a custom TreeLayout, BorderedTreeLayout which I have defined below.
The main thing that is missing is setting the vertical lines properly for your circumstances. You can implement that. For the purposes of this demonstration, I hard-coded two vertical lines at fixed x positions.
<!DOCTYPE html>
<html><body>
<div id="sample">
<div id="myDiagramDiv" style="border: 1px solid black; width: 500px; height: 700px"></div>
</div>
<script src="https://unpkg.com/gojs"></script>
<script id="code">
// use a V figure instead of MinusLine in the TreeExpanderButton
go.Shape.defineFigureGenerator("ExpandedLine", (shape, w, h) => {
return new go.Geometry()
.add(new go.PathFigure(0, 0.25*h, false)
.add(new go.PathSegment(go.PathSegment.Line, .5 * w, 0.75*h))
.add(new go.PathSegment(go.PathSegment.Line, w, 0.25*h)));
});
// use a sideways V figure instead of PlusLine in the TreeExpanderButton
go.Shape.defineFigureGenerator("CollapsedLine", (shape, w, h) => {
return new go.Geometry()
.add(new go.PathFigure(0.25*w, 0, false)
.add(new go.PathSegment(go.PathSegment.Line, 0.75*w, .5 * h))
.add(new go.PathSegment(go.PathSegment.Line, 0.25*w, h)));
});
// Perform a TreeLayout where commitLayers is overridden to modify the background Part whose key is "_BANDS".
class BorderedTreeLayout extends go.TreeLayout {
constructor() {
super();
this.layerStyle = go.TreeLayout.LayerUniform; // needed for straight layers
}
commitLayers(layerRects, offset) {
// update the background object holding the visual "bands"
var bands = this.diagram.findPartForKey("_BANDS");
if (bands) {
const orig = this.arrangementOrigin.copy().add(offset);
bands.location = new go.Point(orig.x - 0.5, orig.y - 0.5);
const border = bands.findObject("BORDER");
const horiz = bands.findObject("HORIZONTALS");
const vert = bands.findObject("VERTICALS");
let x = 0; let y = 0;
let w = 0; let h = 0;
this.network.vertexes.each(v => {
if (v.node) {
x = Math.min(x, v.bounds.x);
y = Math.min(y, v.bounds.y);
w = Math.max(w, v.bounds.right);
h = Math.max(h, v.bounds.bottom);
}
});
w = w-x-offset.x;
h = h-y-offset.y;
if (border) { // define border around everything
border.width = w;
border.height = h;
}
if (horiz) { // add horizontal separator lines
let fig = null;
this.network.vertexes.each(v => {
if (v.node) {
if (fig) {
fig.add(new go.PathSegment(go.PathSegment.Move, 0, v.bounds.y));
fig.add(new go.PathSegment(go.PathSegment.Line, w, v.bounds.y));
} else { // don't draw line before first node
fig = new go.PathFigure(0, 0);
}
}
});
horiz.geometry = new go.Geometry().add(fig);
}
if (vert) { // add vertical separator lines
let fig = new go.PathFigure(64, 0);
fig.add(new go.PathSegment(go.PathSegment.Move, 100, 0));
fig.add(new go.PathSegment(go.PathSegment.Line, 100, h));
fig.add(new go.PathSegment(go.PathSegment.Move, 128, 0));
fig.add(new go.PathSegment(go.PathSegment.Line, 128, h));
vert.geometry = new go.Geometry().add(fig);
}
}
}
}
// end BandedTreeLayout
// Since 2.2 you can also author concise templates with method chaining instead of GraphObject.make
// For details, see https://gojs.net/latest/intro/buildingObjects.html
const $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram =
$(go.Diagram, "myDiagramDiv",
{
allowMove: false,
allowCopy: false,
allowDelete: false,
allowHorizontalScroll: false,
layout:
$(BorderedTreeLayout,
{
alignment: go.TreeLayout.AlignmentStart,
angle: 0,
compaction: go.TreeLayout.CompactionNone,
layerSpacing: 16,
layerSpacingParentOverlap: 1.0,
nodeIndentPastParent: 1.0,
nodeSpacing: 0,
setsPortSpot: false,
setsChildPortSpot: false
})
});
myDiagram.nodeTemplateMap.add("BANDS",
$(go.Part,
{ layerName: "Grid", isLayoutPositioned: false },
$(go.Shape, { name: "BORDER", fill: null, stroke: "red" }),
$(go.Shape, { name: "HORIZONTALS", fill: null, stroke: "green" }),
$(go.Shape, { name: "VERTICALS", fill: null, stroke: "blue" })
));
myDiagram.nodeTemplate =
$(go.Node,
{ // no Adornment: instead change panel background color by binding to Node.isSelected
selectionAdorned: false,
// a custom function to allow expanding/collapsing on double-click
// this uses similar logic to a TreeExpanderButton
doubleClick: (e, node) => {
var cmd = myDiagram.commandHandler;
if (node.isTreeExpanded) {
if (!cmd.canCollapseTree(node)) return;
} else {
if (!cmd.canExpandTree(node)) return;
}
e.handled = true;
if (node.isTreeExpanded) {
cmd.collapseTree(node);
} else {
cmd.expandTree(node);
}
}
},
$("TreeExpanderButton",
{ // customize the button's appearance
"_treeExpandedFigure": "ExpandedLine",
"_treeCollapsedFigure": "CollapsedLine",
"ButtonBorder.fill": "whitesmoke",
"ButtonBorder.stroke": null,
"_buttonFillOver": "rgba(0,128,255,0.25)",
"_buttonStrokeOver": null
}),
$(go.Panel, "Horizontal",
{ position: new go.Point(18, 0), margin: new go.Margin(0.5, 0.5) }, // room for separators
new go.Binding("background", "isSelected", s => s ? "lightblue" : "white").ofObject(),
$(go.Picture,
{
width: 18, height: 18,
margin: new go.Margin(0, 4, 0, 0),
imageStretch: go.GraphObject.Uniform
},
// bind the picture source on two properties of the Node
// to display open folder, closed folder, or document
new go.Binding("source", "isTreeExpanded", imageConverter).ofObject(),
new go.Binding("source", "isTreeLeaf", imageConverter).ofObject()),
$(go.TextBlock,
{ font: '9pt Verdana, sans-serif' },
new go.Binding("text", "key", s => "item " + s))
) // end Horizontal Panel
); // end Node
// without lines
myDiagram.linkTemplate = $(go.Link);
// create a random tree
var nodeDataArray = [{ key: 0 }];
var max = 499;
var count = 0;
while (count < max) {
count = makeTree(3, count, max, nodeDataArray, nodeDataArray[0]);
}
// add a special "_BANDS" object for the background grid
nodeDataArray.unshift({key:"_BANDS", category: "BANDS"});
myDiagram.model = new go.TreeModel(nodeDataArray);
function makeTree(level, count, max, nodeDataArray, parentdata) {
var numchildren = Math.floor(Math.random() * 10);
for (var i = 0; i < numchildren; i++) {
if (count >= max) return count;
count++;
var childdata = { key: count, parent: parentdata.key };
nodeDataArray.push(childdata);
if (level > 0 && Math.random() > 0.5) {
count = makeTree(level - 1, count, max, nodeDataArray, childdata);
}
}
return count;
}
// takes a property change on either isTreeLeaf or isTreeExpanded and selects the correct image to use
function imageConverter(prop, picture) {
var node = picture.part;
if (node.isTreeLeaf) {
return "images/document.svg";
} else {
if (node.isTreeExpanded) {
return "images/openFolder.svg";
} else {
return "images/closedFolder.svg";
}
}
}
</script>
</body></html>