我如何将 href 应用于整个 svg?

How would I apply an href to the ENTIRE svg?

我有一个页面,我在其中构建了一堆饼图。在每一个上,我都想在页面上的另一个位置添加一个 href。

目前我的代码 有效 ,但它仅将 href 应用于饼图的各个部分以及文本。因此,例如,如果您单击饼图的环,它会正常工作,但如果您单击 space 环之间,它不会.

SVG 本身更大且更容易点击,但即使我将锚标记附加到 svg,它也仅适用于 within SVG 的元素。我该如何纠正这种行为?

function pieChartBuilder(teamName, values) {
    var dataset = values;

    var trimTitle = teamName.replace(' ', '');
    trimTitle = trimTitle.toLowerCase();

    var width = 175,
        height = 175,
        cwidth = 8;

    var color = d3.scale.category20();

    var pie = d3.layout.pie()
        .sort(null);

    var arc = d3.svg.arc();

    var svg = d3.select("#pieChart").append("svg")
        .attr("width", width)
        .attr("height", height)
        .append("a") // here is where I append the anchor tag to the SVG, but it only applies to the individual elements within.
        .attr("href", ("#" + trimTitle))
        .append("g")
        .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")

    var gs = svg.selectAll("g").data(d3.values(dataset)).enter().append("g");
    var path = gs.selectAll("path")
        .data(function (d) { return pie(d); })
        .enter().append("path")
        .attr("fill", function (d, i) { return color(i); })
        .attr("d", function (d, i, j) { return arc.innerRadius(5 + cwidth * j).outerRadius(3 + cwidth * (j + 1))(d); });

    svg.append("text")
        .attr("class", "title")
        .attr("x", 0)
        .attr("y", (0 - (height / 2.5)))
        .attr("text-anchor", "middle")
        .style("fill", "#808080")
        .text(teamName);

}

我认为你只是 select 或者方向错了。您想在 a 标签中包含 svg 吗?:

d3.select("#pieChart")
  .append("a")
  .append("svg");

您 (1) select pieChart,然后 (2) 向其附加 a 标签,然后您 (3) 向其附加 svg .