Skip to content

Instantly share code, notes, and snippets.

@patricoferris
Created September 21, 2018 14:22
Show Gist options
  • Save patricoferris/0b492cbf1c9e073b5748d5eb8be62b03 to your computer and use it in GitHub Desktop.
Save patricoferris/0b492cbf1c9e073b5748d5eb8be62b03 to your computer and use it in GitHub Desktop.
//We first perform a selectAll on the circles of the SVG (i.e. the nodes of our graph)
//And if a node is clicked, we perform the function supplying that nodes values as an argument
svg.selectAll("circle").on("click", function(d){
//We first create a string for the country's name of the node clicked
let name = "Country: " + d.name.toUpperCase();
//We then set up the exporters string
let string = "Exporters: ";
//From our predefined object containing key-value pairs of linked nodes we check to see what our node is linked to
//To make the linkedByIndex object we iterated over the links of the graph and placed them in it
Object.keys(linkedByIndex[d.index]).forEach(key => {
for(n of graph.nodes) {
//Sometimes we might not have parsed the data perfectly so we need to do some sanity checks
if(n.index == key && n.name != undefined) {
//If everything is code we add it to the string
string += (n.name.toUpperCase() + ", ");
}
}
});
//And then manipulate the DOM using some vanilla JS
let country = document.getElementById("country");
country.innerHTML = name;
let exporters = document.getElementById("exporters");
exporters.innerHTML = string;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment