I'm using the JUNG API for graph visualization. I cannot figure out how to change the edge label of an edge in the graph.
The situation is that the graph has already been created in the program. I keep dropping edges and nodes and I've found a way to animate those things and update them in the graph. Some of the demos online are helpful. But is there no way to change an edge label of an edge in the graph later?
I understand that JUNG requires the edge labels to be unique.
The basics of edge labelling in JUNG are demonstrated by this snippet of code:
vv.getRenderContext().setEdgeLabelTransformer(new Transformer<MyEdge, String>() {
public String transform(MyEdge e) {
return (e.toString() + " " + e.getWeight() + "/" + e.getCapacity());
}
});
Here, vv is your VisualizationViewer and MyEdge refers to your custom edge class. In my case, I've defined the functions getWeight() and getCapacity() to return the weight and capacity of my edge.
Then I created a popup menu for each edge that allows the user to enter the edge weight and capacity and then used the setWeight() and setCapacity() functions to update my edge. I picked up how exactly to create the edge popups from http://www.grotto-networking.com/JUNG/
You could borrow from this example to set your own edge labels.
Related
I am trying to create a JGraphXAdapter which receives a directed graph as a constructor argument and applied a mxHierarchicalLayout to it. Then, I use mxCellRenderer to create a BufferedImage and write the visualization to a png file.
The problem is that I do not want the edge labels to be visible. I tried (incorrectly) using this command, but it turns off all the labels.
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<>(directedGraph);
graphAdapter.setLabelsVisible(false);
Is there any way to just turn off the edge labels, but not the vertex labels? Thanks
graphAdapter.getEdgeToCellMap().forEach((edge, cell) -> cell.setValue(null));
Edit: there is not much to comment there... "cells" in JGraphX terminology are objects representing visuals of both edges and vertices. So, if you want to change how one specific element is drawn, you should first find its cell through edge->cell or vertex->cell map.
Cell's "value" is its reference to a represented object. Strings that you see on vertices and edges after JGraphX renders everything are basically cell.value == null ? "" : cell.value.toString() (might look a bit different in the source code).
I'm making a stock market game in which the application scrubs the internet for different stocks. I saw an idea for a stock game online, [I do not own this image]:
https://dribbble.com/shots/4161894-Invest-App-UI-Dark-Version
The image on the right has tiny line graphs to show the change in stock value based on previous values of that stock.
and I'm wondering if there is a way to simply make a line graph that applies the values to the previous stock amounts earlier in time using java. I do NOT want it to be a complete graph, with labels and X & Y markers, I just want the line itself to be present
I've tried using JavaFX line graphs, but it had a background and labels and other numbers that I didn't want for the aesthetic.
Basically, the chart consists of many parts (f.i. axis, background grid, legend, graph). Your task is to configure the state of all such that only those you want are showing, in your case (more or less) only the graph.
Astonishingly, nearly everything is configurable to be either visible or not - except for the axis: these have to be manually removed from their parent.
A code snippet to undecorate a LineChart:
protected void undecorate(LineChart chart) {
chart.setLegendVisible(false);
chart.setCreateSymbols(false);
chart.setHorizontalGridLinesVisible(false);
chart.setHorizontalZeroLineVisible(false);
chart.setVerticalGridLinesVisible(false);
chart.setVerticalZeroLineVisible(false);
undecorateAxis(chart.getXAxis());
undecorateAxis(chart.getYAxis());
}
protected void undecorateAxis(Axis axis) {
// trying to hide: not working
// xAxis.setVisible(false);
// configure all ticks/label to not visible doesn't work
// if (axis instanceof ValueAxis)
// ((ValueAxis<?>) axis).setMinorTickVisible(false);
// axis.setTickLabelsVisible(false);
//axis.setTickMarkVisible(false);
// remove from parent does work
Pane parent = (Pane) axis.getParent();
parent.getChildren().removeAll(axis);
}
I'm using a CircleLayout for my graph using Jung2. I overrode the initiate() method so that the vertices are drawn on a certain position in the circle depending on its id. This means that vertices are spread irregular on the circle.
Now I have a problem: because of how the edges are painted, the graph doesn't look like a circle anymore.
is there a way to make the edges look like a circle again?
You need to supply a different (custom) edge renderer; see the code in jung.visualization.renderers for guidance. You supply it to the visualization system as follows:
VisualizationServer.getRenderer().setEdgeRenderer(yourCustomEdgeRenderer);
Alternatively, if you really just want it to look like a circle, you can do this:
(0) Draw a circle using a pre-render Paintable. (Demos show how this works.)
(1) Supply an edge rendering predicate that always returns false, i.e., ensure that none of the edges are rendered.
That will be much easier and simpler than drawing the appropriate arc of a circle in between each pair of connected vertices.
I am trying to allocate fixed coordinate positions for vertices using static layout. Normally we can get vertex coordinate as Point2D object using layout.transform(Vertex);
Now i wanted to initialize a layout and set the vertices at specified positions but somehow i am stuck. I read here on StackOverflow if i implement Transformer<Vertex, Point2D> interface:
Transformer<Vertex, Point2D> locationTransformer =
new Transformer<Vertex, Point2D>() {
#Override
public Point2D transform(Vertex) {
Point2D p2d = //here i calculate the position
return p2D;
}
};
I have tried this. On fixed graph it works but i have an editable graph and on it nothing happens. I was expecting if i have fixed the position in above, then whichever node i add by mouse click, it should go to the fixed position irrespective where i put it?
can you please give me some idea what could be the reason?
Or with editable graph, is it Overriding the position again somewhere?
UPDATE:
If i remove e.g these implementations from visualizaton viewer:
vv.getRenderContext().setVertexFontTransformer(fontTransfoer);
vv.getRenderContext().setVertexFillPaintTransformer(colorTransformer);
vv.getRenderContext().setVertexShapeTransformer(shapeTransformer);
It starts working, but then the nodes have default shape circular red ones. I want to completely redraw the structure that i had drawn last time. So is there something that can be done with this problem?
I have got some issues using the DAGLayout algorithm of JUNG and subsequently reading out the layout coordinates into my own data structure again.
I have got a Network class with lists of Nodes and Edges. To convert this to a JUNG data structure, I create a DirectedSparseMultigraph object and add the edges. e.getSrc() and e.getDest() return Node objects.
DirectedSparseMultigraph<Node, Edge> graph;
for (Edge e : net.getEdges()) {
graph.addEdge(e, e.getSrc(), e.getDest());
}
Then, I apply the layout algorithm.
Layout<Node, Point2D> layout;
layout = new DAGLayout(graph);
After that, I use to layout to get the vertex coordinates.
for (Node node : net.getNodes()) {
Point2D coord = layout.transform(node);
node.setPos((float)coord.getX(), (float)coord.getY());
}
But the Node objects always have (0,0) as (x,y).
Why does this not work this way, and how do I fix it?
I'm not too familiar with JUNG, but I think you have to first specify size of the layout, for example:
layout.setSize(new Dimension(800,600));