javascript - d3 更新图形中的路径和圆圈

标签 javascript d3.js graph

我想更新图表,但它不起作用。我想更新线条和圆圈。我尝试添加

.exit().remove()

更新圈子和

  g.selectAll("path").attr("d", line);

更新路径。

但是它不起作用。

用 exit().remove() 更新外部组工作正常。 (本例中的复选框)。

仅更新路径和圆是行不通的。 (本例中的更新按钮)

我不想删除图中的所有线并再次附加它,因为我想在数据更改时添加过渡。

这是一个 JS fiddle :LINK 这是我的代码:

var data = [
  [{
    point: {
      x: 10,
      y: 10
    }
  }, {
    point: {
      x: 100,
      y: 30
    }
  }],
  [{
      point: {
        x: 30,
        y: 100
      }
    }, {
      point: {
        x: 230,
        y: 30
      }
    },
    {
      point: {
        x: 50,
        y: 200
      }
    },
    {
      point: {
        x: 50,
        y: 300
      }
    },
  ]
];

var svg = d3.select("svg");

var line = d3.line()
  .x((d) => d.point.x)
  .y((d) => d.point.y);

function updateGraph() {
  console.log(data)
  var allGroup = svg.selectAll(".pathGroup").data(data);
  var g = allGroup.enter()
    .append("g")
    .attr("class", "pathGroup")
  allGroup.exit().remove()

  g.append("path")
    .attr("class", "line")
    .attr("stroke", "red")
    .attr("stroke-width", "1px")
    .attr("d", line);

  g.selectAll("path").attr("d", line);

  g.selectAll(null)
    .data(d => d)
    .enter()
    .append("circle")
    .attr("r", 4)
    .attr("fill", "teal")
    .attr("cx", d => d.point.x)
    .attr("cy", d => d.point.y)
    .exit().remove()

}
updateGraph()

document.getElementById('update').onclick = function(e) {

  data = [
    [{
      point: {
        x: 10,
        y: 10
      }
    }, {
      point: {
        x: 100,
        y: 30
      }
    }],
    [{
        point: {
          x: 30,
          y: 100
        }
      }, {
        point: {
          x: 230,
          y: 30
        }
      },
      {
        point: {
          x: 50,
          y: 300
        }
      },
    ]
  ];
  updateGraph()
}


$('#cb1').click(function() {
  if ($(this).is(':checked')) {
    data = [
      [{
        point: {
          x: 10,
          y: 10
        }
      }, {
        point: {
          x: 100,
          y: 30
        }
      }],
      [{
          point: {
            x: 30,
            y: 100
          }
        }, {
          point: {
            x: 230,
            y: 30
          }
        },
        {
          point: {
            x: 50,
            y: 200
          }
        },
        {
          point: {
            x: 50,
            y: 300
          }
        },
      ]
    ];

  } else {
    data = [
      [{
        point: {
          x: 10,
          y: 10
        }
      }, {
        point: {
          x: 100,
          y: 30
        }
      }]
    ];
  }
  updateGraph()
});

最佳答案

问题

allGroup.exit().remove() 什么都不做的原因是更新后的数据集仍然具有与原始数据集相同数量的项目。因此退出选择是空的。

变量 data 包含线,而不是点。在页面加载时定义的和 update 监听器中的包含两行,只是其中的点数不同。

您可以通过在函数 updateGraph 中放置一个 console.log(data.length) 来检查这一点。

方案一

改变你的数据结构。您可以为每一行分配一个 id 属性,并使用 .datakey 函数。比照。 d3-selection documentation .

更新了 jsFiddle 实现解决方案 1:see here .

此解决方案需要较少的更改。

解决方案2

如果您无法控制数据结构,您可以在 update 选择中转换线条图,而不是 exit 选择。

关于javascript - d3 更新图形中的路径和圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49820455/

相关文章:

javascript - 使用 d3 没有子节点和父节点的树

python - 如何使用顶点和边的分类属性在图形工具中绘制颜色?

plot - 用阴影线填充绘制不等式的最简单方法?

algorithm - 寻找有界子图之间的最小割集

javascript - 使用backbone和Express渲染模板

javascript - 将 css 添加到谷歌地图 v3 多段线?

javascript - nvd3折线图自动设置填充不透明度

javascript - 组合 geojson 和 csv,然后使用新数据设置 map 样式

javascript - Chrome : background-images not rendered after refreshing page + javascript redirect

javascript - 在不访问对象的情况下以编程方式编辑 CodeMirror 内容