工具提示不起作用,也不显示在D3散点图上

时间:2019-11-15 15:11:28

标签: javascript d3.js

我想在折线图中添加一个工具提示,其中每个数据点在悬停时都会显示一个文本框,如下所示:

----------------- | x坐标:## | y坐标:## | ----------------- |

工作图的工作代码段发布在下面。但是我将注释掉工具提示块以绘制图表。

谢谢。

var margin = {top: 50, right: 50, bottom: 50, left: 50}
  , width = window.innerWidth - margin.left - margin.right 
  , height = window.innerHeight - margin.top - margin.bottom; 


//labels
var labels = ['Mon','Tue','Thur','Frid'];
var yvals = [12,11,0,18];
// X scale
var xScale = d3.scalePoint()
    .domain(labels) // input
    .range([0, width-1]); // output

// Y scale 
var yScale = d3.scaleLinear()
    .domain([0, 20]) 
    .range([height,0]); 


var line = d3.line()
    .x(function(d, i) { return xScale(labels[i]); }) 
    .y(function(d) { return yScale(d.y); }) 
    .curve(d3.curveMonotoneX) 


var dataset = d3.range(yvals.length).map(function(d,i) { return {"y": yvals[i]} })

//Tooltip
  //var tip = d3.select('body')
      //.append('div')
      //.attr('class', 'tip')
      //.html('number:'+ function(d,i) return {data[data.i]})
     // .style('border', '1px solid steelblue')
     // .style('padding', '5px')
      //.style('position', 'absolute')
     // .style('display', 'none')
      //.on('mouseover', function(d, i) {
      //  tip.transition().duration(0);
     // })
     // .on('mouseout', function(d, i) {
      //  tip.style('display', 'none');
    //  });
// SVGs 
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.append("rect")
    .attr("width", "100%")
    .attr("height", "100%")
    .attr("fill", "white");

svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// x axis call 
svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    //.call(d3.axisBottom(xScale)); 
	.call(d3.axisBottom(xScale));

// y axis call 
svg.append("g")
    .attr("class", "y axis")
    .call(d3.axisLeft(yScale)); 


svg.append("path")
    .datum(dataset) 
    .attr("class", "line") 
    .attr("d", line); 

// 12. Appends a circle for each datapoint 
svg.selectAll(".dot")
    .data(dataset)
  .enter().append("circle") // Uses the enter().append() method
    .attr("class", "dot") // Assign a class for styling
    .attr("cx", function(d, i) { return xScale(labels[i]) })
    .attr("cy", function(d,i) { return yScale(yvals[i]) })
    .attr("r", 3);
    //.on('mouseover', function(d, i) {
      //  tip.transition().duration(0);
        
     // })
	
	
 svg.append("text")
      .attr("class", "title")
      .attr("x", width/2)
      .attr("y", 0 - (margin.top / 2))
      .attr("text-anchor", "middle")
      .text("Testing");
.line {
    fill: none;
    stroke: orange;
    stroke-width: 1;
}


.dot {
    fill: brown;
    stroke: #fff;
}
<!DOCTYPE html>
<meta charset="utf-8">

<style type="text/css">
</style>

<body>
</body>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
</script>

1 个答案:

答案 0 :(得分:0)

我刚刚对mousemove事件进行了一些更改。

var margin = {
    top: 50,
    right: 50,
    bottom: 50,
    left: 50
  },
  width = window.innerWidth - margin.left - margin.right,
  height = window.innerHeight - margin.top - margin.bottom;


//labels
var labels = ['Mon', 'Tue', 'Thur', 'Frid'];
var yvals = [12, 11, 0, 18];
// X scale
var xScale = d3.scalePoint()
  .domain(labels) // input
  .range([0, width - 1]); // output

// Y scale 
var yScale = d3.scaleLinear()
  .domain([0, 20])
  .range([height, 0]);


var line = d3.line()
  .x(function(d, i) {
    return xScale(labels[i]);
  })
  .y(function(d) {
    return yScale(d.y);
  })
  .curve(d3.curveMonotoneX)


var dataset = d3.range(yvals.length).map(function(d, i) {
  return {
    "y": yvals[i]
  }
})

var tip = d3.select('body').append("div")
  .attr("class", "tip");

// SVGs 
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.append("rect")
  .attr("width", "100%")
  .attr("height", "100%")
  .attr("fill", "white");

svg.append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// x axis call 
svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  //.call(d3.axisBottom(xScale)); 
  .call(d3.axisBottom(xScale));

// y axis call 
svg.append("g")
  .attr("class", "y axis")
  .call(d3.axisLeft(yScale));


svg.append("path")
  .datum(dataset)
  .attr("class", "line")
  .attr("d", line);

// 12. Appends a circle for each datapoint 
svg.selectAll(".dot")
  .data(dataset)
  .enter().append("circle") // Uses the enter().append() method
  .attr("class", "dot") // Assign a class for styling
  .attr("cx", function(d, i) {
    return xScale(labels[i])
  })
  .attr("cy", function(d, i) {
    return yScale(yvals[i])
  })
  .attr("r", 3)
  .on("mouseover", function() {
    tip.style("display", null);
  })
  .on("mouseout", function() {
    tip.style("display", "none");
  })
  .on("mousemove", function(d) {

    return tip
      .style("left", d3.event.pageX + "px")
      .style("top", d3.event.pageY + 10 + "px")
      .style("visibility", "visible")
      .html(function() {
        return '<div style="border:1px solid #ccc;">' +
          '<p style="font-weight:bold;">' + d.y + '</p>' +
          '</div>';
      })

  })


svg.append("text")
  .attr("class", "title")
  .attr("x", width / 2)
  .attr("y", 0 - (margin.top / 2))
  .attr("text-anchor", "middle")
  .text("Testing");
.line {
  fill: none;
  stroke: orange;
  stroke-width: 1;
}

.dot {
  fill: brown;
  stroke: #fff;
}

.tip {
  position: absolute;
  border: 1px solid steelblue;
  visibility: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.0.0/d3.min.js"></script>

这是工作中的jsFiddle

希望它会有所帮助:)

相关问题