GithubHelp home page GithubHelp logo

scala-force-layout's Introduction

Scala Force Layout

Scala Force Layout is a force-directed graph layout implementation in Scala. The project originally started out as a port of the Springy JavaScript graph layout code by Dennis Hotson. In addition, I added Barnes-Hut simulation to improve performance on bigger graphs (here's a video), and based my physics model parameters on those used in VivaGraphJS by Andrei Kashcha.

Scala Force Layout Example

Getting Started

Create a graph from collections of nodes and edges.

val nodes = Seq(
    Node("id_a", "Node A"),
    Node("id_b", "Node B"),
    Node("id_c", "Node C"),
    Node("id_d", "Node D"))
      
val edges = Seq(
    Edge(nodes(0), nodes(1)),
    Edge(nodes(1), nodes(2)),
    Edge(nodes(2), nodes(3)),
    Edge(nodes(0), nodes(3)))
      
val graph = new SpringGraph(nodes, edges)

Run the layout algorithm using the graph.doLayout() method. Attach onIteration and onComplete handlers to capture intermediate and final results of the layout process.

graph.doLayout(
        onIteration = (it => { ... do something on every layout iteration ... })
        onComplete = (it => { println("completed in " + it + " iterations") }))

Rendering an Image

The ImageRenderer is a simple utility for rendering an image of your graph. If all you want is to store an image of the final layout, this is what you're looking for:

graph.doLayout(
  onComplete = (it => {
    // Renders a 500x500 pixel image of the final graph layout  
    val image = ImageRenderer.drawGraph(graph, 500, 500)
        
    // Writes the image to a PNG file
    ImageIO.write(image, "png", new File("my-graph.png"))
  }))

Opening a Viewer

If you want to open your graph in a window on the screen (with mouse pan and zoom included), use this code:

// Creates a zoom- and pan-able view of the graph
val vis = new BufferedInteractiveGraphRenderer(graph)
  
// Creates a JFrame, with the graph renderer in the content pane
val frame = new JFrame("Les Miserables")
frame.setSize(920, 720)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.getContentPane().add(vis) 
frame.pack()
    
// Pops up the JFrame on the screen, and starts the layout process
frame.setVisible(true)
vis.start

You may also want to take a look at the Hello World and LesMiserables examples for complete, working code.

Current Version

The current version of Scala Force Layout is 0.4.0. Download the jar for Scala 2.10 here: scala-force-layout_2.10-0.4.0.jar, or include it in your SBT project through the Maven Central Repository:

libraryDependencies += "at.ait.dme.forcelayout" % "scala-force-layout_2.10" % "0.4.0"

Building From Source & Running the Examples

Scala Force Layout uses SBT as a build tool. Please refer to the SBT documentation for instructions on how to install SBT on your machine. Once you have installed SBT, you can run the examples by typing sbt run. To build a .jar package type sbt package. To generate a project for the Eclipse IDE, type sbt eclipse.

Future Work

There are many things on the list - feel free to help out if you care to!

  • "The last thing we need is another graph API." // TODO use the Tinkerpop Blueprints graph model
  • "Speed is of the essence." // TODO I'm sure there is much room for performance optimization. Any thoughts & experiences welcome!
  • "Where can I click?" // TODO create a renderer that produces an interactive graph, complete with draggable nodes and such
  • "Sorry, I don't code." // TODO A simple command-line wrapper that opens some GraphSON, with no coding involved, would be nice

License

Scala Force Layout is released under the MIT License.

scala-force-layout's People

Contributors

mathieuleclaire avatar siddhartha-gadgil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

scala-force-layout's Issues

Layout settings

A nice feature would be to render possible to pass layout settings (REPULSION, SPRING_COEFFICIENT, etc) to the doLayout method because the default settings does not fit to all kind of graphs.

Support scala 2.11

I note that the code works with Scala 2.11, but there has been no new release since almost 1.5 years. Could you please do that? I'm working with the source code checked into my project tree for now, since the maven jar files don't work with 2.11 directly.

Sbt deploy

Hi,
thanks for your contribution, it seems very nice.
Would it be possible to deploy your application on a repository ?
I saw a deploy instruction for the sonatype repo in your build.sbt but it does not seem to be deployed yet.
Deploying for scala 2.10 and sbt 0.13 would be very nice !

Thanks
Mathieu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.