springy

A force directed graph layout algorithm in JavaScript

  • 所有者: dhotson/springy
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Springy

A force directed graph layout algorithm in JavaScript.

http://getspringy.com/

What is this?

Springy is a force directed graph layout algorithm.

So what does this 'force directed' stuff mean anyway? Excellent question!

It basically means that it uses some real world physics to try and
figure out how to show a network graph in a nice way.

Try to imagine it as a bunch of springs connected to each other.

Demo

basic, simplified API, JSON API

Getting Started

springy.js by itself is quite plain and doesn't include any code to do rendering
or drag and drop etc. It's just for calculating the layout.

The drawing and interaction stuff is mostly up to you.

However, I've written a little helper jQuery plugin called springyui.js
to help get you started. It's got a semi-decent default renderer and some
half assed drag and drop.

Basic Usage

See demo.html for the way to
add nodes and edges to graph and springyui.js for the rendering example.

Springy 1.1+ supports simplified API for adding nodes and edges, see
demo-simple.html:

var graph = new Springy.Graph();
graph.addNodes('mark', 'higgs', 'other', 'etc');
graph.addEdges(
    ['mark', 'higgs'],
    ['mark', 'etc'],
    ['mark', 'other']
);

Springy 1.2+ also accepts JSON, see
demo-json.html:

graphJSON = {
  "nodes": ["mark", "higgs", "other", "etc"],
  "edges": [
    ["mark", "higgs"],
    ["mark", "etc"],
    ["mark", "other"]
  ]
};

var graph = new Springy.Graph();
graph.loadJSON(graphJSON);

Advanced Drawing

If you're keen to do your own custom drawing, you'll need to know a few
things before you get started.

This is the basic graph API, you can create nodes and edges etc.

// make a new graph
var graph = new Springy.Graph();

// make some nodes
var node1 = graph.newNode({label: '1'});
var node2 = graph.newNode({label: '2'});

// connect them with an edge
graph.newEdge(node1, node2);

So now to draw this graph, lets make a layout object:

var layout = new Springy.Layout.ForceDirected(graph, 400.0, 400.0, 0.5);

I've written a Renderer class, which will handle the rendering loop.
You just need to provide some callbacks to do the actual drawing.

var renderer = new Springy.Renderer(layout,
  function clear() {
    // code to clear screen
  },
  function drawEdge(edge, p1, p2) {
    // draw an edge
  },
  function drawNode(node, p) {
    // draw a node
  }
);

Now, just start the rendering loop:

renderer.start();

Further Reading

Have a look at the code in springy.js.
Seriously, it's not very much code and it should be pretty easy to understand.

Please let me know if anything is unclear. Feedback is welcome.

Acknowledgements

Thanks to Lachlan Donald for his helpful suggestions and
feedback.

主要指標

概覽
名稱與所有者dhotson/springy
主編程語言JavaScript
編程語言JavaScript (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2010-04-16 02:19:49
推送於2024-06-06 21:13:41
最后一次提交2018-03-06 11:32:22
發布數13
最新版本名稱2.8.0 (發布於 )
第一版名稱v2.0.1 (發布於 )
用户参与
星數1.9k
關注者數42
派生數237
提交數136
已啟用問題?
問題數71
打開的問題數22
拉請求數25
打開的拉請求數7
關閉的拉請求數11
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?