A graph visualization utility for ontology graphs that provides an interactive React-based web interface for exploring and analyzing graph structures.
# In a pnpm monorepo
pnpm add ontology-plot
# Or with npm
npm install ontology-plot
import { createInstance } from "ontology-plot";
import { createGraph, createNode, createEdge } from "ontology";
// Create a visualization instance
const instance = createInstance();
// Create and plot your graph
const myGraph = createGraph("example", {
nodes: [/* your nodes */],
edges: [/* your edges */]
});
instance.plot(myGraph);
createInstance(options?)
Creates a new visualization instance and starts a web server.
Parameters:
options
(optional):
port?: number
- Port to run the server on (default: 3000)autoOpen?: boolean
- Whether to automatically open the browser (default: true)Returns: PlotInstance
Example:
const instance = createInstance({
port: 8080,
autoOpen: false
});
PlotInstance.plot(graph)
Adds a graph to the visualization interface.
Parameters:
graph: Graph
- The ontology graph to visualizeExample:
instance.plot(myGraph);
PlotInstance.close()
Closes the web server.
Returns: Promise<void>
Example:
await instance.close();
plot(graph, options?)
Convenience function that creates an instance and plots a single graph.
Parameters:
graph: Graph
- The graph to visualizeoptions?
- Same options as createInstance()
Returns: PlotInstance
Example:
import { plot } from "ontology-plot";
const instance = plot(myGraph);
import { createInstance } from "ontology-plot";
import { createGraph, createNode, createEdge } from "ontology";
async function visualizeGraph() {
// Create nodes
const node1 = await createNode("person1", {
name: "Alice",
description: "A software engineer"
});
const node2 = await createNode("person2", {
name: "Bob",
description: "A data scientist"
});
// Create edge
const edge1 = await createEdge("knows", {
name: "knows",
description: "Knows relationship",
sourceId: node1.id,
targetId: node2.id
});
// Create graph
const graph = createGraph("social_network", {
nodes: [node1, node2],
edges: [edge1]
});
// Visualize
const instance = createInstance();
instance.plot(graph);
}
const instance = createInstance();
// Plot multiple graphs
instance.plot(graph1);
instance.plot(graph2);
instance.plot(graph3);
// Users can switch between them in the web interface
const instance = createInstance({
port: 4000, // Custom port
autoOpen: false // Don't auto-open browser
});
console.log("Visit http://localhost:4000 to view graphs");
The web interface provides:
The package works with ontology graphs that have this structure:
interface Graph {
id: string;
nodes: Node[];
edges: Edge[];
}
interface Node {
id: string;
name: string;
description?: string;
properties?: Property[];
embedding: number[];
}
interface Edge {
id: string;
name: string;
description?: string;
sourceId: string;
targetId: string;
properties?: Property[];
embedding: number[];
}
To build the package:
pnpm build
To build only the server:
pnpm build:server
To build only the React frontend:
pnpm build:client
To run the React frontend in development mode:
pnpm dev
To preview the built React app:
pnpm preview
ISC