lnNoam lnNoam - 1 year ago 99
Javascript Question

Append SVG items on top of existing elements

I'm trying to add points to a map using the

(I've tried to adapt a script from this gist).

The problem is that the map is being rendered on top of the dots.
I've tried to use another SO answer which suggested something like this:
svg.append("g").attr("id", "map").attr("id", "points")
...but I have been unable to make it work. I'm a long time python user...JavaScript is new for me (so please excuse the naivety).

Am using the following CSS:

body {
background-color: white;
svg {
border: 1px solid black;
background-color: #a4bac7;
.land {
fill: #d7c7ad;
stroke: #8999ab;
.boundary {
fill: none;
stroke: #a5967f;


<script type="text/javascript" src="d3/d3.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>

var width = 960;
var height = 480;
var dataURL = "https://gist.githubusercontent.com/abenrob/787723ca91772591b47e/raw/8a7f176072d508218e120773943b595c998991be/world-50m.json";

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

In response to this SO answer answer, added this:

.attr("id", "map")
.attr("id", "points");

var projection = d3.geoEquirectangular()

var path = d3.geoPath()

d3.json(dataURL, function(error, world) {
.attr("class", "land")
.data([topojson.object(world, world.objects.land)])
.attr("d", path);
.attr("class", "boundary")
.data([topojson.object(world, world.objects.countries)])
.attr("d", path);

var lngLatExtract = function(d, i){
return projection([d['lng'], d['lat']])[i];

d3.csv("cities.csv", function(data){
.attr("cx", function(d){
return lngLatExtract(d, 0);
.attr('cy', function(d){
return lngLatExtract(d, 1);
.style("fill", "blue")
.style("opacity", 0.75)
.attr("r", function(d){
return Math.sqrt(parseInt(d['population']) * 0.000001)


looks like this:

1,New York city,8175133,40.71455,-74.007124
2,Los Angeles city,3792621,34.05349,-118.245323

Answer Source

Both d3.json and d3.csv are async functions. The order in which their callbacks fire is not a given and in fact since your csv file is smaller, it's callback is probably firing first. That means that your svg.append("g") in your csv callback is happening before the svg.append("g") in the json callback. You can confirm this by inspecting the DOM and to see which was appended first.

That said, I would switch to using d3.queue. This allows you to fire off both the d3.json and d3.csv async requests and then fire a single callback with both are complete.

Another less streamlined option is to place your d3.csv code in the callback of the d3.json call.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download