Introduction
In this tutorial, you will learn the basics of how to change edge styles, anchors, and endpoints.
There are three built in edge styles in thinkmap. The first and most common is a linear edge style which is ideal for the spider layout. The next most popular edge style is curved, which is typically used for left/right and radial trees. Lastly, the third built in edge style is piped. Piped edges should only be used in left/right and top/down trees, typically with hierarchy data.
Anchors are functions used to determine the location where edges connect to their nodes in the display. They are attached directly to the edge styles we defined earlier. There are nearly 10 built in anchors that come with thinkmap.
Endpoints, more commonly known as arrows, are the markers that can be used at the either end of an edge. There is one default type of arrow style in Thinkmap, although it is possible to create your own.
If you view the source of this page, you can see the code that makes up this example in its entirety, but we'll go through it section-by-section here.
Getting Started
If you view the source of this example, you will notice that we are linking to two important source files:
The first file is the thinkmap.js file for your project. The second file edges-anchors.js contains
the code for this example, which we will now discuss.
We start first by creating a few graphs, attaching a datasource, setting up a layout, and configuring the display.
// build three graphs
var graph = new thinkmap.graph.Graph();
var tree = new thinkmap.graph.Tree();
var display = new thinkmap.display.Display(document.getElementById('map'));
// attach a datasource to the main graph
graph.datasource('/moviedb.tas');
// configure the layout
var layout = thinkmap.layout.treeLayout(display).tree(tree).spacing(250);
// configure the display
configureDisplay();
// setup the node templates
makeTemplates();
Using Edge Styles
An edge style is just a predefined template that handles the drawing of an edge between two nodes. All edge styles are defined with this general pattern, which is the name of the edgestyle module:
thinkmap.display.edgestyle.
From there we define which edgestyle of the package we want:
thinkmap.display.edgestyle.linear();
thinkmap.display.edgestyle.curved();
thinkmap.display.edgestyle.piped();
However, we need to associate the edgestyle with the template we wish to use it with:
display.template('movie_genre').as(thinkmap.display.edgestyle.linear());
display.template('movie_director').as(thinkmap.display.edgestyle.curved());
display.template('movie_actor').as(thinkmap.display.edgestyle.piped());
Using Anchors with Edge Styles
So what are anchors? An anchor serves as a location for where an edge can visually attach to a node. They can range from simple to incredibly complex. Most users will
use one of the built in anchors defined in the anchor module/package thinkmap.display.anchor
We attach anchors to edges using the anchorFrom() and anchorTo() methods on edgestyles
display.template('edge').as(
thinkmap.display.edgestyle.linear()
.anchorFrom(thinkmap.display.anchor.center())
.anchorTo(thinkmap.display.anchor.center())
);
There are currently 8 built in anchor types, but lets start first with the most basic:
thinkmap.display.anchor.center()thinkmap.display.anchor.north()thinkmap.display.anchor.east()thinkmap.display.anchor.south()thinkmap.display.anchor.west()
These anchors place the edge at the center, north, east, south, or west points of the actual node. You can notice this behavior by changing the anchor style value in the drop down to your right.
Now lets discuss these two more advanced anchors
thinkmap.display.anchor.eastWest()-
thinkmap.display.anchor.northSouth()
These two anchors place the edges endpoint nearest the respective horizontal or vertical anchor. For instance, in the case
of a left to right tree, we would want to use the thinkmap.display.anchor.eastWest() anchor as we only want an edge to be connected to the nearest
horizontal port. Typically you will only use the eastWest and northSouth anchors for horizontal and vertical trees.
The last anchor we will discuss is the bounds anchor.thinkmap.display.anchor.bounds()
This anchor places an edge directly at the intersection point of a node. This is a complicated anchor that uses trigonometry to determine where an edge will be placed directly on a shape. It has support for the built in shapes such as circles, rectangles, and lines but does not support complicated SVG paths. In addition, if there are a lot of entities on a screen that are constantly in motion, it is best to avoid using this anchor so overall performance doesn't suffer.
Adding Endpoints into the Mix
Endpoints are added to edges in a very similar manner as anchors. We use the endpoint module and the endpointFrom and endpointTo methods of an edgestyle.
display.template('edge').as(
thinkmap.display.edgestyle.linear()
.anchorFrom(thinkmap.display.anchor.center())
.anchorTo(thinkmap.display.anchor.center())
.endpointFrom(thinkmap.display.endpoint.arrow())
.endpointTo(thinkmap.display.endpoint.arrow())
);
In the case of "from" and "to", much like the anchors, we are referring to the nodes and direction of the edge. Assigning only an endpointFrom will result in the edge having an arrow that points to the "from" node.
Each endpoint adds new svg elements to each edge entity that uses that particular display template. Each endpoint element will be given the class
endpointFrom and endpointTo respectively, allowing
one to select them using JavaScript and to style them with CSS.
.endpointFrom {
fill: none;
}
.endpointTo {
fill: #FF0000;
}
Putting it all Together
This example uses a function changeEdge that takes the values from the drop
down menus to come up with the edges, anchors, and endpoints used.
function changeEdge() {
// get the values of the select menus
var edgeStyle = document.getElementById('edgeStyle').value;
var anchorStyle = document.getElementById('anchorStyle').value;
var endpointStyle = document.getElementById('endpointStyle').value;
// declare variables that will hold the selected edge style and anchor
var edge, anchor;
// choose the correct edge style based on the edgeStyle value
if (edgeStyle == 'curved') {
edge = thinkmap.display.edgestyle.curved();
} else if (edgeStyle == 'piped') {
edge = thinkmap.display.edgestyle.piped();
} else {
// default to linear
edge = thinkmap.display.edgestyle.linear();
}
// now pick an anchor for the edge style
if (anchorStyle == 'north') {
anchor = thinkmap.display.anchor.north();
} else if (anchorStyle == 'south') {
anchor = thinkmap.display.anchor.south();
} else if (anchorStyle == 'east') {
anchor = thinkmap.display.anchor.east();
} else if (anchorStyle == 'west') {
anchor = thinkmap.display.anchor.west();
} else if (anchorStyle == 'eastWest') {
anchor = thinkmap.display.anchor.eastWest();
} else if (anchorStyle == 'northSouth') {
anchor = thinkmap.display.anchor.northSouth();
} else if (anchorStyle == 'bounds') {
anchor = thinkmap.display.anchor.bounds();
} else {
// default to the center
anchor = thinkmap.display.anchor.center();
}
// add the anchor to the edge, using the same for both ends
edge.anchorFrom(anchor).anchorTo(anchor);
// now append an endpoint based on the selected value
if (endpointStyle == 'from') {
edge.endpointFrom(thinkmap.display.endpoint.arrow());
} else if (endpointStyle == 'to') {
edge.endpointTo(thinkmap.display.endpoint.arrow());
} else if (endpointStyle == 'both') {
edge.endpointFrom(thinkmap.display.endpoint.arrow())
.endpointTo(thinkmap.display.endpoint.arrow());
}
// now create an edge template with the built up edgestyle
display.template('edge').as(edge);
}
Next, we bind this function to the onchange listeners for the drop down menus
// bind the select menu listeners to the changeEdgeStyle function
document.getElementById('edgeStyle').addEventListener('change', changeEdge);
document.getElementById('anchorStyle').addEventListener('change', changeEdge);
document.getElementById('endpointStyle').addEventListener('change',changeEdge);
We also add some styles for our edge path and the endpoints in our index.html file.
Finally, we make sure in the reset method that we set the initial values for the edge style and anchor.
.edge {
stroke: #647382;
stroke-width:1px;
}
.edge path {
fill: none;
}
.endpointFrom path{
fill: #647382;
}
.endpointTo path{
fill: #647382;
}
function reset(){
...
// set the select menus to their default value
document.getElementById('edgeStyle').value = 'curved';
document.getElementById('anchorStyle').value = 'eastWest';
document.getElementById('endpointStyle').value = 'to';
// change the edge style to the default
changeEdge();
....
}
SUMMARY
In this tutorial, you learned about the basics of edges and anchors and how they are used. You saw how easy it is to switch between different styles of edges and anchors and bind them to a template in the display.