'Add hyperlink to nodes in amcharts Force Directed Tree
I have this amcharts 4 Force Directed Tree as a map for different pages I have on my website. What I want (and could not figure out how despite searching a lot) is that when a user clicks on a node they go to the webpage corresponding to that node. In short, I wonder if I can have a clickable name for nodes? I greatly appreciate any helps. Here is the JavaScript code:
am4core.useTheme(am4themes_dark);
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4plugins_forceDirected.ForceDirectedTree);
var networkSeries = chart.series.push(new am4plugins_forceDirected.ForceDirectedSeries())
networkSeries.nodes.template.outerCircle.filters.push(new am4core.DropShadowFilter());
networkSeries.dataFields.linkWith = "linkWith";
networkSeries.dataFields.name = "name";
networkSeries.dataFields.id = "name";
networkSeries.dataFields.value = "value";
networkSeries.dataFields.children = "children";
networkSeries.dataFields.color = "color";
networkSeries.dataFields.fixed = "fixed";
networkSeries.nodes.template.propertyFields.x = "x";
networkSeries.nodes.template.propertyFields.y = "y";
networkSeries.links.template.strength = 1;
networkSeries.manyBodyStrength = -20;
networkSeries.centerStrength = 0.4;
networkSeries.nodes.template.label.text = "{name}"
networkSeries.fontSize = 16;
networkSeries.minRadius = 40;
networkSeries.maxRadius = 80;
var nodeTemplate = networkSeries.nodes.template;
nodeTemplate.fillOpacity = 1;
nodeTemplate.label.hideOversized = true;
nodeTemplate.label.truncate = true;
var linkTemplate = networkSeries.links.template;
linkTemplate.strokeWidth = 5;
linkTemplate.distance = 1.5;
nodeTemplate.events.on("out", function (event) {
var dataItem = event.target.dataItem;
dataItem.childLinks.each(function (link) {
link.isHover = false;
})
})
networkSeries.events.on("inited", function() {
networkSeries.animate({
property: "velocityDecay",
to: 0.7
}, 3000);
});
networkSeries.data = [
{
"name":"Complex Networks",
"value":1000,
"color":"#0086ad",
"fixed": true,
x: am4core.percent(50),
y: am4core.percent(10),
"children":[
{
"name":"Theory \n and Model",
"value":600,
"color":"#0086ad",
"fixed": true,
x: am4core.percent(20),
y: am4core.percent(20),
"children":[
{
"name":"Statistical \n Physics \n Approach",
"value":600,
"color":"#36896E"
},
{
"name":"Balance Theory \n Approach",
"value":600,
"color":"#36896E"
},
{
"name":"Topological \n Data \n Analysis",
"value":400,
"color":"#36896E"
},
{
"name": "Aged \n Networks",
"value": 200,
"color":"#36896E"
},
{
"name": "Dark \n Networks",
"value": 100,
"color":"#36896E"
}
]
},
{
"name":"Application \n and Real-data",
"color":"#0086ad",
"fixed": true,
x: am4core.percent(80),
y: am4core.percent(20),
"value":600,
"children":[
{
"name": "Cancer \n Project",
"value": 100,
"color":"#36896E",
"linkWith":[
"Balance Theory \n Approach"
]
},
{
"name": "Social Data",
"value": 200,
"color":"#8b225b",
"fixed": true,
x: am4core.percent(80),
y: am4core.percent(33),
"children":[
{"name":"Twitter \n Project",
"value":200,
"color":"#36896E"},
{"name":"Complex \n Social \n Systems",
"value":300,
"color":"#36896E"}
]
}
]
}
]
},
{
"name":"Stochastic \n Process",
"value":200,
"color":"#0086ad",
"fixed":true,
x: am4core.percent(50),
y: am4core.percent(45),
"linkWith":[
"Financial \n Markets",
"Application \n and Real-data"
]
},
{
"name":"Network \n Neuroscience \n and Cognition",
"value":500,
"fixed": true,
x: am4core.percent(35),
y: am4core.percent(45),
"linkWith":[
"Balance Theory \n Approach",
"Application \n and Real-data",
"Topological \n Data \n Analysis",
],
"children":[
{
"name":"Resting-state \n fMRI Networks",
"value":600,
"color":"#36896E",
"url":"https://ccnsd.ir/research_projects/brain_networks/"
}
]
},
{
"name":"Econo-physics",
"value":500,
"fixed": true,
x: am4core.percent(65),
y: am4core.percent(45),
"linkWith":[
"Theory \n and Model",
"Application \n and Real-data"
],
"children":[
{
"name":"Quantum \n Economics",
"value":100,
"color":"#36896E"
},
{
"name":"Financial \n Markets",
"value":100,
"children":[
{"name": "Cryptocurrency",
"value": 500,
"color":"#36896E"},
{"name": "Stock \n Markets",
"value": 500,
"color":"#36896E"}
]
}
]
}
];
And the HTML:
<head>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
body { background-color: #30303d; color: #fff; }
#chartdiv {
width: 100%;
height: 1300px;
}
</style>
</head>
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<script src="https://cdn.amcharts.com/lib/4/plugins/forceDirected.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/dark.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
Thanks in advance!
Solution 1:[1]
You can put click event in each node and use the following code. Please take note that I used amchart 5 version.
series.nodes.template.events.on("click", function (e) {
switch (e.target.dataItem.dataContext.name) {
case 'Econo-physics':
window.open("https://stackoverflow.com/questions/68037180/add-hyperlink-to-nodes-in-amcharts-force-directed-tree");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
