简体   繁体   English

D3在动态折线图中添加点

[英]D3 Adding dots to dynamic line chart

I have a scrolling line chart that is being updated in realtime as data comes in. Here is the js fiddle for the line chart. 我有一个滚动折线图,随着数据的输入实时更新。这是折线图的js小提琴。 https://jsfiddle.net/eLu98a6L/ https://jsfiddle.net/eLu98a6L/

What I would like to do is replace the line with a dot graph, so each point that comes in would create a dot, and the scrolling feature is maintained.This is the type of chart I would like to create dow jones dot graph and ultimately I would like to remove the line underneath. 我想做的是用点图代替该线,因此每个点都会创建一个点,并保持滚动功能。这是我要创建的道琼斯点图的图表类型 ,最终我想删除下面的线。

This is the code I have used to try and add dots to my graph. 这是我用来尝试在图形中添加点的代码。

g.append("g")
.selectAll("dot")
.data(4)
.enter("circle")
.attr("cx", "2")
.attr("cy", "2");

So far I haven't had any success. 到目前为止,我还没有取得任何成功。 I'm very new to d3, so any help is appreciated. 我是d3的新手,因此能提供任何帮助。 Thanks! 谢谢!

Based on your code an approach for this can be : 根据您的代码,可以采用以下方法:

var circleArea = g.append('g'); /* added this to hold all dots in a group */
function tick() {

  // Push a new data point onto the back.
  data.push(random());
  // Redraw the line.

  /* hide the line as you don't need it any more 
  d3.select(this)
      .attr("d", line)
      .attr("transform", null);
  */

  circleArea.selectAll('circle').remove(); // this makes sure your out of box dots will be remove.

  /* this add dots based on data */
  circleArea.selectAll('circle')
    .data(data)
    .enter()
         .append('circle')
         .attr('r',3)  // radius of dots 
         .attr('fill','black') // color of dots
         .attr('transform',function(d,i){ return 'translate('+x(i)+','+y(d)+')';}); 

  /* here we can animate dots to translate to left for 500ms */
  circleArea.selectAll('circle').transition().duration(500)
    .ease(d3.easeLinear)
    .attr('transform',function(d,i){ 
         if(x(i )<=0) {   // this makes sure dots are remove on x=0
              d3.select(this).remove();
         }
         return 'translate('+x(i-1)+','+y(d)+')';
    }); 

   /* here is rest of your code */ 
  // Slide it to the left.
  d3.active(this)
      .attr("transform", "translate(" + x(-1) + ",0)")
      .transition()
      .on("start", tick);
  // Pop the old data point off the front.
  data.shift();
}

See it in action : https://codepen.io/FaridNaderi/pen/weERBj 运行中查看它: https : //codepen.io/FaridNaderi/pen/weERBj

hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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