DerYeger/yeger

bug (d3-graph-controller): PositionInitializers.Stable rotate graph a little after update

Closed this issue · 2 comments

Affected Packages

  • d3-graph-controller

Description

When I use PositionInitializers.Stable graph rotating a little bit after each update

Reproduction

import { defineGraphConfig, PositionInitializers } from "d3-graph-controller";

export const config = (oldGraph?: Graph<ReatomEntity, EntityNode, EntityLink>) =>
  defineGraphConfig<ReatomEntity, EntityNode, EntityLink>({
    nodeRadius: (node: EntityNode) => 32,
    positionInitializer: oldGraph
      ? PositionInitializers.Stable(oldGraph)
      : PositionInitializers.Centered,
  });

https://codesandbox.io/p/sandbox/reatom-reactivity-rotate-bug-4645jr?file=%2Fsrc%2Fmain.tsx

Additional context

Perhaps I am doing something wrong, but I have not found any mention of this type of positioning in the documentation.

I used this code as example for archive stable positioning between updates

Preferences

  • I want to be assigned to and work on this issue myself

Yes, PositionInitializer.Stable is not yet documented.

An example usage can be seen here:

resetGraphController(PositionInitializers.Stable(oldGraph))

In short, the initializer has to be created by passing an existing graph.
It then matches corresponding nodes by their ids and uses existing position values.

I guess editor didn't report the type issue here.
Unlike the other initializers, PositionInitializer.Stable is a function that returns the actual initializer.

I missed the edit of the issue, it's usually better to create a new issue.

Regarding the rotation, this is caused by the simulation being "reheated" (as d3 calls it) and a new node affecting the position of the existing nodes.
You can configure the initialize alpha to reduce the rotation.