0

我有这个 D3 SVG 条形图:

http://tinyurl.com/cul7oyg 

在右下方(您必须向下滚动)。您可以毫无问题地将鼠标悬停在栏上。但是,单击左上角框底部的链接后,我创建了一个包含新数据的新条形图。鼠标悬停现在仅在悬停在条之间的线条上时有效。我对链接所做的只是使用: (d3.select("#barchart").html("") 清空 div 的内容,然后将一个新的较小数据集传递给该函数。数据是一个数组对象。

function generateBarForData(data) {
var margin = {top: 30, right: 10, bottom: 10, left: 10},
    width = 750 - margin.left - margin.right,
    height = data.length * 20 //500 - margin.top - margin.bottom;

var x = d3.scale.linear()
    .range([0, width*.85])

//chart1.x =x
//chart1.y = y

var y = d3.scale.ordinal()
    .rangeRoundBands([0, height], 0);

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

var svg = d3.select("#barchart").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 + ")");

//d3.tsv("data.tsv", type, function(error, data) {
  x.domain(d3.extent(data, function(d) { return +d.netdonations_over_population_to_gdppercap_percentage; })).nice();
  y.domain(data.map(function(d) { return d.name; }));

  bar = svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", function(d) { return +d.netdonations_over_population_to_gdppercap_percentage <= 0 ? ("bar negative " + d.name) : ("bar positive " + d.name); })
      .attr("x", function(d) { return x(Math.min(0, +d.netdonations_over_population_to_gdppercap_percentage)); })
      .attr("y", function(d) { return y(d.name); })
      .attr("width", function(d) { return Math.abs(x(+d.netdonations_over_population_to_gdppercap_percentage) - x(0)); })
      .attr("height", y.rangeBand()).on('mouseover', function(d,i) {
        //barChartSelectOneCountry(this) //rect
        selectBarChartCountryWithClear(d, this)  //mouseover(d)

      }).style("stroke","white")

chart1.svg = svg
chart1.x = x
chart1.y = y

svg.selectAll("text")
    .data(data)
  .enter().append("text").text(function(d) {
        return d;
   }).attr("class", function(d) {return +d.netdonations_over_population_to_gdppercap_percentage <= 0 ? "recipient" : "donor"})
   .attr("y", function(d, i) {
        return y(d.name)+ y.rangeBand();
        //return i * (height / data.length)+30;
   }).attr("x", function(d) {
         return x(0) //x(Math.min(0, +d.netdonations_over_population_to_gdppercap_percentage));
   }).text(function(d) {
        //return y(d) + y.rangeBand() / 2;
        return d.name + " " + d.netdonations_over_population_to_gdppercap_percentage;
        }).style("font-size","14px").attr("dx", 3).attr("dy", "-0.45em")


  svg.append("g")
      .attr("class", "x axis")
      .call(xAxis);

  svg.append("g")
      .attr("class", "y axis")
    .append("line")
      .attr("x1", x(0))
      .attr("x2", x(0))
      .attr("y2", height);
}
4

1 回答 1

0

只要鼠标悬停在栏的文本部分上,您的 mouseover 事件侦听器就不会触发。那是因为文本捕获了它。诀窍是阻止文本部分捕获鼠标事件。有关指南,请参阅http://themousepotatowebsite.co.za/pointer-events-none/

在你的 CSS

.text { pointer-event: none }
于 2013-04-29T01:22:17.267 回答