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

问题描述 投票:0回答:1

我有一个页面,我建立了一堆饼图。在每一个上,我想将href添加到页面上的另一个位置。

目前我的代码有效,但它只将href应用于饼图的各个部分以及文本。因此,例如,如果您单击饼图的一个环,它将按照它应该的方式工作,但如果您单击环之间的空间,它将不会。

SVG本身更大,更容易点击,但即使我将锚标记附加到svg,它也只适用于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);

}
javascript d3.js svg href
1个回答
1
投票

我认为你只是选择错误的方式。你想让svga标签内吗?:

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

你(1)选择pieChart,然后(2)向它附加一个a标签,然后你(3)将svg附加到那个。

© www.soinside.com 2019 - 2024. All rights reserved.