I've checked a lot of replies but I can't get any of them to work.
I'm using the d3js interactive map and I'd like to fill each state with a different image.
Using
g.selectAll("#countries")
.append('image')
.attr('xlink:href', 'image-uri')
.attr('width', '500')
.attr('height', '500')
this seems to work, but it won't paint the image just in one of the countries (setting x
and y
is not enough because the country has not a fixed width
or height
and so the image will always overflow)
With
g.selectAll("#US")
.append('image')
.attr('xlink:href', 'image-uri')
.attr('width', '500')
.attr('height', '500')
nothing happens and, by the way, we would have the same problem of the case above.
Since the fill
style property works, I thought to fill the country using an image, with the following code
g.selectAll("#US").style('fill', 'url(urlimage-uri)');
This makes the country just disappear, like if there is no image to paint (the image uri il valid and working)
I've been struggling with this for days and I can't seem to find a solution to this problem.
Is there something that I'm missing?
EDIT:
I'm notrying to fill them with a static image but I need them to be filled by images coming from a json source.
Using
<svg id="mySvg" width="80" height="80">
<defs id="mdef">
<pattern id="image" x="0" y="0" height="40" width="40">
<image x="0" y="0" width="40" height="40" xlink:href="http://www.e-pint.com/epint.jpg"></image>
</pattern>
</defs>
</svg>
works but I've no idea on how to set a different image for each country.
The upper solution works just using
g.selectAll("#countries").style('fill', 'url(#image)')
but going with
g.selectAll("#US").style('fill', 'url(#image)')
makes the country disappear
EDIT:
This is what I'm trying to add patterns dynamically
d3.json("json/world-top/latest.json", function(error, us) {
var defs = map.svg.append('svg:defs');
for (var i = 0; i < us.length; i++) {
defs.append('svg:pattern')
.attr('id', function () { return '#image_' + us[i].track_name; })
.attr('patternUnits', 'userSpaceOnUse')
.attr('width', '6')
.attr('height', '6')
.append('svg:image')
.attr('xlink:href', function(){return us[i].artwork_url;})
.attr('x', 0)
.attr('y', 0)
.attr('width', 6)
.attr('height', 6);
}
});