简体   繁体   中英

d3 real time line chart with labels

I am creating the line chart with the following http://bost.ocks.org/mike/path/

Now I want to display the value of last data on line chart curve ie the new value adding into the chart.so I tried this code it added the label but when new data is added the chart doesnot get updated. NOTE:My data length is 13.I am trying to show last data value only.

   svg.append("text")
     .attr("dy", ".35em")
     .style("fill", "red")
     .attr({
         'x':x(12),
          'y': y(data[12])
      })
      .text(data[12]);

Even though you solved the issue, here is a code snippet just in case anyone else is looking (since I was in the middle of posting when you added your comment).

 <!DOCTYPE html> <html> <meta charset="utf-8"> <style> svg { font: 10px sans-serif; } .line { fill: none; stroke: #000; stroke-width: 1.5px; } .text { font-size: 20px; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } </style> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <script> var numPoints = 40, random = d3.random.normal(0, .2), data = d3.range(numPoints).map(random); var margin = {top: 20, right: 60, bottom: 20, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.linear() .domain([0, numPoints - 1]) .range([0, width]); var y = d3.scale.linear() .domain([-1, 1]) .range([height, 0]); var line = d3.svg.line() .x(function(d, i) { return x(i); }) .y(function(d, i) { return y(d); }); 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("defs").append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + y(0) + ")") .call(d3.svg.axis().scale(x).orient("bottom")); svg.append("g") .attr("class", "y axis") .call(d3.svg.axis().scale(y).orient("left")); var path = svg.append("g") .attr("clip-path", "url(#clip)") .append("path") .datum(data) .attr("class", "line") .attr("d", line); var textFormat = d3.format(".3f"); var lastPoint = svg.append("text") .attr("dy", ".35em") .style("fill", "red") .attr("class", "text") .attr({ 'x':x(numPoints-1), 'y': y(data[numPoints-1]) }) .text(textFormat(data[numPoints-1])); tick(); function tick() { // push a new data point onto the back data.push(random()); // redraw the line, and slide it to the left path .attr("d", line) .attr("transform", null) .transition() .duration(500) .ease("linear") .attr("transform", "translate(" + x(-1) + ",0)") .each("start", function () { lastPoint .text(textFormat(data[numPoints-2])) .attr({ 'x':x(numPoints-2), 'y': y(data[numPoints-2]) }) .attr("transform", null) .transition() .duration(500) .ease("linear") .text(textFormat(data[numPoints-1])) .attr({ 'x':x(numPoints-1), 'y': y(data[numPoints-1]) }) }) .each("end", tick); // pop the old data point off the front data.shift(); } </script> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM