f5 / unovis

Modular data visualization framework for React, Angular, Svelte, Vue, and vanilla TypeScript or JavaScript
https://unovis.dev
Apache License 2.0
2.23k stars 43 forks source link

Rerendering Whole Network Programatically #362

Closed mossy426 closed 5 months ago

mossy426 commented 6 months ago

We're in the middle of migrating microbetrace.cdc.gov from using d3.js to Unovis (Angular).

I've looked at documentation and gallery examples, but I don't see how I can programatically trigger rerendering of the entire network to, for instance, trigger the nodeLabel function to run on all nodes after I change another variable that the nodeLabel function considers.

For Example:

 nodeLabel = (n: NodeDatum, i: number) => this.getLabelByVariable(i)

 getLabelByVariable( index : number) {

        // If no label variable then should be none
        if (this.widgets['node-label-variable'] == 'None') {
            return '';
        } else {
            return this.session.data.nodes[index][this.widgets['node-label-variable']]
        }
    }

I have another function that changes the 'this.widgets['node-label-variable']', so then I want the nodeLabel function to be called on all nodes. Right now the node/link functions will only tigger for a specific node when I click on a node on the graph. I've tried change detection to see if that would work, but it doesn't appear to do anything either.

Any prompt help would be greatly appreaciated, and love this tool! Thank you.

rokotyan commented 6 months ago

@mossy426 Excited to hear that!

As far as I remember, you can trigger a change detection in Angular to make it re-render the graph:

@reb-dev Can you please check how we do it?

If that doesn't help, you can try updating the entire data object like this.nodes = [...nodes]. This way Angular will understand that the data has been updated and needs to be rendered again. Also feel free to create an online example somewhere (e.g. CodesSandox). It'll make it easier for us to take a look and provide feedback.

mossy426 commented 6 months ago
Screenshot 2024-04-10 at 10 22 20 AM

Appreciate the assistance! Yes I've tried with change detection, and tried both mark for check and detect changes after updating the entire object as you can see in the above and still not getting anything triggered. I'll get to creating an online sample. Thanks!

mossy426 commented 6 months ago

https://stackblitz.com/edit/angular-zyybxk?file=src%2Fforce-graph%2Fforce-graph.component.ts

Here is an example. I change the size variable after button click and now that the variable has been updated I want to update all the nodes.

mossy426 commented 6 months ago

Figured it out! I had to reassign a new data object, rather than a nodes array. Since the entire data object is what's being passed to the graph (not the nodes/links), you must reassign that object instead and it rerenders all nodes/links again.

That being said, the whole network rearranges itself when rerendering, which may prove frustrating when you've moved everything into a specific position.

rokotyan commented 6 months ago

@mossy426 Right! Somehow I didn't think about it. Glad you were able to solve the problem.

rokotyan commented 6 months ago

@reb-dev Can you convert this into a discussion please so the others can see it.