HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create data
4
var data = {
5
nodes: [
6
{id: "Richard", group: "family", last_name: "Roe"},
7
{id: "Larry", group: "family", last_name: "Loe"},
8
{id: "Marta", group: "family", last_name: "Moe"},
9
{id: "Jane", group: "friends", last_name: "Poe"},
10
{id: "Norma", group: "friends", last_name: "Noe"},
11
{id: "Frank", group: "friends", last_name: "Foe"},
12
{id: "Brett", group: "friends", last_name: "Boe"},
13
{id: "Tommy", group: "lone wolf", last_name: "Toe"}
14
],
15
edges: [
16
{from: "Richard", to: "Larry"},
17
{from: "Richard", to: "Marta"},
18
{from: "Larry", to: "Marta"},
19
{from: "Marta", to: "Jane"},
20
{from: "Jane", to: "Norma"},
21
{from: "Jane", to: "Frank"},
22
{from: "Jane", to: "Brett"},
23
{from: "Brett", to: "Frank"}
24
]
25
};
26
27
// create a chart and set the data
28
var chart = anychart.graph(data);
29
30
// prevent zooming the chart with the mouse wheel
31
chart.interactivity().zoomOnMouseWheel(false);
32
33
// enable labels of nodes
34
chart.nodes().labels().enabled(true);
35
36
// configure labels of nodes
37
chart.nodes().labels().format(function() {
38
if (this.siblings.length > 2) {
39
return this.id.toUpperCase();
40
} else {
41
if (this.siblings.length == 0) {
42
return this.id.toUpperCase() + "\n(" + this.getData("group") + ")";
43
} else {
44
return "";
45
}
46
}
47
});
48
chart.nodes().labels().fontSize(12);
49
chart.nodes().labels().fontWeight(600);
50
51
// configure labels of nodes in groups
52
chart.group("family").labels().fontColor("#00bfa5");
53
chart.group("friends").labels().fontColor("#ffa000");
54
chart.group("lone wolf").labels().fontColor("#dd2c00");
55
56
// configure tooltips
57
chart.tooltip().useHtml(true);
58
chart.tooltip().format(function() {
59
if (this.type == "node") {
60
return "<span style='font-weight:bold'>" +
61
this.id + " " + this.getData("last_name") +
62
"</span><br><br>siblings: " + this.siblings.length +
63
"<br>group: " + this.getData("group");
64
} else {
65
return this.getData("from") + " -> " + this.getData("to");
66
}
67
});
68
69
// set the chart title
70
chart.title("Network Graph: Labels and Tooltips (Formatting Functions)");
71
72
// set the container id
73
chart.container("container");
74
75
// initiate drawing the chart
76
chart.draw();
77
});