Created
August 22, 2012 09:02
-
-
Save MoritzStefaner/3423911 to your computer and use it in GitHub Desktop.
Sample class for rendering a gephi layout with Java/processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.net.URISyntaxException; | |
import org.gephi.data.attributes.api.AttributeController; | |
import org.gephi.data.attributes.api.AttributeModel; | |
import org.gephi.filters.api.FilterController; | |
import org.gephi.graph.api.DirectedGraph; | |
import org.gephi.graph.api.Edge; | |
import org.gephi.graph.api.GraphController; | |
import org.gephi.graph.api.GraphModel; | |
import org.gephi.graph.api.Node; | |
import org.gephi.io.importer.api.Container; | |
import org.gephi.io.importer.api.EdgeDefault; | |
import org.gephi.io.importer.api.ImportController; | |
import org.gephi.io.processor.plugin.DefaultProcessor; | |
import org.gephi.layout.plugin.forceAtlas2.ForceAtlas2; | |
import org.gephi.layout.plugin.forceAtlas2.ForceAtlas2LayoutData; | |
import org.gephi.preview.api.PreviewController; | |
import org.gephi.preview.api.PreviewModel; | |
import org.gephi.project.api.ProjectController; | |
import org.gephi.project.api.Workspace; | |
import org.gephi.ranking.api.RankingController; | |
import org.joda.time.field.OffsetDateTimeField; | |
import org.openide.util.Lookup; | |
import com.itextpdf.text.Font; | |
import processing.core.PApplet; | |
import processing.core.PFont; | |
import processing.core.PVector; | |
public class GACNetworkApp extends PApplet { | |
private GraphModel graphModel; | |
private ForceAtlas2 layout; | |
private float maxY = -10000; | |
private float minX = 10000; | |
private float minY = 10000; | |
private float maxX = -10000; | |
private PFont regularFont; | |
@Override | |
public void setup() { | |
size(1000, 1000, OPENGL); | |
regularFont = loadFont("HelveticaNeue-Medium-48.vlw"); | |
//Init a project - and therefore a workspace | |
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); | |
pc.newProject(); | |
Workspace workspace = pc.getCurrentWorkspace(); | |
//Get models and controllers for this new workspace - will be useful later | |
AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel(); | |
graphModel = Lookup.getDefault().lookup(GraphController.class).getModel(); | |
PreviewModel model = Lookup.getDefault().lookup(PreviewController.class).getModel(); | |
ImportController importController = Lookup.getDefault().lookup(ImportController.class); | |
FilterController filterController = Lookup.getDefault().lookup(FilterController.class); | |
RankingController rankingController = Lookup.getDefault().lookup(RankingController.class); | |
//Import file | |
Container container = null; | |
File file = null; | |
try { | |
file = new File(getClass().getResource("/<your gephi file>.gexf").toURI()); | |
} catch (URISyntaxException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
try { | |
container = importController.importFile(file); | |
} catch (FileNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED); //Force DIRECTED | |
//Append imported data to GraphAPI | |
importController.process(container, new DefaultProcessor(), workspace); | |
//See if graph is well imported | |
DirectedGraph graph = graphModel.getDirectedGraph(); | |
System.out.println("Nodes: " + graph.getNodeCount()); | |
System.out.println("Edges: " + graph.getEdgeCount()); | |
layout = new ForceAtlas2(null); | |
layout.setGraphModel(graphModel); | |
layout.resetPropertiesValues(); | |
layout.setOutboundAttractionDistribution(false); | |
((ForceAtlas2) layout).setEdgeWeightInfluence(1.5d); | |
((ForceAtlas2) layout).setGravity(10d); | |
((ForceAtlas2) layout).setJitterTolerance(.02); | |
((ForceAtlas2) layout).setScalingRatio(15.0); | |
layout.initAlgo(); | |
} | |
@Override | |
public void draw() { | |
background(255); | |
textFont(regularFont); | |
noStroke(); | |
minX = 10000; | |
minY = 10000; | |
maxX = -10000; | |
maxY = -10000; | |
float x1, x2, y1, y2, w; | |
for (Node n : graphModel.getDirectedGraph().getNodes()) { | |
minX = Math.min(minX, n.getNodeData().x()); | |
maxX = Math.max(maxX, n.getNodeData().x()); | |
minY = Math.min(minY, n.getNodeData().y()); | |
maxY = Math.max(maxY, n.getNodeData().y()); | |
} | |
for (Node n : graphModel.getDirectedGraph().getNodes()) { | |
x1 = x(n.getNodeData().x()); | |
y1 = y(n.getNodeData().y()); | |
w = (float) Math.sqrt(1.0f * (Integer) n.getNodeData().getAttributes().getValue("Weighted In-Degree")); | |
fill(30, w * 10 + 90); | |
ellipse(x1, y1, 5, 5); | |
textSize((int) (1.5f * w + 3)); | |
String l = n.getNodeData().getAttributes().getValue("label").toString(); | |
float offset = map(x1 - width * .5f, width * .5f, -width * .5f, 0f, textWidth(l)); | |
text(l, x1 - offset, y1 - 2); | |
((ForceAtlas2LayoutData) n.getNodeData().getLayoutData()).mass = 1 + w * 5; | |
} | |
layout.goAlgo(); | |
PVector normal = new PVector(); | |
for (Edge e : graphModel.getDirectedGraph().getEdges()) { | |
x1 = x(e.getEdgeData().getSource().x()); | |
x2 = x(e.getEdgeData().getTarget().x()); | |
y1 = y(e.getEdgeData().getSource().y()); | |
y2 = y(e.getEdgeData().getTarget().y()); | |
normal.set(y2 - y1, -(x2 - x1), 0f); | |
normal.normalize(); | |
w = (Float) e.getEdgeData().getAttributes().getValue("weight"); | |
beginShape(); | |
fill(90, 80, 70, w * 30); | |
vertex(x2 + normal.x * w, y2 + normal.y * w); | |
vertex(x1, y1); | |
vertex(x2 - normal.x * w, y2 - normal.y * w); | |
endShape(CLOSE); | |
} | |
} | |
private float x(float x) { | |
return map(x, minX, maxX, 50, width - 100); | |
} | |
private float y(float y) { | |
return map(y, minY, maxY, 50, height - 100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment