Hello everyone,
I
am new to the group and I wanted to ask a question regarding an issue I
am facing with D3.js and external svg files. I hope this is the right
place for such questions.
I am working on this pie-chart (gist). When one clicks on a path (i.e. the pie's wedges), the div on the right shows an explanation of that function (sorry for the text being all in Italian).
Despite the fact that some icons are missing, this is how I managed to load external svg files.
First, I create a div that contains the image (i.e. svg file), the function's title, and a text that describes the function.
HTML
<div class="description" id="funzioni">
<object id="descrImg" data="" type="image/svg+xml" ></object>
<p><strong><span id="descrTitle">example title</strong></span></p>
<p><span id="descrText">example description text.<span></p>
</div>Second, I made an event listener that changes the div's title, image and description according to the info contained in data.json when a path is clicked.
d3.selectAll("path").on("click", function (d) {
//update tooltip title
d3.select("#descrTitle")
.text(d.data.descrTitle)
//update tooltip text
d3.select("#descrText")
.html(d.data.descrText)
//update tooltip image
d3.select("#descrImg")
.attr("data", function () {return d.data.descrImg;})
})
}Images load correctly. When I inspect the element this is the structure I see:
<object>
<svg>
<circle>
<path></path>
</circle>
</svg>
</object>
The next step would be to change the path's `fill` attribute to white, and the circle's `fill` attribute with the function that assigns colors to the pie's wedges, that is:
var path = chart.datum(dataset).selectAll("path")
.data(pie)
.enter() .append("path") .attr("fill", function (d, i) { //this is the function var scale = colorMap[d.data.categoria]; if (scale) return scale(d.data.catIndex) }) .style("fill-opacity", 0.75) .attr("d", arc) .each(function (d) { this._current = d; }); }
Alternatively, I could make a function that copies the fill attribute from the corresponding wedge (any idea?).
However, before I can do that, I need to "access" the the `circle` and `path` contained in the `object`. How can I do that?
In the console, I tried:
d3.select("#descrImg") //selects the object
.select("svg")
.select("circle")
.attr("fill", "black")
Which doesn't work. However, if I use the inspector to change circle's or path's fill attribute it works fine.
What am I doing wrong?
Hope someone can help.
Best,
Federico
--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
descrImgURI = d.data.descrImg;
d3.xml(descrImgURI, "image/svg+xml", function(error, xml) {
if (error) throw error;
document.body.appendChild(xml.documentElement);
});
d3.xml(descrImgURI, "image/svg+xml", function(error, xml) {
if (error) throw error;
document.getElementById("#myDiv").appendChild(xml.documentElement);
xml.documentElement.attr("fill", "red")
}); descrImg = d3.select("#descrImg"); //creates a variable pointing to the Div where I want the image to appear
descrImg.select("svg").remove(); //removes previous image, if any
descrImgURI = d.data.descrImg; //takes the image URI from the data
d3.xml(descrImgURI, "image/svg+xml", function(error, xml) {
if (error) throw error;
var svg= xml.documentElement;
descrImg.node().appendChild(svg);
svg.style.backgroundColor= "red";
});