score:2

Accepted answer
  1. Firstly, a minor and quick glitch in the line coloring is due to this Name = d[0].name where d has no attribute as name but does have one as Name (capital N)

    Name = d[0].Name;
    
  2. Now, the main issue with your code is that you're trying to append text to the path which would never work. The way you should do is:

    var lines = d3.select(this)
      .append("path")
      .attr("d", function (b) { return line(b) })
      .style({ "stroke-width": "3px", "fill": "none" })
      .style("stroke", LineColor(Name))
      .transition().duration(1500);
    
    var texts = d3.select(this).selectAll('text.value')
       .....
    
  3. Using the logic in #2, append the texts for each element in d and assign x, y and text accordingly. Here's how:

    var texts = d3.select(this).selectAll('text.value')
                 .data(d)
                 .enter().append('text')
                 .classed('label', true)
                 .attr('dy', '-1em')
                 .text(function (d) {
                    return d.Value;
                 })                    
                 .attr({
                   'x': function(d, i) {
                         var width = d3.select(this).node().getBBox().width;
                         return x0(d.Date) + x0.rangeBand() / 2 - width/2;
                        },
                   'y': function(d, i) {
                         return YLine(d.Value);
                        }
                    });
    

    If you notice the above code, I'm assigning the text first and then applying x based on the width of the text using getBBox to center the text at the point. Feel free to adjust dy, dx according to your requirements.

    Putting together all of the above, here's a fork of that code:

    https://bl.ocks.org/shashank2104/2acfdd38dc262285d2c736ba86dbc1ad/11380be2e4c095808231056930d6860f97722254

    Hope this helps. And if you're up for suggestions, please refer this for adding data-points to a line chart.


Related Query

More Query from same tag