//Init a project - and therefore a workspace ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); pc.newProject(); Workspace workspace = pc.getCurrentWorkspace();
//B. GraphModel是全局都需要的
//Get models and controllers for this new workspace - will be useful later GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
//C. 数据读入部分
ImportController importController = Lookup.getDefault().lookup(ImportController.class); //Import file Container container; try { File file = new File(getClass().getResource("/org/gephi/toolkit/demos/polblogs.gml").toURI()); container = importController.importFile(file); container.getLoader().setEdgeDefault(EdgeDirectionDefault.DIRECTED); //Force DIRECTED } catch (Exception ex) { ex.printStackTrace(); return; } //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());
//D. 过滤部分
FilterController filterController = Lookup.getDefault().lookup(FilterController.class); //Filter DegreeRangeFilter degreeFilter = new DegreeRangeFilter(); degreeFilter.init(graph); degreeFilter.setRange(new Range(3, Integer.MAX_VALUE)); //Remove nodes with degree < 30 Query query = filterController.createQuery(degreeFilter); GraphView view = filterController.filter(query); graphModel.setVisibleView(view); //Set the filter result as the visible view
//Run YifanHuLayout for 100 passes - The layout always takes the current visible view YifanHuLayout layout = new YifanHuLayout(null, new StepDisplacement(1f)); layout.setGraphModel(graphModel); layout.resetPropertiesValues(); layout.setOptimalDistance(200f);
layout.initAlgo(); for (int i = 0; i < 100 && layout.canAlgo(); i++) { layout.goAlgo(); } layout.endAlgo();
//Get Centrality GraphDistance distance = new GraphDistance(); distance.setDirected(true); distance.execute(graphModel);
//Rank color by Degree Function degreeRanking = appearanceModel.getNodeFunction(graph, AppearanceModel.GraphFunction.NODE_DEGREE, RankingElementColorTransformer.class); RankingElementColorTransformer degreeTransformer = (RankingElementColorTransformer) degreeRanking.getTransformer(); degreeTransformer.setColors(new Color[]{new Color(0xFEF0D9), new Color(0xB30000)}); degreeTransformer.setColorPositions(newfloat[]{0f, 1f}); appearanceController.transform(degreeRanking); //Rank size by centrality Column centralityColumn = graphModel.getNodeTable().getColumn(GraphDistance.BETWEENNESS); Function centralityRanking = appearanceModel.getNodeFunction(graph, centralityColumn, RankingNodeSizeTransformer.class); RankingNodeSizeTransformer centralityTransformer = (RankingNodeSizeTransformer) centralityRanking.getTransformer(); centralityTransformer.setMinSize(3); centralityTransformer.setMaxSize(10); appearanceController.transform(centralityRanking);
//F. 最后展现时图的模式:点、边的样式
PreviewModel model = Lookup.getDefault().lookup(PreviewController.class).getModel(); //Preview model.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE); model.getProperties().putValue(PreviewProperty.EDGE_COLOR, new EdgeColor(Color.GRAY)); model.getProperties().putValue(PreviewProperty.EDGE_THICKNESS, new Float(0.1f)); model.getProperties().putValue(PreviewProperty.NODE_LABEL_FONT, model.getProperties().getFontValue(PreviewProperty.NODE_LABEL_FONT).deriveFont(8));
publicvoidscript(){ //Init a project - and therefore a workspace ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); pc.newProject(); Workspace workspace = pc.getCurrentWorkspace();
//New Processing target, get the PApplet G2DTarget target = (G2DTarget) previewController.getRenderTarget(RenderTarget.G2D_TARGET); final PreviewSketch previewSketch = new PreviewSketch(target); previewController.refreshPreview();
//Add the applet to a JFrame and display JFrame frame = new JFrame("Test Preview"); frame.setLayout(new BorderLayout());
//Wait for the frame to be visible before painting, or the result drawing will be strange frame.addComponentListener(new ComponentAdapter() { @Override publicvoidcomponentShown(ComponentEvent e){ previewSketch.resetZoom(); } }); frame.setVisible(true); }
publicstaticvoidmain(String[] args){ PreviewJFrame previewJFrame = new PreviewJFrame(); previewJFrame.script(); }