Toggle topojson Layers (reload file)
This example demonstrates a map of world countries that can be toggled between different TopoJSON layers, with the ability to reload the file. Built with D3 v4 and rendered as SVG, it loads geographic data from countries.json and displays it using a custom projection. The visualization uses a button to re-fetch and re-render the TopoJSON data, allowing users to refresh the map layers dynamically.
AI-generated descriptionIn response to a stack overflow question on how to load a topojson file based on a file input.
Click the map to change the source file.
As topojson files often have a filename (without extenstion) that is equal to the name of the property that holds the features, we can use:
d3.json(fileName + ".json", function(error, topo) { ....
... .data(topojson.feature(topo, topo.objects[fileName]).features)
To ensure that these two match, you can check the topojson itself. Though it can be a pain to find. If you can find the word "objects", you can find the property name.