score:5

Accepted answer

i had the same issue a few days ago. the way i did it is as follows:

we have two .join, the parent one is for the stack and the child is for the rectangles.

in the enter of the parent join, we call the updaterects in order to draw the rectangles for the first time, this updaterects function will do the child .join, this second join function will draw the rectangles.

for the update we do the same, but instead of doing it in the enter function of the join, we do it in the update.

also, my svg is structured in a different way, i have a stacks groups, then i have the stack group, and a bars group, in this bars group i add the rectangles. in the fiddle below, you can see that i added the parent group with the class stack.

the two functions are below:

updatestack:

  function updatestack(data) {
    // update the y scale
    y.domain([0, d3.max(data.map((d) => d.sum))]);
    g.select(".y").transition().duration(1000).call(yaxis);

    const stackdata = stack(data);

    stackdata.foreach((stackedbar) => {
      stackedbar.foreach((stack) => {
        stack.id = `${stackedbar.key}-${stack.data.year}`;
      });
    });

    let bars = g
      .selectall("g.stacks")
      .selectall(".stack")
      .data(stackdata, (d) => {
        return d.key;
      });

    bars.join(
      (enter) => {
        const barsenter = enter.append("g").attr("class", "stack");

        barsenter
          .append("g")
          .attr("class", "bars")
          .attr("fill", (d) => {
            return color(d.key);
          });

        updaterects(barsenter.select(".bars"));

        return enter;
      },
      (update) => {
        const barsupdate = update.select(".bars");
        updaterects(barsupdate);
      },
      (exit) => {
        return exit.remove();
      }
    );
  }

updaterects:

  function updaterects(childrects) {
    childrects
      .selectall("rect")
      .data(
        (d) => d,
        (d) => d.id
      )
      .join(
        (enter) =>
          enter
            .append("rect")
            .attr("id", (d) => d.id)
            .attr("class", "bar")
            .attr("x", (d) => x(d.data.year))
            .attr("y", y(0))
            .attr("width", x.bandwidth())
            .call((enter) =>
              enter
                .transition()
                .duration(1000)
                .attr("y", (d) => y(d[1]))
                .attr("height", (d) => y(d[0]) - y(d[1]))
            ),
        (update) =>
          update.call((update) =>
            update
              .transition()
              .duration(1000)
              .attr("y", (d) => y(d[1]))
              .attr("height", (d) => y(d[0]) - y(d[1]))
          ),
        (exit) =>
          exit.call((exit) =>
            exit
              .transition()
              .duration(1000)
              .attr("y", height)
              .attr("height", height)
          )
      );
  }

here is an update jsfiddle https://jsfiddle.net/5oqwlxdj/1/

i hope it helps.

score:0

it is getting called, but you can't see it. try changing .attr(d => console.log('update stack')) to .attr(console.log('update stack')).


Related Query