【问题标题】:Apply D3 tooltip to Donut Multiples将 D3 工具提示应用于甜甜圈倍数
【发布时间】:2014-07-15 21:51:37
【问题描述】:

我有这样的代码,可以创建多个 D3 甜甜圈倍数。

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  font: 10px sans-serif;
}

svg {
  padding: 10px 0 0 10px;
}

.arc {
  stroke: #fff;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var radius = 74,
    padding = 10;

var color = d3.scale.ordinal()
    .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

var arc = d3.svg.arc()
    .outerRadius(radius)
    .innerRadius(radius - 30);

var pie = d3.layout.pie()
    .sort(null)
    .value(function(d) { return d.population; });

d3.csv("data.csv", function(error, data) {
  color.domain(d3.keys(data[0]).filter(function(key) { return key !== "State"; }));

  data.forEach(function(d) {
    d.ages = color.domain().map(function(name) {
      return {name: name, population: +d[name]};
    });
  });

  var legend = d3.select("body").append("svg")
      .attr("class", "legend")
      .attr("width", radius * 2)
      .attr("height", radius * 2)
    .selectAll("g")
      .data(color.domain().slice().reverse())
    .enter().append("g")
      .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

  legend.append("rect")
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", color);

  legend.append("text")
      .attr("x", 24)
      .attr("y", 9)
      .attr("dy", ".35em")
      .text(function(d) { return d; });

  var svg = d3.select("body").selectAll(".pie")
      .data(data)
    .enter().append("svg")
      .attr("class", "pie")
      .attr("width", radius * 2)
      .attr("height", radius * 2)
    .append("g")
      .attr("transform", "translate(" + radius + "," + radius + ")");

  svg.selectAll(".arc")
      .data(function(d) { return pie(d.ages); })
    .enter().append("path")
      .attr("class", "arc")
      .attr("d", arc)
      .style("fill", function(d) { return color(d.data.name); });

  svg.append("text")
      .attr("dy", ".35em")
      .style("text-anchor", "middle")
      .text(function(d) { return d.State; });

});

</script>

我正在寻找一种方法来实现 D3 工具提示,以便当我将光标放在甜甜圈上时,我可以看到每个甜甜圈块的确切数据。我知道这里还有其他工具提示示例,但没有一个与甜甜圈倍数示例一起使用。

这是一些示例数据

State,Under 5 Years,5 to 13 Years,14 to 17 Years,18 to 24 Years,25 to 44 Years,45 to 64 Years,65 Years and Over
AL,310504,552339,259034,450818,1231572,1215966,641667
AK,52083,85640,42153,74257,198724,183159,50277
AZ,515910,828669,362642,601943,1804762,1523681,862573
AR,202070,343207,157204,264160,754420,727124,407205
CA,2704659,4499890,2159981,3853788,10604510,8819342,4114496
CO,358280,587154,261701,466194,1464939,1290094,511094
CT,211637,403658,196918,325110,916955,968967,478007
DE,59319,99496,47414,84464,230183,230528,121688
DC,36352,50439,25225,75569,193557,140043,70648
FL,1140516,1938695,925060,1607297,4782119,4746856,3187797
GA,740521,1250460,557860,919876,2846985,2389018,981024
HI,87207,134025,64011,124834,356237,331817,190067
ID,121746,201192,89702,147606,406247,375173,182150
IL,894368,1558919,725973,1311479,3596343,3239173,1575308
IN,443089,780199,361393,605863,1724528,1647881,813839
IA,201321,345409,165883,306398,750505,788485,444554
KS,202529,342134,155822,293114,728166,713663,366706
KY,284601,493536,229927,381394,1179637,1134283,565867
LA,310716,542341,254916,471275,1162463,1128771,540314
ME,71459,133656,69752,112682,331809,397911,199187
MD,371787,651923,316873,543470,1556225,1513754,679565

这方面的 D3 文档可以在以下位置找到 http://bl.ocks.org/mbostock/3888852

【问题讨论】:

    标签: javascript d3.js jquery-tooltip


    【解决方案1】:

    我不确定您所说的“D3 工具提示”指的是什么,因为 d3 没有任何内置的工具提示功能。也就是说,有一些很好的第三方插件可以在 d3 中做工具提示。

    d3-tip 可以很好地满足您的要求。

    您可以创建一个工具提示函数来显示每个弧的人口数据,如下所示:

    var tip = d3.tip()
      .attr('class', 'd3-tip')
      .html(function(d) { return d.data.population; })
      .direction('s');
    

    然后您可以在您的 svg 选择上调用该函数:

    svg.call(tip);
    

    最后,您可以在弧上使用鼠标事件侦听器来显示和隐藏工具提示:

    svg.selectAll(".arc")
      .data(function(d) { return pie(d.ages); })
      .enter().append("path")
        .attr("class", "arc")
        .attr("d", arc)
        .style("fill", function(d) { return color(d.data.name); })
        .on('mouseover', tip.show)
        .on('mouseout', tip.hide);
    

    这是一个带有工作示例的PLUNK

    您还可以查看 d3-tip 文档here

    【讨论】:

    • 看起来很棒,谢谢@jshanley!再问一个问题。我应该能够在函数中使用它吗?我目前有一个搜索栏,可以搜索数据集中的状态,但我现在似乎无法同时工作,所以我想知道是否有可能?
    • 我收到“未捕获的类型错误:未定义不是函数”
    • 它发生在 svg.call(tip);行
    • 你应该可以在函数中使用它。也许您可以发布您尝试使用它的功能的代码,以便我们可以尝试解决它。至于您遇到的错误,请确保您的变量 svg 是 d3 选择,而不是元素本身。 .call() 方法适用于 d3 选择,如果 svg 不是 d3 选择,则属性 .call 将未定义,因此将 .call() 作为函数调用将抛出您得到的错误。
    • 其实也有可能是变量tip没有在你当前的作用域中定义,因为它会被作为一个函数调用。发布更多更新的代码,我们可以尝试诊断它。
    【解决方案2】:

    在为我的条形图添加工具提示时,我在使用 d3js 调试我的 javascript 代码时也遇到了相同的 TypeError。

     bars.append('rect')
           .attr('y', maxHeight)
           .attr('height', 0)
           .attr('width', function (d) { return x.rangeBand(d) - 1; })
           .attr('class', 'bar')
           .transition().duration(1500)
           .attr('y', function (d, i) { return y(d.y); })
           .attr('height', function (d, i) { return maxHeight - y(d.y); });
           .on('mouseover', tip.show)
           .on('mouseout', tip.hide);
    

    在上面的代码中,我在“bars.append()”上使用了“.on('mouseover', tip.show)”。这是错误的用法。

    后来,我发现“.on('mouseover', tip.show)”应该应用于函数“.select('rect') 或.selectall('rect')”。 现在下面的 sn-p 对我的应用程序正常工作。

     bars.append('rect')
           .attr('y', maxHeight)
           .attr('height', 0)
           .attr('width', function (d) { return    x.rangeBand(d) - 1; })
           .attr('class', 'bar')
           .transition().duration(1500)
           .attr('y', function (d, i) { return y(d.y); })
           .attr('height', function (d, i) { return    maxHeight - y(d.y); });
    
      bars.select('rect')
            .on('mouseover', tip.show)
            .on('mouseout', tip.hide);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      相关资源
      最近更新 更多