CxJS Diagrams

ElbowLine

import { ElbowLine } from "cx-diagrams"

The ElbowLine component connects two shapes with horizontal and vertical segments and figures out the number of segments on its own. You only tell it where the line should leave the first shape (startDirection) and in which direction it should be traveling when it enters the second shape (endDirection).

The line starts in the middle of the side that startDirection points to and ends in the middle of the side that endDirection points into. For example, endDirection="right" enters the shape on its left side and endDirection="down" enters it at the top.

Depending on the directions and the position of the shapes, the result is a straight line, an L-shaped, Z-shaped or U-shaped line, or a line with four or five segments which goes around the shapes. The route with the fewest corners is always used and it never crosses either of the two shapes. Other shapes in the diagram are not taken into account.

The shapes in the example below are wrapped in Draggable. Drag them around to see how the lines change.

Lines start and end in the middle of the side. Use startOffset and endOffset to move the endpoints along the side, e.g. to separate lines which share the same side of a shape, like the two lines leaving the bottom of shape 2 in the example.

When a shape is only a few pixels off, the first or the last segment can get too short to fit an arrow head. To prevent this, both segments are kept at least 8 pixels long and the line takes a small detour instead. Use minStartLength and minEndLength to change that, e.g. for larger arrow heads.

Use cornerRadius to round the corners, or set smooth to draw a curve which leaves and enters the shapes in the given directions.

<Diagram unitSize={48} center showGrid>
    <Flow direction="down" gap={2} align="center">
        <Flow gap={2}>
            <Draggable offsetX={bind(m.s1.offsetX, 0)} offsetY={bind(m.s1.offsetY, 0)}>
                <Cell>
                    <Shape fill="lightgray" text="1" id="1" />
                </Cell>
            </Draggable>
            <Draggable offsetX={bind(m.s2.offsetX, 0)} offsetY={bind(m.s2.offsetY, 0)}>
                <Cell>
                    <Shape fill="lightgray" text="2" id="2" />
                </Cell>
            </Draggable>
            <Draggable offsetX={bind(m.s3.offsetX, 0)} offsetY={bind(m.s3.offsetY, 0)}>
                <Cell>
                    <Shape fill="lightgray" text="3" id="3" />
                </Cell>
            </Draggable>
        </Flow>
        <Flow gap={2}>
            <Draggable offsetX={bind(m.s4.offsetX, 0)} offsetY={bind(m.s4.offsetY, 0)}>
                <Cell>
                    <Shape fill="lightgray" text="4" id="4" />
                </Cell>
            </Draggable>
            <Draggable offsetX={bind(m.s5.offsetX, 0)} offsetY={bind(m.s5.offsetY, 0)}>
                <Cell>
                    <Shape fill="lightgray" text="5" id="5" />
                </Cell>
            </Draggable>
        </Flow>
    </Flow>

    {/* one segment */}
    <ElbowLine from="4" to="5" stroke="black">
        <ArrowHead size={7} fill="black" />
    </ElbowLine>

    {/* two segments */}
    <ElbowLine from="1" to="4" startDirection="down" endDirection="right" stroke="green">
        <ArrowHead size={7} fill="green" />
    </ElbowLine>

    {/* three segments */}
    <ElbowLine from="2" to="5" startDirection="down" startOffset={8} stroke="blue">
        <ArrowHead size={7} fill="blue" />
    </ElbowLine>
    <ElbowLine from="2" to="3" startDirection="up" endDirection="down" stroke="purple" cornerRadius={8}>
        <ArrowHead size={7} fill="purple" />
    </ElbowLine>

    {/* four segments */}
    <ElbowLine from="5" to="3" startDirection="down" endDirection="left" stroke="red" cornerRadius={8}>
        <ArrowHead size={7} fill="red" />
    </ElbowLine>

    {/* five segments */}
    <ElbowLine from="4" to="1" startDirection="down" endDirection="down" stroke="orange" cornerRadius={8}>
        <ArrowHead size={7} fill="orange" />
    </ElbowLine>

    {/* smooth */}
    <ElbowLine from="2" to="4" startDirection="down" endDirection="left" startOffset={-8} stroke="teal" smooth>
        <ArrowHead size={7} fill="teal" />
    </ElbowLine>
</Diagram>

Configuration

Property Description Type
cornerRadius

Radius used to round the corners of the line. Default value is 8.

number
endDirection

Direction in which the line travels when it enters the end shape, e.g. right means that the line enters the shape on its left side. Defaults to startDirection.

string
endOffset

Moves the end point away from the middle of the side, in pixels. Positive values move it down on the left and right side, and to the right on the top and bottom side. The point always stays on the side. Default value is 0.

number
from

Id of the first (start) shape.

string
smooth

Set to true to draw a smooth curve instead of straight segments.

boolean
startDirection

Direction in which the line leaves the start shape. Available values are up, down, left, right. Default value is right.

string
startOffset

Moves the start point away from the middle of the side, in pixels. Positive values move it down on the left and right side, and to the right on the top and bottom side. The point always stays on the side. Default value is 0.

number
stroke

Color of the line. Set the thickness with style (e.g. stroke-width: 2px) or a CSS class.

string
to

Id of the second (end) shape.

string
anchors

Anchor defines how child bounds are tied to the parent. Zero aligns with the top/left edge. One aligns with the bottom/right edge.

string/number/rect
class

Additional CSS classes to be applied to the element. If an object is provided, all keys with a "truthy" value will be added to the CSS class list.

string/object
clearance

Minimum distance in pixels kept between the line and the shapes when the line has to go around them. Default value is 16.

number
items

List of child elements.

array
layout

Define an inner layout.

string/object
margin

Apply margin to the given boundaries.

string/number/rect
minEndLength

Minimum length of the last segment in pixels. Use it to leave enough room for an arrow head at the end of the line. Default value is 8.

number
minStartLength

Minimum length of the first segment in pixels. Use it to leave enough room for an arrow head at the start of the line. Default value is 8.

number
mod

Appearance modifier. For example, mod="big" will add the CSS class .cxm-big to the block element.

string/array
offset

Move boundaries specified by the offset.

string/number/rect
outerLayout

Defines the outer layout which wraps the widget.

widget
padding

Padding to be applied to the boundaries rectangle before passing to the children.

string/number/rect
plainText

Set to true to avoid converting inner strings to templates. Default false.

boolean
preserveWhitespace

Keep whitespace in text based children. Default is false. See also trimWhitespace.

boolean
putInto

Used with outer layouts. Specifies the name of the content placeholder which should render the widget.

string
style

Style object applied to the wrapper div. Used for setting the dimensions of the element.

string/object
trimWhitespace

Remove all whitespace in text based children. Default is true. See also preserveWhitespace.

boolean
vdomKey

Key that will be used as the key when rendering the React component.

string
visible

Visibility of the widget. Defaults to true.

boolean