score:0

Accepted answer

I have just made a few changes to the mousemove event.

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>

Here is the working jsFiddle

Hope it helps :)


Related Query

More Query from same tag