Skip to content
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

support shortest path layout between anchors #120

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default App;
| `strokeWidth` | `number` | A size in `px`
| `strokeDasharray` | `string` | Adds dashes to the stroke. It has to be a string representing an array of sizes. See some [SVG strokes documentation](https://www.w3schools.com/graphics/svg_stroking.asp).
| `noCurves` | `boolean` | Set this to true if you want angles instead of curves
| `shortestPath` | `boolean` | Set this to true if you want the shortest straight line between anchors
| `offset` | `number` | Optional number for space between element and start/end of stroke
| `svgContainerStyle` | `Style` | Style of the SVG container element. Useful if you want to add a z-index to your SVG container to draw the arrows under your elements, for example.
| `children` | `React.Node` |
Expand Down Expand Up @@ -131,6 +132,7 @@ The `ArcherStyle` type has the following shape:
strokeWidth: number,
strokeDasharray: number,
noCurves: boolean,
shortestPath: boolean,
endShape: Object
}
```
Expand Down
4 changes: 4 additions & 0 deletions src/ArcherContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Props = {
strokeWidth: number,
strokeDasharray?: string,
noCurves?: boolean,
shortestPath?: boolean,
children: React$Node | FunctionChild,
style?: Object,
svgContainerStyle?: Object,
Expand Down Expand Up @@ -284,6 +285,8 @@ export class ArcherContainer extends React.Component<Props, State> {

const noCurves = style.noCurves || this.props.noCurves;

const shortestPath = style.shortestPath || this.props.shortestPath;

const offset = this.props.offset || 0;

const startingAnchorOrientation = source.anchor;
Expand Down Expand Up @@ -313,6 +316,7 @@ export class ArcherContainer extends React.Component<Props, State> {
arrowLabel={label}
arrowMarkerId={this._getMarkerId(source, target)}
noCurves={!!noCurves}
shortestPath={!!shortestPath}
offset={offset}
endShape={endShape}
/>
Expand Down
26 changes: 25 additions & 1 deletion src/SvgArrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Props = {
arrowLabel?: ?React$Node,
arrowMarkerId: string,
noCurves: boolean,
shortestPath: boolean,
offset?: number,
endShape: Object,
};
Expand Down Expand Up @@ -126,7 +127,9 @@ function computePathString({
xEnd,
yEnd,
noCurves,
shortestPath,
offset,
endingAnchorOrientation,
}: {|
xStart: number,
yStart: number,
Expand All @@ -137,7 +140,9 @@ function computePathString({
xEnd: number,
yEnd: number,
noCurves: boolean,
shortestPath: boolean,
offset?: number,
endingAnchorOrientation?: AnchorPositionType,
|}): string {
const curveMarker = noCurves ? '' : 'C';

Expand All @@ -154,9 +159,25 @@ function computePathString({
yEnd = yEnd - yOffset;
}

function computeArrowDirection(endingAnchorOrientation) {
switch (endingAnchorOrientation) {
case 'left':
return `${xEnd - 1},${yEnd} `;
case 'right':
return `${xEnd + 1},${yEnd} `;
case 'top':
return `${xEnd},${yEnd - 1} `;
case 'bottom':
return `${xEnd},${yEnd + 1} `;
default:
return '';
}
}

const convertArrowDirectionParams = computeArrowDirection(endingAnchorOrientation)
return (
`M${xStart},${yStart} ` +
`${curveMarker}${xAnchor1},${yAnchor1} ${xAnchor2},${yAnchor2} ` +
(shortestPath ? convertArrowDirectionParams : `${curveMarker}${xAnchor1},${yAnchor1} ${xAnchor2},${yAnchor2} `) +
`${xEnd},${yEnd}`
);
}
Expand All @@ -171,6 +192,7 @@ const SvgArrow = ({
arrowLabel,
arrowMarkerId,
noCurves,
shortestPath,
offset,
endShape,
}: Props) => {
Expand Down Expand Up @@ -218,7 +240,9 @@ const SvgArrow = ({
xEnd,
yEnd,
noCurves,
shortestPath,
offset,
endingAnchorOrientation,
});

const { xLabel, yLabel, labelWidth, labelHeight } = computeLabelDimensions(
Expand Down