Skip to content

Latest commit

History

64 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Graphoon

VersionLicense

A force directed graph algorithm written in Lua.

example

Introduction

Graphoon emerged from the graph calculation code used in both LoGiVi and LoFiVi.

A force directed graph layout is achieved by simulating physical forces, which push and pull each node in the graph until a nice layout is found.

Basic Usage

The basic idea is that you create a new graph object, to which you can then add nodes and edges.

localGraphLibrary=require('Graphoon').Graphgraph=GraphLibrary.new()
graph:addNode( "Ash Williams" )
graph:addNode( "Necronomicon" )
graph:connectIDs( "Ash Williams", "Necronomicon" )

By itself Graphoon only provides functionality for creating the graph and calculating the layout based on physical attraction and repulsion forces.

It provides a draw and update function, which can be used to easily write your own rendering code.

The draw function should be called with two callback functions. The first callback will be used for all nodes and the second one for all the edges.

graph:draw( function( node )
localx, y=node:getPosition()
drawCircle( 'fill', x, y, 10 )
end,
function( edge )
localox, oy=edge.origin:getPosition()
localtx, ty=edge.target:getPosition()
drawLine( ox, oy, tx, ty )
end)

At its simplest the force calculations can be updated via graph:update( dt ), but the update function also can receive optional callbacks for both nodes and edges.

Advanced usage

Using anchors

Anchors can be used to attach a node to a certain position on the screen. This can be useful if you want to center a certain node for example.

This can either be done directly via the constructor of the node:

-- Anchor the node to the center of the screen.graph:addNode( "Ash Williams", screenX*0.5, screenY*0.5, true )

Or by using the setAnchor function:

-- Invert anchor statusnode:setAnchor( notnode:isAnchor(), mouseX, mouseY )

Using custom classes for Nodes and Edges

If you prefer to not touch the default classes, you can simply inherit from them and tell Graphoon to use your custom classes instead.

localGraphLibraryNode=require('lib.libfdgraph.fd').NodelocalCustomNodeClass= {}
-- You can pass additional arguments to your custom class. Just make sure the-- default parameters ar in the right order.functionCustomNodeClass.new( id, x, y, anchor, ... )
localself=GraphLibraryNode.new( id, x, y, anchor )
-- ... Custom codeendreturnCustomNodeClass
localGraphLibrary=require('Graphoon').GraphGraphLibrary.setNodeClass( require('CustomNodeClass') )

About

A force directed graph algorithm written in Lua.

Topics

Resources

Stars

28 stars

Watchers

4 watching

Forks

Releases

Packages

Contributors

Languages