【问题标题】:Appending div to circle in d3.js将 div 附加到 d3.js 中的圆圈
【发布时间】:2014-05-26 15:22:09
【问题描述】:

我有一个 d3.js 的甜甜圈条,我想在它的中心放置一些信息。我可以附加文本元素,但我想在那里放置一个格式化的信息,所以我决定在鼠标悬停时添加 div:

$(".arc").on("mouseover",(function(){
d3.select("text").remove();
var appendingString="<tspan>"+cityName[$(this).attr("id")]+"</tspan> <tspan>"+$(this).attr("id")+"%</tspan>";
group
.append("text")
.attr("x",-30)
.attr("y",-10)
.text(appendingString);
})); 

由于某种原因,div 成功添加了我需要但未显示的信息。附加它的正确方法是什么,还是有一些替代方法? 如果需要,完整的脚本:

<script>
var cityNames=["Челябинск","Область","Миасс","Копейск"];
var cityPercentage=[50,30,20,10];
var width=300,
    height=300,
    radius=100;
var color=d3.scale.linear()
            .domain([0,60])
            .range(["red","blue"]);
var cityDivision = d3.select("#cities")
            .append("svg")
            .attr("width", width)
            .attr("height", height)
            .attr("class","span4");
var group=cityDivision.append("g")
.attr("transform","translate(" + width / 2 + "," + height / 2 + ")");
var arc=d3.svg.arc()
    .innerRadius(radius-19)
    .outerRadius(radius);
var pie= d3.layout.pie()
    .value(function(d){return d;});
var cityName={
    50:"Челябинск",
    30:"Область",
    20:"Миасс",
    10:"Копейск"
}
var arcs=group.selectAll(".arc")
    .data(pie(cityPercentage))
    .enter()
    .append("g")
    .attr("class","arc")
    .attr("id",function(d){return d.data;});
    arcs.append("path")
    .attr("d",arc)
    .attr("fill",function(d){return color(d.data);});
//Добавление надписи в центре
    group
    .append("circle")
    .style("fill","white")
    .attr("r",radius-20);
$(".arc").on("mouseover",(function(){
    d3.select("div.label").remove();
    var appendingString=cityName[$(this).attr("id")]+"\n "+$(this).attr("id")+"%";
    group
    .append("div")
    .attr("class","label")
    .html(appendingString);
}));
</script>

【问题讨论】:

    标签: javascript jquery html css d3.js


    【解决方案1】:

    您不能将div 直接注入svg 元素。您有两种选择:

    1. 使用text 元素,然后在其中使用tspan 元素对其进行格式化。这很麻烦,但保证可以与任何支持 SVG 的浏览器一起使用。
    2. 使用foreignObject 元素,然后在其中包含格式化的HTML(div)。浏览器对此的支持相当粗略:https://stackoverflow.com/a/4992988/987185

    在这种情况下使用tspan 的示例:

    $(".arc").on("mouseover",(function(){
        d3.select("text").remove();
        var text = group
        .append("text")
        .attr("x",-30)
        .attr("y",-10)
        .selectAll('tspan')
        .data([cityName[$(this).attr('id')], $(this).attr('id') + '%'])
        .enter()
        .append('tspan')
          .attr('x', 0)
          .attr('dx', '-1em')
          .attr('dy', function (d, i) { return (2 * i - 1) + 'em'; })
          .text(String);
    
    })); 
    

    旁注:您似乎使用数字 ([0-9]*) 作为 id 属性。有效的id 属性不能以数字开头,尽管它们适用于大多数 浏览器。

    【讨论】:

    • 现在就像你建议的那样,但它不是两行,而是像 ......
    • @SergeyScopin 用一个例子更新了答案。它应该让您大致了解如何在text 中使用tspan
    猜你喜欢
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 2017-05-02
    • 2012-11-16
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多