将D3工具提示应用于“甜甜圈倍数" [英] Apply D3 tooltip to Donut Multiples

查看:111
本文介绍了将D3工具提示应用于“甜甜圈倍数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have code like this that creates multiple D3 donut multiples.

<!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工具提示的方法,以便将光标放在甜甜圈上时可以看到每个甜甜圈的确切数据.我了解这里还有其他工具提示示例,但没有一个适用于甜甜圈倍数示例.

I am looking for a way to implement the D3 tooltip so that I can see the exact data of each chunk of the donut when I put my cursor over it. I understand that there are other examples of the tooltip on here but none of them have worked with the donut multiples example.

以下是一些示例数据

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

The D3 doc of this can be found at http://bl.ocks.org/mbostock/3888852

推荐答案

当您说"D3工具提示"时,我不确定您指的是什么,因为d3没有任何内置的工具提示功能.也就是说,有一些不错的第三方插件可以在d3中进行工具提示.

I'm not sure what you're referring to when you say "the D3 tooltip", because d3 doesn't have any built-in tooltip functionality. That said, there are some good third-party plugins out there for doing tooltips in d3.

d3-tip 可以很好地满足您的尝试.

d3-tip is one that would work well for what you're trying to do.

您可以创建一个工具提示功能来显示每个弧的填充数据,如下所示:

You can create a tooltip function to display your population data for each arc like this:

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

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

Then you can call the function on your svg selection:

svg.call(tip);

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

Finally, you can use mouse event listeners on your arcs to show and hide the tooltip:

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 ,上面有一个有效的示例.

Here's a PLUNK with a working example.

您还可以在此处查看d3提示文档. .

这篇关于将D3工具提示应用于“甜甜圈倍数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆