Skip to content

Commit

Permalink
Create app.js
Browse files Browse the repository at this point in the history
Explanation of Code:
HTML (index.html):

Includes D3.jsand sets up a basic HTML structure with a div (#viz) to hold the visualization.

CSS (style.css):

Adds basic styles for the body and visualization elements.

JavaScript (app.js):

Data Structure: stateMachineData contains sample nodes and links representing states and transitions.

SVG Canvas: Sets up an SVG canvas with defined width and height.

Simulation: Uses D3.jsforce simulation to position nodes and links.

Nodes and Links: Appends circles and text to represent states, and lines to represent transitions.

Tick Function: Updates positions of nodes and links on each tick of the simulation.
  • Loading branch information
adityakrmishra authored Jan 10, 2025
1 parent 62b55ca commit 825ccb7
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions visualization/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Sample state machine data (for demonstration)
const stateMachineData = {
nodes: [
{ id: 'RedState', group: 1 },
{ id: 'YellowState', group: 1 },
{ id: 'GreenState', group: 1 }
],
links: [
{ source: 'RedState', target: 'YellowState', value: 1 },
{ source: 'YellowState', target: 'GreenState', value: 1 }
]
};

// Setup the SVG canvas dimensions
const width = 800;
const height = 600;

const svg = d3.select("#viz")
.append("svg")
.attr("width", width)
.attr("height", height);

const simulation = d3.forceSimulation(stateMachineData.nodes)
.force("link", d3.forceLink(stateMachineData.links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-400))
.force("center", d3.forceCenter(width / 2, height / 2));

const link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(stateMachineData.links)
.enter().append("line")
.attr("class", "link");

const node = svg.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(stateMachineData.nodes)
.enter().append("g");

node.append("circle")
.attr("r", 10)
.attr("fill", d => '#69b3a2');

node.append("text")
.attr("dy", -10)
.text(d => d.id);

simulation
.nodes(stateMachineData.nodes)
.on("tick", ticked);

simulation.force("link")
.links(stateMachineData.links);

function ticked() {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node
.attr("transform", d => `translate(${d.x},${d.y})`);
}

0 comments on commit 825ccb7

Please sign in to comment.