You don't need the "
around the "function(d) { return y(d."+city+"); }"
-- that tells javascript to interpret the text function(d) { return y(d.
and ); }
as a string, giving a y
attribute of function(d) { return y(d.london); }
. You want to perform the function, rather than just quote it.
To access the data for the city, you need to access
d['london']
from the object d
(it can also be written as d.london
). Since city
is a variable storing the name of the current city, you can do this using
d[city]
The whole line is thus
.y(function(d) { return y(d[city]); });