-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add a 'direction' option for breadthfirst layout that can be 'top-bottom', 'bottom-top', 'left-right', or 'right-left' #3373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
soetji
wants to merge
4
commits into
cytoscape:unstable
Choose a base branch
from
soetji:feature/breadthfirst-direction
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import * as is from '../../is.mjs'; | |
const defaults = { | ||
fit: true, // whether to fit the viewport to the graph | ||
directed: false, // whether the tree is directed downwards (or edges can point in any direction if false) | ||
direction: 'top-bottom', // 'top-bottom', 'bottom-top', 'left-right', or 'right-left' | ||
padding: 30, // padding on fit | ||
circle: false, // put depths in concentric circles if true, put depths top down if false | ||
grid: false, // whether to create an even grid into which the DAG is placed (circle:false only) | ||
|
@@ -48,10 +49,8 @@ BreadthFirstLayout.prototype.run = function(){ | |
const maximal = options.acyclic || options.maximal || options.maximalAdjustments > 0; // maximalAdjustments for compat. w/ old code; also, setting acyclic to true sets maximal to true | ||
|
||
const hasBoundingBox = !!options.boundingBox; | ||
const cyExtent = cy.extent(); | ||
const bb = math.makeBoundingBox( hasBoundingBox ? options.boundingBox : { | ||
x1: cyExtent.x1, y1: cyExtent.y1, w: cyExtent.w, h: cyExtent.h | ||
} ); | ||
const bb = math.makeBoundingBox( hasBoundingBox ? options.boundingBox : | ||
structuredClone(cy.extent())); | ||
|
||
let roots; | ||
if( is.elementOrCollection( options.roots ) ){ | ||
|
@@ -350,7 +349,7 @@ BreadthFirstLayout.prototype.run = function(){ | |
|
||
const maxDepthSize = depths.reduce( (max, eles) => Math.max(max, eles.length), 0 ); | ||
|
||
const getPosition = function( ele ){ | ||
const getPositionTopBottom = function( ele ){ | ||
const { depth, index } = getInfo( ele ); | ||
|
||
if ( options.circle ){ | ||
|
@@ -386,10 +385,18 @@ BreadthFirstLayout.prototype.run = function(){ | |
|
||
return epos; | ||
} | ||
|
||
}; | ||
|
||
eles.nodes().layoutPositions( this, options, getPosition ); | ||
const rotateDegrees = { | ||
'bottom-top': 180, | ||
'left-right': -90, | ||
'right-left': 90, | ||
'top-bottom': 0, | ||
} | ||
|
||
const getPosition = (ele) => util.rotatePosAndSkewByBox(getPositionTopBottom(ele), bb, rotateDegrees[options.direction] ?? 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code silently defaults to 0° if Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
eles.nodes().layoutPositions( this, options, getPosition); | ||
|
||
return this; // chaining | ||
}; | ||
|
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export function rotatePoint(x, y, centerX, centerY, angleDegrees) { | ||
const angleRadians = (angleDegrees * Math.PI) / 180; | ||
const rotatedX = | ||
Math.cos(angleRadians) * (x - centerX) - | ||
Math.sin(angleRadians) * (y - centerY) + | ||
centerX; | ||
const rotatedY = | ||
Math.sin(angleRadians) * (x - centerX) + | ||
Math.cos(angleRadians) * (y - centerY) + | ||
centerY; | ||
return { x: rotatedX, y: rotatedY }; | ||
} | ||
|
||
export const skewPointInBox = (x, y, boxX, boxY, skewX, skewY) => ({ | ||
x: (x - boxX) * skewX + boxX, | ||
y: (y - boxY) * skewY + boxY | ||
}); | ||
|
||
export function rotatePosAndSkewByBox(pos, box, angleDegrees) { | ||
if (angleDegrees === 0) return pos; | ||
const centerX = (box.x1 + box.x2) / 2; | ||
const centerY = (box.y1 + box.y2) / 2; | ||
const skewX = box.w / box.h; | ||
const skewY = 1 / skewX; | ||
|
||
const rotated = rotatePoint(pos.x, pos.y, centerX, centerY, angleDegrees); | ||
const skewed = skewPointInBox(rotated.x, rotated.y, centerX, centerY, skewX, skewY); | ||
|
||
return { | ||
x: skewed.x, | ||
y: skewed.y, | ||
}; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new
direction
option should be documented in the public API docs or JSDoc so users know the full set of valid values and their effects.Copilot uses AI. Check for mistakes.