d3带标签的实时折线图

时间:2015-08-20 06:33:37

标签: javascript d3.js svg

我正在使用以下http://bost.ocks.org/mike/path/

创建折线图

现在我想在折线图曲线上显示最后一个数据的值,即添加到图表中的新值。所以我尝试了这个代码,它添加了标签,但是当添加新数据时,图表不会更新。 注意:我的数据长度为13.我正在尝试仅显示最后一个数据值。

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

1 个答案:

答案 0 :(得分:0)

即使你解决了这个问题,这里还有一个代码片段,以防万一其他人正在寻找(因为我在你发表评论时我正在发帖)。

<!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>