I am writing a graph display program using D3, and I found a problem in which Microsoft Edge does not cope with edge scaling. My code is below:
HTML:
<svg id="container" width="200" height="200"> <g id="inner-container"> </g> </svg>
JS:
var container = d3.select("#container"); var innerContainer = container.select("g"); container.call(d3.behavior.zoom().scaleExtent([.5,4]).on("zoom", function(){ innerContainer.attr("transform", "translate(" + d3.event.translate + ") scale(" + d3.event.scale + ")"); })); var nodes = innerContainer.append("g").selectAll(".graph-node"); var labels = innerContainer.append("g").selectAll(".graph-label"); var data = ["A", "B", "C", "D", "E"]; nodes.data(data).enter().call(function (selection){ selection = selection.append("g"); selection.attr("class", ".graph-node"); selection.attr("transform", function(d, i){ return ["translate(", (i * 20).toString(), " ", (i * 20).toString(), ")"].join(""); }); selection.append("rect") .attr("width", 20) .attr("height", 20) .attr("rx", 2).attr("ry", 2) .attr("fill", "red"); selection.append("text") .text(function(d){ return d; }) .attr("text-anchor", "middle") .attr("x", 10).attr("y", 15) .style("fill", "white"); }); labels.data(data).enter().call(function (selection){ selection = selection.append("g"); selection.attr("class", "graph-label"); selection.append("text") .text(function (d) { return "Node" + d; }); selection.attr("transform", function (d, i) { return ["translate(", (i * 20 + 25).toString(), " ", (i * 20 + 15).toString(), ")"].join(""); }); });
View in JSFiddle
In Google Chrome, IE, and Firefox, scaling works exactly as expected. However, when I run this code in Microsoft Edge, the text labels on top of the nodes will shift to the right when I zoom in, completely disappearing when they leave the rectangle. The problem disappears when I do not install text-anchor , but this will mean that I have to consider the location of the text manually, especially considering when the site is used internationally. (Text characters themselves do not matter, since they are from a custom font in the final product).
How can I get around this by creating a label with the text centered and still saving it correctly on Edge?