score:0

edited to change the data join to be be appropriate.

i think the issue has to do with variable data. so, i added data as an argument to the function and specified separate names for different data sets. when i specify different data names, i'm able to update the chart:

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)
});

fiddle: https://jsfiddle.net/wj266kmx/32/


Related Query