2

i have created a line chart using d3.js and added tooltip in the chart which on mouse hover creates a small circle and shows the y-axis value. Till this stage everything working fine but issue comes with the position of the circle which is displayed on mouse hover.

I want this circle to be displayed over the line but it is always shown either above the line graph or below it.

So, i have to issues:

  1. To fix the Tooltip circle, so that it will always be on the line chart's line?
  2. How to show the y values for the points which are between the defined points?

I am using .interpolate("basis") to generate the line due to which circle position is getting messed up. i don't know how to fix this because i need .interpolate("basis") in my code.

Please anyone have idea how to fix these issue in this code:

var data = [{
    x: '1-May-12',
    y: 5
  }, {
    x: '30-Apr-12',
    y: 28
  }, {
    x: '27-Apr-12',
    y: 58
  }, {
    x: '26-Apr-12',
    y: 88
  }, {
    x: '25-Apr-12',
    y: 8
  }, {
    x: '24-Apr-12',
    y: 48
  }, {
    x: '23-Apr-12',
    y: 28
  }, {
    x: '20-Apr-12',
    y: 68
  }, {
    x: '19-Apr-12',
    y: 8
  }, {
    x: '18-Apr-12',
    y: 58
  }, {
    x: '17-Apr-12',
    y: 5
  }, {
    x: '16-Apr-12',
    y: 80
  }, {
    x: '13-Apr-12',
    y: 38
  }],
  width = 1200,
  height = 360,
  margin = {
    top: 30,
    right: 20,
    bottom: 30,
    left: 50
  };
width -= margin.left - margin.right;
height -= margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
  .orient("bottom").ticks(0).tickSize(0)
  .tickFormat("").outerTickSize(0);

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

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

// function for the y grid lines
function make_y_axis() {
  return d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(10);
}

// Draw the y Grid lines
svg.append("g")
  .attr("class", "grid")
  .call(make_y_axis()
    .tickSize(-width, 0, 0)
    .tickFormat("")
  );

x.domain(d3.extent(data, function(d) {
  return parseDate(d.x);
}));
y.domain([0, d3.max(data, function(d) {
  return d.y;
})]);

data.sort(function(a, b) {
  return parseDate(a.x) - parseDate(b.x);
});

/*x.domain([parseDate(data[0].x), parseDate(data[data.length - 1].x)]);
y.domain(d3.extent(data, function (d) {
    return d.y;
}));*/

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

// Add the Y Axis
svg.append("g")
  .attr("class", "y axis")
  .call(yAxis);

// Add the valueline path
svg.append("path")
  .attr("class", "line");

// Define the line
var lineGen = d3.svg.line()
  .x(function(d) {
    return x(parseDate(d.x));
  })
  .y(function(d) {
    return y(d.y);
  })
  .interpolate("basis");

svg.append('path')
  .attr("class", "line")
  .attr('d', lineGen(data));

var focus = svg.append("g")
  .attr("class", "focus")
  .style("display", "none");

focus.append("circle")
  .attr("r", 4.5);

focus.append("text")
  .attr("x", 9)
  .attr("dy", ".35em");

svg.append("rect")
  .attr("class", "overlay")
  .attr("width", width)
  .attr("height", height)
  .on("mouseover", function() {
    focus.style("display", null);
  })
  .on("mouseout", function() {
    focus.style("display", "none");
  })
  .on("mousemove", mousemove);

var bisectDate = d3.bisector(function(d) {
  return parseDate(d.x);
}).left;

function mousemove() {
  var x0 = x.invert(d3.mouse(this)[0]),
    i = bisectDate(data, x0, 1),
    d0 = data[i - 1],
    d1 = data[i],
    d = x0 - parseDate(d0.x) > parseDate(d1.x) - x0 ? d1 : d0;
  console.log(y(d.y));
  focus.attr("transform", "translate(" + x(parseDate(d.x)) + "," + y(d.y) + ")");
  focus.select("text").text(d.y);
}
.x.axis path {
  display: none;
}

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.overlay {
  fill: none;
  pointer-events: all;
}

.focus circle {
  fill: none;
  stroke: steelblue;
}

.axis path,
.axis line {
  fill: none;
  stroke: grey;
  stroke-width: 2;
  shape-rendering: crispEdges;
}

.grid .tick {
  stroke: lightgrey;
  stroke-opacity: 0.7;
  shape-rendering: crispEdges;
}

.grid path {
  stroke-width: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

https://jsfiddle.net/nikunj2512/w5y1q5ds/1/

nikunj2512
  • 621
  • 7
  • 26
  • in your linegen function you have made interpolate as `.interpolate("basis");` this is an approximate location to make it exact do `.interpolate("linear");` this will resolve your first problem https://jsfiddle.net/53aLmt7r/1/ – Cyril Cherian Dec 18 '15 at 07:14
  • @Cyril: True but i need to use the `interpolate("basis")` because i want my graph should have curves instead of steeps. – nikunj2512 Dec 18 '15 at 08:12
  • my reason is this http://bl.ocks.org/mbostock/4342190 (you can choose teh different interpolation) the path calculation for d will not pass through the data points we provide :) and so is teh reson why the tooltip is coming in strange places – Cyril Cherian Dec 18 '15 at 08:19
  • Try [this](https://jsfiddle.net/ramanbedi1989/3zw6gef4/2/) and see if it helps. – Raman Dec 18 '15 at 09:10
  • @Cyril: Ok but how to fix this issue. How to pass the path calculation for d? – nikunj2512 Dec 18 '15 at 09:13
  • @Raman: I had tried `monotone` but it is similar to `linear` and different from `basic`. Do you know any example where `basic is used`?? – nikunj2512 Dec 18 '15 at 09:17

1 Answers1

6

This question comes up from time to time and I can think of two solutions.

Solution 1 -- you could parse the path itself and display those points (and values) as your tooltip to stay on the fitted path. The down-side of this solution is that depending on how d3 does the fitting you'll end up with extra points or points slightly out of place:

// get path
var p = svg.select('.line')
  .attr("d");
// parse it by M coords, L coords and C coords
// build an array of points used in generating the path
var points = p.split(/(?=[LMC])/).map(function(d){
  var fChar = d.slice(0, 1);
  if (fChar === "M" || fChar === "L"){
    return d.slice(1, d.length)
            .split(",")
            .map(function(p){ return +p; });
  } else if (fChar === "C") {
    var s = d.split(",");
    return [+s[s.length - 2], +s[s.length - 1]];
  }
});
// modify the bisect our array of points
var bisectDate = d3.bisector(function(d) {
  return d[0];
}).left;
// modify the mousemove
function mousemove() {
  var x0 = d3.mouse(this)[0],
    i = bisectDate(points, x0, 1) - 1,
    y0 = y.invert(points[i][1]);

  //console.log(y(d.y));
  focus.attr("transform", "translate(" + points[i][0] + "," + points[i][1] + ")");
  focus.select("text").text(y0);
}

Solution 2 -- Just have your tooltip follow the path in its entirety. This is a very easy approach with some built-in SVG constructs:

var path = svg.select('.line').node();
var totLength = path.getTotalLength();
function mousemove() {
  var x0 = d3.mouse(this)[0],
      per = width / x0;
      point = path.getPointAtLength(totLength / per)
      y0 = y.invert(point.y);

  focus.attr("transform", "translate(" + point.x + "," + point.y + ")");
  focus.select("text").text(y0);
}

Solution 1 - Full working code:

<!DOCTYPE html>
<html>

<head>
  <script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
  <style>
    .x.axis path {
      display: none;
    }
    
    .line {
      fill: none;
      stroke: steelblue;
      stroke-width: 1.5px;
    }
    
    .overlay {
      fill: none;
      pointer-events: all;
    }
    
    .focus circle {
      fill: none;
      stroke: steelblue;
    }
    
    .axis path,
    .axis line {
      fill: none;
      stroke: grey;
      stroke-width: 2;
      shape-rendering: crispEdges;
    }
    
    .grid .tick {
      stroke: lightgrey;
      stroke-opacity: 0.7;
      shape-rendering: crispEdges;
    }
    
    .grid path {
      stroke-width: 0;
    }
  </style>
</head>

<body>
  <script>
    var data = [{
        x: '1-May-12',
        y: 5
      }, {
        x: '30-Apr-12',
        y: 28
      }, {
        x: '27-Apr-12',
        y: 58
      }, {
        x: '26-Apr-12',
        y: 88
      }, {
        x: '25-Apr-12',
        y: 8
      }, {
        x: '24-Apr-12',
        y: 48
      }, {
        x: '23-Apr-12',
        y: 28
      }, {
        x: '20-Apr-12',
        y: 68
      }, {
        x: '19-Apr-12',
        y: 8
      }, {
        x: '18-Apr-12',
        y: 58
      }, {
        x: '17-Apr-12',
        y: 5
      }, {
        x: '16-Apr-12',
        y: 80
      }, {
        x: '13-Apr-12',
        y: 38
      }],
      width = 1200,
      height = 360,
      margin = {
        top: 30,
        right: 20,
        bottom: 30,
        left: 50
      };
    width -= margin.left - margin.right;
    height -= margin.top - margin.bottom;
    var parseDate = d3.time.format("%d-%b-%y").parse;
    var x = d3.time.scale().range([0, width]);
    var y = d3.scale.linear().range([height, 0]);
    var xAxis = d3.svg.axis().scale(x)
      .orient("bottom").ticks(0).tickSize(0)
      .tickFormat("").outerTickSize(0);

    var yAxis = d3.svg.axis().scale(y)
      .orient("left").tickSize(-width, 0, 0);
      //.ticks(0)
      //.tickFormat("");

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

    // function for the y grid lines
    function make_y_axis() {
      return d3.svg.axis()
        .scale(y)
        .orient("left")
        .ticks(10);
    }

    x.domain(d3.extent(data, function(d) {
      return parseDate(d.x);
    }));
    y.domain([0, d3.max(data, function(d) {
      return d.y;
    })]);


    console.log(y.domain())

    data.sort(function(a, b) {
      return parseDate(a.x) - parseDate(b.x);
    });

    /*x.domain([parseDate(data[0].x), parseDate(data[data.length - 1].x)]);
    y.domain(d3.extent(data, function (d) {
        return d.y;
    }));*/

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

    // Add the Y Axis
    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);

    // Define the line
    var lineGen = d3.svg.line()
      .x(function(d) {
        return x(parseDate(d.x));
      })
      .y(function(d) {
        return y(d.y);
      })
      .interpolate("basis");

    svg.append('path')
      .attr("class", "line")
      .attr('d', lineGen(data));

    var focus = svg.append("g")
      .attr("class", "focus")
      .style("display", "none");

    focus.append("circle")
      .attr("r", 4.5);

    focus.append("text")
      .attr("x", 9)
      .attr("dy", ".35em");

    svg.append("rect")
      .attr("class", "overlay")
      .attr("width", width)
      .attr("height", height)
      .on("mouseover", function() {
        focus.style("display", null);
      })
      .on("mouseout", function() {
        focus.style("display", "none");
      })
      .on("mousemove", mousemove);

    
    var p = svg.select('.line')
      .attr("d");
    var points = p.split(/(?=[LMC])/).map(function(d){
      var fChar = d.slice(0, 1);
      if (fChar === "M" || fChar === "L"){
        return d.slice(1, d.length)
                .split(",")
                .map(function(p){ return +p; });
      } else if (fChar === "C") {
        var s = d.split(",");
        return [+s[s.length - 2], +s[s.length - 1]];
      }
   });
   var bisectDate = d3.bisector(function(d) {
      return d[0];
    }).left;
    function mousemove() {
      var x0 = d3.mouse(this)[0],
        i = bisectDate(points, x0, 1) - 1,
        y0 = y.invert(points[i][1]);
        
      //console.log(y(d.y));
      focus.attr("transform", "translate(" + points[i][0] + "," + points[i][1] + ")");
      focus.select("text").text(y0);
    }
  </script>
</body>

</html>

Solution 2 - Full working code:

<!DOCTYPE html>
<html>

<head>
  <script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
  <style>
    .x.axis path {
      display: none;
    }
    
    .line {
      fill: none;
      stroke: steelblue;
      stroke-width: 1.5px;
    }
    
    .overlay {
      fill: none;
      pointer-events: all;
    }
    
    .focus circle {
      fill: none;
      stroke: steelblue;
    }
    
    .axis path,
    .axis line {
      fill: none;
      stroke: grey;
      stroke-width: 2;
      shape-rendering: crispEdges;
    }
    
    .grid .tick {
      stroke: lightgrey;
      stroke-opacity: 0.7;
      shape-rendering: crispEdges;
    }
    
    .grid path {
      stroke-width: 0;
    }
  </style>
</head>

<body>
  <script>
    var data = [{
        x: '1-May-12',
        y: 5
      }, {
        x: '30-Apr-12',
        y: 28
      }, {
        x: '27-Apr-12',
        y: 58
      }, {
        x: '26-Apr-12',
        y: 88
      }, {
        x: '25-Apr-12',
        y: 8
      }, {
        x: '24-Apr-12',
        y: 48
      }, {
        x: '23-Apr-12',
        y: 28
      }, {
        x: '20-Apr-12',
        y: 68
      }, {
        x: '19-Apr-12',
        y: 8
      }, {
        x: '18-Apr-12',
        y: 58
      }, {
        x: '17-Apr-12',
        y: 5
      }, {
        x: '16-Apr-12',
        y: 80
      }, {
        x: '13-Apr-12',
        y: 38
      }],
      width = 1200,
      height = 360,
      margin = {
        top: 30,
        right: 20,
        bottom: 30,
        left: 50
      };
    width -= margin.left - margin.right;
    height -= margin.top - margin.bottom;
    var parseDate = d3.time.format("%d-%b-%y").parse;
    var x = d3.time.scale().range([0, width]);
    var y = d3.scale.linear().range([height, 0]);
    var xAxis = d3.svg.axis().scale(x)
      .orient("bottom").ticks(0).tickSize(0)
      .tickFormat("").outerTickSize(0);

    var yAxis = d3.svg.axis().scale(y)
      .orient("left").tickSize(-width, 0, 0);
      //.ticks(0)
      //.tickFormat("");

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

    // function for the y grid lines
    function make_y_axis() {
      return d3.svg.axis()
        .scale(y)
        .orient("left")
        .ticks(10);
    }

    x.domain(d3.extent(data, function(d) {
      return parseDate(d.x);
    }));
    y.domain([0, d3.max(data, function(d) {
      return d.y;
    })]);


    data.sort(function(a, b) {
      return parseDate(a.x) - parseDate(b.x);
    });

    /*x.domain([parseDate(data[0].x), parseDate(data[data.length - 1].x)]);
    y.domain(d3.extent(data, function (d) {
        return d.y;
    }));*/

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

    // Add the Y Axis
    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);

    // Define the line
    var lineGen = d3.svg.line()
      .x(function(d) {
        return x(parseDate(d.x));
      })
      .y(function(d) {
        return y(d.y);
      })
      .interpolate("basis");

    svg.append('path')
      .attr("class", "line")
      .attr('d', lineGen(data));

    var focus = svg.append("g")
      .attr("class", "focus")
      .style("display", "none");

    focus.append("circle")
      .attr("r", 4.5);

    focus.append("text")
      .attr("x", 9)
      .attr("dy", ".35em");

    svg.append("rect")
      .attr("class", "overlay")
      .attr("width", width)
      .attr("height", height)
      .on("mouseover", function() {
        focus.style("display", null);
      })
      .on("mouseout", function() {
        focus.style("display", "none");
      })
      .on("mousemove", mousemove);

    
    var path = svg.select('.line').node();
    var totLength = path.getTotalLength();
    function mousemove() {
      var x0 = d3.mouse(this)[0],
          per = width / x0;
          point = path.getPointAtLength(totLength / per)
          y0 = y.invert(point.y);
      
      focus.attr("transform", "translate(" + point.x + "," + point.y + ")");
      focus.select("text").text(y0);
    }
  </script>
</body>

</html>
Mark
  • 106,305
  • 20
  • 172
  • 230
  • Thank You for the code. i liked the Solution 2 because it solves both of my questions. Thank You – nikunj2512 Dec 19 '15 at 17:25
  • Hi @mark i liked your solution 2 but can you help me for multiple line in line chart with hover effect – Joyson Oct 13 '16 at 10:01
  • Im new to d3 js `http://jsfiddle.net/jasonantho/wmab3tyw/6/` here my working code please help me to give hover effect on both lines – Joyson Oct 13 '16 at 10:01
  • @Joyson, are you looking for something like [this](http://stackoverflow.com/a/34887578/16363)? – Mark Oct 13 '16 at 14:32
  • yes @mark but i need to know which line i need to update because im not expert at this – Joyson Oct 14 '16 at 05:46