Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adaptive size not applied #1476

Open
taehun-choi opened this issue Oct 8, 2024 · 1 comment
Open

adaptive size not applied #1476

taehun-choi opened this issue Oct 8, 2024 · 1 comment
Labels

Comments

@taehun-choi
Copy link

I tried to set the same maximum size for the nodes when zooming in using nodeReducer, but updating the size isn't working. Here is a sample of the code.

  • Sigma.js version : 3.0.0-beta.32
  • Graphology version : 0.25.4
  • Operating system: max os sonoma 14.6.1
  • Web browser: safari
  • Steps to reproduce
  1. render topology
  2. zoom in
  • Expected behavior : max size of node should be fixed as 4

  • Actual behavior : actual node size is keep growing as zooming in

  • sample code

     this.graph = graph;

        // Initialize the Sigma instance
        this.sigma = new Sigma(graph, document.getElementById(this.container), {
            nodeProgramClasses: {
                image: createNodeImageProgram(),
            },
            renderEdgeLabels: true,
            nodeReducer: (node, data) => {
                if (this.sigma) {
                    const camera = this.sigma.getCamera();
                    // Adjust size based on zoom level
                    let adjustedSize = SIZE_NODE / camera.ratio;
                    adjustedSize = Math.min(adjustedSize, SIZE_NODE);

                    return {
                        ...data,
                        size: adjustedSize,
                    };
                } else {
                    return {
                        ...data,
                        size: SIZE_NODE,
                    };
                }
            },
            edgeReducer: (edge, data) => {
                if (this.sigma) {
                    const camera = this.sigma.getCamera();
                    // Adjust size based on zoom level
                    let adjustedSize = SIZE_EDGE / camera.ratio;
                    adjustedSize = Math.min(adjustedSize, SIZE_EDGE);

                    return {
                        ...data,
                        size: adjustedSize,
                    };
                } else {
                    return {
                        ...data,
                        size: SIZE_EDGE,
                    };
                }
            },
            // Override hover rendering
            defaultDrawNodeHover: (context, data, settings) => {
                this.customDrawNodeHover(context, data, settings);
            },
            // Override label rendering to draw custom round boxes with text
            defaultDrawNodeLabel: (context, data, settings) => {
                this.customDrawNodeLabel(context, data, settings);
            },
        });

        const fa2Layout = new FA2Layout(graph, {
            settings: {
                barnesHutOptimize: true,
                iterations: 100,
                gravity: 0.2,
                scalingRatio: 2,
                strongGravityMode: true,
                slowDown: 2,
                edgeWeightInfluence: 0.5,
            },
        });

        fa2Layout.start();
@taehun-choi taehun-choi added the bug label Oct 8, 2024
@jacomyal
Copy link
Owner

Hi @taehun-choi,

Thanks for that report. There is one misunderstanding in your code: The node and edge reducers are not re-executed on camera updates. It is on purpose, though: Recalling the reducers means that sigma needs to reindex (or process, as we use internally, see the lifecycle doc) the data , which is expensive. Instead, when the camera is updated, sigma only needs to give to the WebGL programs the new camera position, and it remains quite efficient.

So, using the camera state in the reducers will not work as you expect.

I think the zoomToSizeRatioFunction setting might help you, though:

  • You can set zoomToSizeRatioFunction to () => 1, to get nodes and edges sizes remain unchanged when the user zooms in or out
  • You can set zoomToSizeRatioFunction to (x) => Math.max(1, x) to allow nodes and edges to shrink when the user zooms out, but they will not exceed their initial size when the user zooms in too much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants