Refactor the attached vis.js program to read the data
from a file called "lab-02.json" and store
the JavaScript in a separate file called lab-02.js
ChatGPT 4o Response
To refactor your lab-01.html file, we can follow these steps:
Move the JavaScript code to a separate file called lab-02.js.
Fetch the node and edge data from a separate lab-02.json file.
Refactor the HTML to reference the new JavaScript file and load the JSON data dynamically.
Here is how we can structure the files:
Refactored HTML (lab-01.html):
1 2 3 4 5 6 7 8 9101112131415161718
<html><head><scripttype="text/javascript"src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script><scripttype="text/javascript"src="network.js"></script><styletype="text/css">#mynetwork{width:600px;height:400px;border:1pxsolidlightgray;}</style></head><body><divid="mynetwork"></div><ahref="lab-01">Back to Lesson</a></body></html>
JavaScript (lab-02.js):
1 2 3 4 5 6 7 8 91011121314151617181920
// Fetch data from the JSON filefetch('lab-02.json').then(response=>response.json()).then(data=>{// Parse the data from JSONconstnodes=newvis.DataSet(data.nodes);constedges=newvis.DataSet(data.edges);// Create a networkconstcontainer=document.getElementById('mynetwork');constnetworkData={nodes:nodes,edges:edges};constoptions={};// Initialize the networkconstnetwork=newvis.Network(container,networkData,options);}).catch(error=>console.error('Error loading the network data:',error));