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
// celestial bodies
6
// with their diameter in kilometers
7
"nodes": [
8
{ "id": "Sun", "value": 1392700, "fill": "#FFC107", "height": 60 },
9
{ "id": "Mercury", "value": 4879, "fill": "#C0C0C0", "height": 14 },
10
{ "id": "Venus", "value": 12104, "fill": "#FFDEAD", "height": 20 },
11
{ "id": "Earth", "value": 12756, "fill": "#0000FF", "height": 20 },
12
{ "id": "Moon", "value": 3474, "fill": "#D3D3D3", "height": 13 },
13
{ "id": "Mars", "value": 6779, "fill": "#FF0000", "height": 16 },
14
{ "id": "Phobos", "value": 22, "fill": "#A0522D", "height": 8 },
15
{ "id": "Deimos", "value": 12, "fill": "#A0522D", "height": 8 },
16
{ "id": "Jupiter", "value": 142984, "fill": "#DEB887", "height": 35 },
17
{ "id": "Io", "value": 3643, "fill": "#FFD700", "height": 13 },
18
{ "id": "Europa", "value": 3122, "fill": "#E6E6FA", "height": 13 },
19
{ "id": "Ganymede", "value": 5262, "fill": "#D2B48C", "height": 15 },
20
{ "id": "Callisto", "value": 4821, "fill": "#BDB76B", "height": 14 },
21
{ "id": "Saturn", "value": 116464, "fill": "#FFDAB9", "height": 25 },
22
{ "id": "Titan", "value": 5150, "fill": "#FFA07A", "height": 15 },
23
{ "id": "Rhea", "value": 1527, "fill": "#F0E68C", "height": 11 },
24
{ "id": "Uranus", "value": 50724, "fill": "#ADD8E6", "height": 25 },
25
{ "id": "Titania", "value": 1578, "fill": "#87CEEB", "height": 11 },
26
{ "id": "Oberon", "value": 1523, "fill": "#87CEEB", "height": 11 },
27
{ "id": "Neptune", "value": 49244, "fill": "#00008B", "height": 25 },
28
{ "id": "Triton", "value": 2706, "fill": "#00BFFF", "height": 12 }
29
],
30
31
// connections between celestial bodies
32
// with the distance between them in million kilometers
33
"edges": [
34
{ "from": "Sun", "to": "Mercury", "distance": 50.9 },
35
{ "from": "Sun", "to": "Venus", "distance": 108.2},
36
{ "from": "Sun", "to": "Earth", "distance": 148.7 },
37
{ "from": "Earth", "to": "Moon", "distance": 0.3877 },
38
{ "from": "Sun", "to": "Mars", "distance": 207.6 },
39
{ "from": "Mars", "to": "Phobos", "distance": 0.00567 },
40
{ "from": "Mars", "to": "Deimos", "distance": 0.02006},
41
{ "from": "Sun", "to": "Jupiter", "distance": 748.5 },
42
{ "from": "Jupiter", "to": "Io", "distance": 0.3484 },
43
{ "from": "Jupiter", "to": "Europa", "distance": 0.6025 },
44
{ "from": "Jupiter", "to": "Ganymede", "distance": 0.9961 },
45
{ "from": "Jupiter", "to": "Callisto", "distance": 1.81 },
46
{ "from": "Sun", "to": "Saturn", "distance": 1450 },
47
{ "from": "Saturn", "to": "Titan", "distance": 1.17 },
48
{ "from": "Saturn", "to": "Rhea", "distance": 0.4667 },
49
{ "from": "Sun", "to": "Uranus", "distance": 2930 },
50
{ "from": "Uranus", "to": "Titania", "distance": 0.4095 },
51
{ "from": "Uranus", "to": "Oberon", "distance": 0.5559 },
52
{ "from": "Sun", "to": "Neptune", "distance": 4470 },
53
{ "from": "Neptune", "to": "Triton", "distance": 0.3287 }
54
]
55
};
56
57
// create a network graph and set the data
58
var chart = anychart.graph(data);
59
60
// enable node labels
61
var nodes = chart.nodes();
62
nodes.labels().enabled(true);
63
// customize node labels
64
nodes.labels().fontSize(15);
65
nodes.labels().fontColor("black")
66
// customize node tooltips
67
nodes.tooltip().useHtml(true);
68
nodes.tooltip().format(function(e){
69
const name = e.getData("id");
70
const value = e.getData("value");
71
return `<b>${name}<br>${value}</b> km in diameter`
72
});
73
74
// customize edge tooltips
75
var edges = chart.edges();
76
edges.tooltip().useHtml(true);
77
edges.tooltip().format(function(e){
78
const from = e.getData("from");
79
const to = e.getData("to");
80
const distance = e.getData("distance") * 1000000;
81
return `From <b>${from}</b> to <b>${to}<br>${distance}</b> km`
82
})
83
// customize the edge stroke
84
edges.stroke("lightblue", 2, "10 5")
85
86
// set the chart title
87
chart.title("Solar System Network Graph");
88
89
// set the container id
90
chart.container("container");
91
92
// initiate drawing the chart
93
chart.draw();
94
95
});