CxJS Diagrams

Network Diagram Example

This example demonstrates a network topology diagram with four network segments arranged around a central “Internet” node. It showcases several key features:

  • FourSides and Rotate for positioning network segments in four directions
  • Repeater for dynamically generating shapes from data
  • ThreeSegmentLine for connecting devices with Z-shaped paths
  • Grouping related devices with background rectangles
interface Model {
    networks: Network[];
    $network: Network;
    $index: number;
    $record: { id: string; name: string };
    $conn: { from: string; to: string };
}

const m = createModel<Model>();

<Diagram unitSize={32} showGrid center>
    <FourSides gap={-3}>
        <Cell width={7} height={6}>
            <Shape id="root" margin={50} />
            <PureContainer putInto="cloud">
                <Image href="/cloud.png" />
                <Text value="Internet" class="fill-black" textAnchor="middle" dy="0.5em" />
            </PureContainer>
        </Cell>
        <Repeater records={m.networks} recordAlias={m.$network}>
            <Rotate turns={m.$index}>
                <Flow gap={1} align="center">
                    <Flow direction="down" gap={1} p={0.5}>
                        <Rectangle class="fill-gray-100" />
                        <Repeater records={m.$network.firewalls}>
                            <Cell width={2}>
                                <Shape text={m.$record.name} class="fill-blue-400" id={m.$record.id} />
                            </Cell>
                            <StraightLine from={m.$record.id} to="root" stroke="black" />
                        </Repeater>
                    </Flow>
                    <Flow direction="down" gap={1} p={0.5}>
                        <Rectangle class="fill-gray-100" />
                        <Repeater records={m.$network.switches}>
                            <Cell width={2}>
                                <Shape text={m.$record.name} class="fill-green-400" id={m.$record.id} />
                            </Cell>
                        </Repeater>
                    </Flow>
                    <Flow direction="down" gap={1} p={0.5}>
                        <Rectangle class="fill-gray-100" />
                        <Repeater records={m.$network.pcs}>
                            <Cell width={2}>
                                <Shape text={m.$record.name} class="fill-orange-400" id={m.$record.id} />
                            </Cell>
                        </Repeater>
                    </Flow>
                </Flow>
                <Repeater records={m.$network.connections} recordAlias={m.$conn}>
                    <ThreeSegmentLine
                        from={m.$conn.from}
                        to={m.$conn.to}
                        direction="right"
                        class="stroke-black"
                        stroke="black"
                    />
                </Repeater>
            </Rotate>
        </Repeater>
    </FourSides>
    <ContentPlaceholder name="cloud" />
</Diagram>