search

Home  >  Q&A  >  body text

Vue.js vs. D3.js - forced mocking

I'm currently trying to use Restful-Api calls and use messages to do d3.js forced mocking. The problem is that if I use data from the API, if there isn't any data, the mock is called to handle it. If I wait for the next tick this.$nextTick(simu) all positions will end up being NaN. Is there a reason for this behavior?

const URL = 'https://jsonplaceholder.typicode.com/posts';

new Vue({
  el: '#app',
  data() {
    return {
      webGraph: {
        nodes: [],
        edges: []
      },
      graph1: {
        nodes:[
          {url:2},
          {url:3},
        ],
        edges:[
          {source:2, target:3},
        ]
      }
    }
  },
  created() {
    axios.get(URL).then((response) => {
      let node1 = {
        url: response.data[1].id
      }
      let node2 = {
        url: response.data[2].id
      }
      let edge = {
        source: {url:response.data[1].id},
        target: {url:response.data[2].id}
      }
      this.webGraph.nodes.push(node1)
      this.webGraph.nodes.push(node2)
      this.webGraph.edges.push(edge)
    })
    
  d3.forceSimulation(this.webGraph.nodes)
      .force("charge", d3.forceManyBody().strength(-25))
      .force("link", d3.forceLink().id(d => d.url).links(this.webGraph.edges))
      .on('end', function() {
        console.log("done")
      });
  d3.forceSimulation(this.graph1.nodes)
      .force("charge", d3.forceManyBody().strength(-25))
      .force("link", d3.forceLink().id(d => d.url).links(this.graph1.edges))
      .on('end', function() {
        console.log("done")
      });
  

  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.2.0/d3.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <h6>{{webGraph}}</h6>
  <br> 
  <h6>{{graph1}}</h6>
</div>

P粉378264633P粉378264633273 days ago419

reply all(1)I'll reply

  • P粉204136428

    P粉2041364282024-03-22 10:24:59

    The reason why

    webGraph and graphData1 have different results is because the simulation of webGraph starts before there is no data. If you move the simulation code inside axios.get().then then you will see it working as you expect.

    const URL = 'https://jsonplaceholder.typicode.com/posts';
    
    new Vue({
      el: '#app',
      data() {
        return {
          webGraph: {
            nodes: [],
            edges: []
          },
          graph1: {
            nodes:[
              {url:2},
              {url:3},
            ],
            edges:[
              {source:2, target:3},
            ]
          }
        }
      },
      created() {
        axios.get(URL).then((response) => {
          let node1 = {
            url: response.data[1].id
          }
          let node2 = {
            url: response.data[2].id
          }
          let edge = {
            source: node1,
            target: node2
          }
          
          this.webGraph = {
            nodes: [node1, node2],
            edges: [edge]
          };
          
          d3.forceSimulation(this.webGraph.nodes)
            .force("charge", d3.forceManyBody().strength(-25))
            .force("link", d3.forceLink().id(d => d.url).links(this.webGraph.edges))
            .on('end', function() {
              console.log("done")
            });
        })
    
      d3.forceSimulation(this.graph1.nodes)
          .force("charge", d3.forceManyBody().strength(-25))
          .force("link", d3.forceLink().id(d => d.url).links(this.graph1.edges))
          .on('end', function() {
            console.log("done")
          });
      
    
      }
    })
    sssccc
    sssccc
    sssccc
    
    
    {{webGraph}}

    {{graph1}}

    reply
    0
  • Cancelreply