Skip to content

Commit

Permalink
Radial layout: center on root option (#941)
Browse files Browse the repository at this point in the history
* Add option to control the initial angle of a radial layout

Signed-off-by: Max Kasperowski <[email protected]>

* Adds option to center a radial layout on its root node

This is done by increasing the overall size of the layout and shifting
the entire layout such that the root node is positioned exactly in the
center.

Signed-off-by: Max Kasperowski <[email protected]>

* PR feedback

Signed-off-by: Max Kasperowski <[email protected]>

* Add tests project for radial layout and add tests for center on root
option

Signed-off-by: Max Kasperowski <[email protected]>

* fix years and names

---------

Signed-off-by: Max Kasperowski <[email protected]>
  • Loading branch information
Eddykasp authored Jul 6, 2023
1 parent d154b63 commit e878d4c
Show file tree
Hide file tree
Showing 13 changed files with 622 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@ algorithm radial(RadialLayoutProvider) {
supports radius
supports sorter
supports wedgeCriteria
supports centerOnRoot
}

/////////////////////////////////Options///////////////////////////////
option centerOnRoot: boolean {
label "Center On Root"
description
"Centers the layout on the root of the tree i.e. so that the central node is also the center node of the
final layout. This introduces additional whitespace."
default = false
targets parents
}
option orderId: int {
label "Order ID "
description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*******************************************************************************/
package org.eclipse.elk.alg.radial.intermediate;

import org.eclipse.elk.alg.radial.InternalProperties;
import org.eclipse.elk.alg.radial.options.RadialOptions;
import org.eclipse.elk.core.alg.ILayoutProcessor;
import org.eclipse.elk.core.math.ElkMargin;
import org.eclipse.elk.core.math.ElkPadding;
Expand Down Expand Up @@ -48,6 +50,45 @@ public void process(final ElkNode graph, final IElkProgressMonitor progressMonit

ElkPadding padding = graph.getProperty(CoreOptions.PADDING);
KVector offset = new KVector(minXPos - padding.getLeft(), minYPos - padding.getTop());


double width = maxXPos - minXPos + padding.getHorizontal();
double height = maxYPos - minYPos + padding.getVertical();

if (graph.getProperty(RadialOptions.CENTER_ON_ROOT)) {
ElkNode root = graph.getProperty(InternalProperties.ROOT_NODE);
ElkMargin rootMargins = root.getProperty(CoreOptions.MARGINS);
// calculate the current midpoint of the root, taking into account the defined margins and the already
// calculated offset necessary to shift the graph into the positive quadrant of the coordinate system
double rootX = root.getX() + root.getWidth()/2 + (rootMargins.left + rootMargins.right)/2 - offset.x;
double rootY = root.getY() + root.getHeight()/2 + (rootMargins.top + rootMargins.bottom)/2 - offset.y;

double dx = width - rootX;
double dy = height - rootY;

if (dx < width / 2) {
// need to add additional space on the left
double additionalX = dx - rootX;
width += additionalX;
offset.x -= additionalX;
} else {
// add addtional space on the right
double additionalX = rootX - dx;
width += additionalX;
}

if (dy < height / 2) {
//need to add additional space on the top
double additionalY = dy - rootY;
height += additionalY;
offset.y -= additionalY;
} else {
// add addtional space on the bottom
double additionalY = rootY - dy;
height += additionalY;
}

}

// process the nodes
for (ElkNode node : graph.getChildren()) {
Expand All @@ -57,12 +98,12 @@ public void process(final ElkNode graph, final IElkProgressMonitor progressMonit
}

// set up the graph
double width = maxXPos - minXPos + padding.getHorizontal();
double height = maxYPos - minYPos + padding.getVertical();
if (!graph.getProperty(CoreOptions.NODE_SIZE_FIXED_GRAPH_SIZE)) {
graph.setWidth(width);
graph.setHeight(height);
}

// store child area info
graph.setProperty(CoreOptions.CHILD_AREA_WIDTH, width - padding.getHorizontal());
graph.setProperty(CoreOptions.CHILD_AREA_HEIGHT, height - padding.getVertical());
progressMonitor.logGraph(graph, "After");
Expand Down
11 changes: 11 additions & 0 deletions test/org.eclipse.elk.alg.radial.test/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
40 changes: 40 additions & 0 deletions test/org.eclipse.elk.alg.radial.test/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.elk.alg.radial.test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
</natures>
</projectDescription>
Loading

0 comments on commit e878d4c

Please sign in to comment.