D3 多行更新路径和转换路径不适用于嵌套数据



我想用过渡更新此图中的圆圈和路径。但是它不起作用。

我对D3不是很有经验。如何使我的代码更好?更改数据结构没有问题。我想使用 exit(( remove(( 和 enter(( 将数据绑定到图形,而不删除整个图形,并在每次再次更新时添加数据。我不知道如何使用 enter(( exit(( 和 remove(( 来存储嵌套数据。一次为整个小组,另一侧更新圆圈和路径。ID 应固定。

在这里,我有一个来自d3noob的小单行示例。


这是一个JS小提琴

var data = [{
    id: 'p1',
    points: [{
      point: {
        x: 10,
        y: 10
      }
    }, {
      point: {
        x: 100,
        y: 30
      }
    }]
  },
  {
    id: 'p2',
    points: [{
        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('dataset contains', data.length, 'item(s)')
  var allGroup = svg.selectAll(".pathGroup").data(data, function(d) {
    return d.id
  });
  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")
    .transition()
    .duration(500)
    .attr("d", function(d) {
      return line(d.points)
    });
  g.selectAll("path")
  .transition()
  .duration(500)
  .attr("d", function(d) {
    return line(d.points)
  });
  g.selectAll("circle")
    .data(d => d.points)
    .enter()
    .append("circle")
    .attr("r", 4)
    .attr("fill", "teal")
    .attr("cx", function(d) {
      return d.point.x
    })
    .attr("cy", function(d) {
      return d.point.y
    })
    .exit().remove()
}
updateGraph()
document.getElementById('update').onclick = function(e) {
  data = [{
      id: 'p1',
      points: [{
        point: {
          x: 10,
          y: 10
        }
      }, {
        point: {
          x: 100,
          y: 30
        }
      }]
    },
    {
      id: 'p2',
      points: [{
          point: {
            x: 30,
            y: 100
          }
        }, {
          point: {
            x: 230,
            y: 30
          }
        },
        {
          point: {
            x: 50,
            y: 300
          }
        },
      ]
    }
  ];
  updateGraph()
}

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

已编辑以将数据连接更改为合适的连接。

我认为这个问题与可变data有关。因此,我添加了data作为函数的参数,并为不同的数据集指定了单独的名称。当我指定不同的数据名称时,我可以更新图表:

var first_data = [{
    id: 'p1',
    points: [{
      point: {
        x: 100,
        y: 10
      }
    }, {
      point: {
        x: 100,
        y: 30
      }
    }]
  },
  {
    id: 'p2',
    points: [{
        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(data) {
  console.log('dataset contains', data.length, 'item(s)')
  var allGroup = svg.selectAll(".pathGroup")
    .data(data, function(d) { return d; });
  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")
    .transition()
    .duration(500)
    .attr("d", function(d) {
        console.log("update path");
      return line(d.points)
    });
  g.selectAll("path")
  .transition()
  .duration(500)
  .attr("d", function(d) {
    return line(d.points)
  });
  g.selectAll("circle")
    .data(d => d.points)
    .enter()
    .append("circle")
    .attr("r", 4)
    .attr("fill", "teal")
    .attr("cx", function(d) {
      return d.point.x
    })
    .attr("cy", function(d) {
      return d.point.y
    })
    .exit().remove()
}
updateGraph(first_data)
document.getElementById('update').onclick = function(e) {
  var update_data = [{
      id: 'p1',
      points: [{
        point: {
          x: 20,
          y: 10
        }
      }, {
        point: {
          x: 100,
          y: 30
        }
      }]
    },
    {
      id: 'p2',
      points: [{
          point: {
            x: 30,
            y: 100
          }
        }, {
          point: {
            x: 230,
            y: 30
          }
        },
        {
          point: {
            x: 50,
            y: 300
          }
        },
      ]
    }
  ];
  updateGraph(update_data)
}

$('#cb1').click(function() {
  if ($(this).is(':checked')) {
   var click_data = [{
        id: 'p1',
        points: [{
          point: {
            x: 10,
            y: 10
          }
        }, {
          point: {
            x: 100,
            y: 30
          }
        }]
      },
      {
        id: 'p2',
        points: [{
            point: {
              x: 30,
              y: 100
            }
          }, {
            point: {
              x: 230,
              y: 30
            }
          },
          {
            point: {
              x: 50,
              y: 200
            }
          },
          {
            point: {
              x: 50,
              y: 300
            }
          },
        ]
      }
    ];
  } else {
    var click_data = [{
      id: 'p1',
      points: [{
        point: {
          x: 10,
          y: 10
        }
      }, {
        point: {
          x: 100,
          y: 30
        }
      }]
    }];
  }
  updateGraph(click_data)
});

小提琴:https://jsfiddle.net/wj266kmx/32/

最新更新