Introduction to D3.js and Data Visualization
The graph is not a picture created independently of the data. Its visible objects come from the JSON structure: nodes supply the pages, and links supply the relationships between pages.
From Crawl to Graph
A network visualization is the final stage of a data workflow. The process begins with PageRank information stored in spider.json. You run spjson.py to extract information about the most highly linked pages and write it as JSON. You then open force.html in a browser, where D3.js reads that JSON and displays the pages and their connections as an interactive network graph.
What the JSON Represents
The generated JSON has two main arrays: nodes and links. A node represents a web page and includes properties such as its URL and PageRank score. A link represents a connection between two pages, typically using the indices or IDs of the source and target nodes. D3.js reads these arrays to position the nodes and draw lines between connected pages.
The graph is not a picture created independently of the data. Its visible objects come from the JSON structure: nodes supply the pages, and links supply the relationships between pages.
Generating the Visualization Data
A Complete Conversion Run
You have PageRank data in spider.json and want to view the most highly linked pages as a network graph.
- 1Read the database
The workflow starts with
spider.json, which stores the PageRank database produced from the web crawl. - 2Execute the converter
Run
spjson.pyfrom a command line or terminal. The script extracts information about the most highly linked pages. - 3Write JSON
spjson.pywrites the extracted information in JSON format. The output contains nodes for web pages and links for connections between pages. - 4Open the viewer
Open
force.htmlin a browser.D3.jsreads the generated JSON and renders the network graph. - 5Explore the result
Use the graph's interactions to reposition nodes and reveal the URLs represented by nodes.
The PageRank database has been transformed into a D3.js network visualization.
Reading the Force Layout
When force.html opens, the graph is live and interactive. Its automatic arrangement uses a force-directed layout. Forces push nodes apart to avoid overlap, while links pull connected nodes together. The result is a natural arrangement intended to make the network easier to read.
What do you think happens?
What is the purpose of the forces in the graph layout?
Reveal answer
To separate nodes while keeping connected nodes together
The force-directed layout uses forces that push nodes apart to avoid overlap and links that pull connected nodes together.
Exploring Individual Nodes
The layout gives you a broad view of the network, but a node does not automatically tell you which real web page it represents. Double-click a node to reveal its URL. The URL is essential for identifying the actual page behind that node.
You can also click and drag a node to reposition it. Dragging is useful when nodes overlap, when you want to untangle part of the graph, or when you want to focus on a particular region. The graph's layout can therefore be explored both automatically and through direct manipulation.
When a node matters to your investigation, double-click it to reveal its URL rather than relying only on its position or appearance in the graph. Use dragging to separate it from nearby nodes and make its connections easier to inspect.
Refreshing After a New Crawl
The web crawl can change over time. Running spider.py again may discover new pages or update link information, causing spider.json to grow or change. The existing visualization does not automatically represent those updates. To reflect the new crawl, run spjson.py again and then press refresh in the browser.
Common mistake Refreshing the browser without running spjson.py again
What it looks likeA new crawl has changed spider.json, but the generated JSON has not been regenerated.
Why it failsThe browser refresh can only load the data available to the visualization. It does not perform the conversion from the updated database.
Do this insteadRun spjson.py again first, then press refresh in the browser.
Common mistake Assuming a node's position identifies its web page
What it looks likeA learner sees a prominent node and treats its location as its URL.
Why it failsThe graph position is produced by the force-directed layout and does not itself reveal the real web page.
Do this insteadDouble-click the node to reveal its URL.
Common mistake Treating the graph as a static image
What it looks likeA learner looks at the initial arrangement but does not try to move or inspect nodes.
Why it failsThe
D3.jsvisualization is live and interactive.Do this insteadClick and drag nodes to reposition them, and double-click nodes to identify their URLs.
Apply the Workflow
Imagine that spider.py has discovered additional pages and updated link information. Describe the exact sequence you would use to make the new information appear in the network graph. Then explain which interaction you would use to identify a particular page and which interaction you would use to separate overlapping nodes.
Hints
- Start with the updated
spider.jsondatabase. - Name the script that converts the database into JSON.
- Remember that the browser must be refreshed after the data is regenerated.
- Use double-clicking to reveal a URL and click-and-dragging to reposition a node.
- 1The workflow is: update the crawl, regenerate the visualization data, and refresh the browser.
spider.jsonstores the PageRank database,spjson.pyextracts the most highly linked pages and writes JSON, andforce.htmldisplays that JSON throughD3.js. The JSON contains nodes for pages and links for connections. The force-directed layout arranges the graph, while clicking and dragging changes a node's position and double-clicking reveals its URL.
Key Takeaways
spjson.pyconverts PageRank information inspider.jsoninto JSON for visualization.- The generated JSON contains nodes for web pages and links for connections between pages.
force.htmlusesD3.jsto display the data as an interactive force-directed network graph.- Double-click a node to reveal its URL and click and drag it to reposition it.
- After a new crawl, run
spjson.pyagain and refresh the browser to update the graph.