-
Notifications
You must be signed in to change notification settings - Fork 18
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
New Added support for node label alignment #78
base: release/1.0.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,13 @@ export enum NodeShapeType { | |
HEXAGON = 'hexagon', | ||
} | ||
|
||
export enum NodeLabelAligment { | ||
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. When thinking about this, I feel like this should be called
So I would propose renaming this to NodeLabelPosition because it is actually a label position, rather than alignment. |
||
TOP = 'top', | ||
BOTTOM = 'bottom', | ||
LEFT = 'left', | ||
RIGHT = 'right', | ||
} | ||
|
||
/** | ||
* Node style properties used to style the node (color, width, label, etc.). | ||
*/ | ||
|
@@ -59,6 +66,7 @@ export type INodeStyle = Partial<{ | |
size: number; | ||
mass: number; | ||
zIndex: number; | ||
labelAlignment: NodeLabelAligment; | ||
}>; | ||
|
||
export interface INodeData<N extends INodeBase> { | ||
|
@@ -95,6 +103,7 @@ export interface INode<N extends INodeBase, E extends IEdgeBase> { | |
getBorderWidth(): number; | ||
getBorderColor(): Color | string | undefined; | ||
getBackgroundImage(): HTMLImageElement | undefined; | ||
getLabelAlignment(): NodeLabelAligment; | ||
} | ||
|
||
// TODO: Dirty solution: Find another way to listen for global images, maybe through | ||
|
@@ -363,6 +372,10 @@ export class Node<N extends INodeBase, E extends IEdgeBase> implements INode<N, | |
}); | ||
} | ||
|
||
getLabelAlignment(): NodeLabelAligment { | ||
return this.style.labelAlignment ?? NodeLabelAligment.BOTTOM; | ||
} | ||
|
||
protected _isPointInBoundingBox(point: IPosition): boolean { | ||
return isPointInRectangle(this.getBoundingBox(), point); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
import { INodeBase, INode, NodeShapeType } from '../../models/node'; | ||
import { IEdgeBase } from '../../models/edge'; | ||
import { drawDiamond, drawHexagon, drawSquare, drawStar, drawTriangleDown, drawTriangleUp, drawCircle } from './shapes'; | ||
import { drawLabel, Label, LabelTextBaseline } from './label'; | ||
import { | ||
INodeBase, | ||
INode, | ||
NodeShapeType, | ||
NodeLabelAligment, | ||
} from "../../models/node"; | ||
import { IEdgeBase } from "../../models/edge"; | ||
import { | ||
drawDiamond, | ||
drawHexagon, | ||
drawSquare, | ||
drawStar, | ||
drawTriangleDown, | ||
drawTriangleUp, | ||
drawCircle, | ||
} from "./shapes"; | ||
import { drawLabel, Label, LabelTextAlign, LabelTextBaseline } from "./label"; | ||
|
||
// The label will be `X` of the size below the Node | ||
const DEFAULT_LABEL_DISTANCE_SIZE_FROM_NODE = 0.2; | ||
|
@@ -97,9 +110,43 @@ const drawNodeLabel = <N extends INodeBase, E extends IEdgeBase>( | |
const center = node.getCenter(); | ||
const distance = node.getBorderedRadius() * (1 + DEFAULT_LABEL_DISTANCE_SIZE_FROM_NODE); | ||
|
||
let labelX = center.x; | ||
let labelY = center.y; | ||
let labelTextAlign: LabelTextAlign; | ||
let labelTextBaseline: LabelTextBaseline; | ||
|
||
switch (node.getLabelAlignment()) { | ||
case NodeLabelAligment.BOTTOM: | ||
labelY += distance; | ||
labelTextAlign = LabelTextAlign.CENTER; | ||
labelTextBaseline = LabelTextBaseline.TOP; | ||
break; | ||
case NodeLabelAligment.TOP: | ||
labelY -= distance; | ||
labelTextAlign = LabelTextAlign.CENTER; | ||
labelTextBaseline = LabelTextBaseline.BOTTOM; | ||
break; | ||
case NodeLabelAligment.LEFT: | ||
labelX -= distance; | ||
labelTextAlign = LabelTextAlign.RIGHT; | ||
labelTextBaseline = LabelTextBaseline.MIDDLE; | ||
break; | ||
case NodeLabelAligment.RIGHT: | ||
labelX += distance; | ||
labelTextAlign = LabelTextAlign.LEFT; | ||
labelTextBaseline = LabelTextBaseline.MIDDLE; | ||
break; | ||
default: | ||
labelY += distance; | ||
labelTextAlign = LabelTextAlign.CENTER; | ||
labelTextBaseline = LabelTextBaseline.TOP; | ||
break; | ||
} | ||
Comment on lines
+118
to
+144
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. Actually, this should be part of Label logic. I would propose the following:
And then Label logic receives position + alignment and handles the rest. Please take care of multiple lines because it is not working well. Check the image below for the "top". 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. Node logic should just handle the start position: |
||
|
||
const label = new Label(nodeLabel, { | ||
position: { x: center.x, y: center.y + distance }, | ||
textBaseline: LabelTextBaseline.TOP, | ||
position: { x: labelX, y: labelY }, | ||
textBaseline: labelTextBaseline, | ||
textAlign: labelTextAlign, | ||
properties: { | ||
fontBackgroundColor: node.style.fontBackgroundColor, | ||
fontColor: node.style.fontColor, | ||
|
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.
A typo, missing
n
->Alig
+n
+ment
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.
Please check other usage of
NodeLabelAlignment
because the typo is there too.