D3: Add data value labels to multi line graph

后端 未结 1 1130
粉色の甜心
粉色の甜心 2021-01-24 19:29

I am using this multiline graph but so far have failed to generate data value labels on every tick (for every day).



        
相关标签:
1条回答
  • 2021-01-24 20:01

    Your text will not be visible, as it is located outside the boundaries of your svg: you added a group that is translated of margin.left, and the put your x at width+3, which means located at width+3+margin.left of the left border of your svg.

    Try replacing your append text with something like:

    svg.append("text")
        .attr("transform", "translate(" + (width/2) + "," + y(data[0].sev4) + ")")
        .attr("dy", ".35em")
        .attr("text-anchor", "start")
        .style("fill", "red")
        .text("Sev4");
    
    svg.append("text")
        .attr("transform", "translate(" + (width/2) + "," + y(data[0].sev3) + ")")
        .attr("dy", ".35em")
        .attr("text-anchor", "start")
        .style("fill", "steelblue")
        .text("Sev3");
    

    I did not test it, so I cannot guarantee, but your code seems fine, that's the only thing I see.

    Result of this add: Your suggestion only moves the line labels to center but does not add value labels to the data points

    EDIT

    After your clarifications, here is a plunk: http://plnkr.co/edit/lDlseqUQQXgoFwTK5Aop?p=preview

    The part you will be interested in is:

    svg.append('g')
    .classed('labels-group', true)
    .selectAll('text')
    .data(data)
    .enter()
    .append('text')
    .classed('label', true)
    .attr({
      'x': function(d, i) {
        return x(d.date);
      },
      'y': function(d, i) {
        return y(d.sev3);
      }
    })
    .text(function(d, i) {
      return d.sev3;
    });
    

    This will draw your labels. Is it the result you try to achieve?

    0 讨论(0)
提交回复
热议问题