如何使用数据从数据驱动的d3图表添加第二个工具提示

时间:2017-03-21 23:42:31

标签: javascript d3.js svg

我一直在使用这个数据驱动的D3图表。它正在工作,但是当我添加第二个工具提示时,它们都停止显示,我在控制台中发现错误。

我正在处理的原件是:http://bl.ocks.org/Caged/6476579

我得到的错误是这个,我相信这意味着鼠标悬停事件上调用的函数无法访问数据:

 svg7-2.html:105  Uncaught TypeError: Cannot read property 'frequency2'
 of undefined
     at Function.<anonymous> (svg7-2.html:105)
     at Function.tip.show (d3.tip.v0.6.3.js:33)
     at SVGRectElement.<anonymous> (svg7-2.html:155)
     at SVGRectElement.__onmouseover (d3.v3.min.js:1)

这是我的完整代码:

<style>

body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.bar {
  fill: orange;
}

.bar:hover {
  fill: orangered ;
}

.x.axis path {
  display: none;
}

.d3-tip {
  line-height: 1;
  font-weight: bold;
  padding: 12px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  border-radius: 2px;
}

/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
  box-sizing: border-box;
  display: inline;
  font-size: 10px;
  width: 100%;
  line-height: 1;
  color: rgba(0, 0, 0, 0.8);
  content: "\25BC";
  position: absolute;
  text-align: center;
}

/* Style northward tooltips differently */
.d3-tip.n:after {
  margin: -1px 0 0 0;
  top: 100%;
  left: 0;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="d3.tip.v0.6.3.js"></script>
<script>

var margin = {top: 40, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var data = 
    [
        {letter:"A", frequency2:.08167},
        {letter:"B", frequency2:.01492},
        {letter:"C", frequency2:.02780},
        {letter:"D", frequency2:.04253},
        {letter:"E", frequency2:.12702},
        {letter:"F", frequency2:.02288},
        {letter:"G", frequency2:.02022},
        {letter:"H", frequency2:.06094},
        {letter:"I", frequency2:.06973},
        {letter:"J", frequency2:.00153},
        {letter:"K", frequency2:.00747},
        {letter:"L", frequency2:.04025},
        {letter:"M", frequency2:.02517},
        {letter:"N", frequency2:.06749}
    ];

var formatPercent = d3.format(".0%");

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickFormat(formatPercent);

var tip = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-10, 0])
  .html(function(d) {
    return "<strong>Frequency2:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
  })

var tip2 = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-40, 0])
  .html(function(d) {
    return "<strong>Other Variables:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
  })

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.call(tip);
svg.call(tip2);

data.forEach(d=>{
  d.frequency = +d.frequency;
});

  x.domain(data.map(function(d) { return d.letter; }));
  y.domain([0, d3.max(data, function(d) { return d.frequency2; })]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Frequency 2");

  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.letter); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.frequency2); })
      .attr("height", function(d) { return height - y(d.frequency2); })
      .on("mouseover", function() {
          tip.show();
          tip2.show();
        }).on("mouseout", function() {
          tip.hide();
          tip2.hide();
        });

function type(d) {
  d.frequency2 = +d.frequency2;
  return d;
}

</script>

1 个答案:

答案 0 :(得分:1)

与修改了文字的previous answer不同,现在您必须将datum传递给工具提示:

.on("mouseover", function(d) {
    tip.show(d);
    tip2.show(d);
}).on("mouseout", function(d) {
    tip.hide();
    tip2.hide();
});

以下是工作代码:

var margin = {
    top: 40,
    right: 20,
    bottom: 30,
    left: 40
  },
  width = 960 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;

var data = [{
  letter: "A",
  frequency2: .08167
}, {
  letter: "B",
  frequency2: .01492
}, {
  letter: "C",
  frequency2: .02780
}, {
  letter: "D",
  frequency2: .04253
}, {
  letter: "E",
  frequency2: .12702
}, {
  letter: "F",
  frequency2: .02288
}, {
  letter: "G",
  frequency2: .02022
}, {
  letter: "H",
  frequency2: .06094
}, {
  letter: "I",
  frequency2: .06973
}, {
  letter: "J",
  frequency2: .00153
}, {
  letter: "K",
  frequency2: .00747
}, {
  letter: "L",
  frequency2: .04025
}, {
  letter: "M",
  frequency2: .02517
}, {
  letter: "N",
  frequency2: .06749
}];

var formatPercent = d3.format(".0%");

var x = d3.scale.ordinal()
  .rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
  .range([height, 0]);

var xAxis = d3.svg.axis()
  .scale(x)
  .orient("bottom");

var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left")
  .tickFormat(formatPercent);

var tip = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-10, 0])
  .html(function(d) {
    return "<strong>Frequency2:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
  })

var tip2 = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-40, 0])
  .html(function(d) {
    return "<strong>Other Variables:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
  })

var svg = d3.select("body").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.call(tip);
svg.call(tip2);

data.forEach(d => {
  d.frequency = +d.frequency;
});

x.domain(data.map(function(d) {
  return d.letter;
}));
y.domain([0, d3.max(data, function(d) {
  return d.frequency2;
})]);

svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis)
  .append("text")
  .attr("transform", "rotate(-90)")
  .attr("y", 6)
  .attr("dy", ".71em")
  .style("text-anchor", "end")
  .text("Frequency 2");

svg.selectAll(".bar")
  .data(data)
  .enter().append("rect")
  .attr("class", "bar")
  .attr("x", function(d) {
    return x(d.letter);
  })
  .attr("width", x.rangeBand())
  .attr("y", function(d) {
    return y(d.frequency2);
  })
  .attr("height", function(d) {
    return height - y(d.frequency2);
  })
  .on("mouseover", function(d) {
    tip.show(d);
    tip2.show(d);
  }).on("mouseout", function(d) {
    tip.hide();
    tip2.hide();
  });

function type(d) {
  d.frequency2 = +d.frequency2;
  return d;
}
body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.bar {
  fill: orange;
}

.bar:hover {
  fill: orangered;
}

.x.axis path {
  display: none;
}

.d3-tip {
  line-height: 1;
  font-weight: bold;
  padding: 12px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  border-radius: 2px;
}


/* Creates a small triangle extender for the tooltip */

.d3-tip:after {
  box-sizing: border-box;
  display: inline;
  font-size: 10px;
  width: 100%;
  line-height: 1;
  color: rgba(0, 0, 0, 0.8);
  content: "\25BC";
  position: absolute;
  text-align: center;
}


/* Style northward tooltips differently */

.d3-tip.n:after {
  margin: -1px 0 0 0;
  top: 100%;
  left: 0;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.min.js"></script>