CxJS Diagrams

Neural Network Example

This example visualizes a simple neural network with input, hidden, and output layers. It demonstrates:

  • Flow layouts for arranging layers horizontally and nodes vertically
  • Repeater for generating nodes and connections from data
  • Conditional styling using CxJS expressions to color nodes by layer type
  • StraightLine for connecting all nodes between adjacent layers
interface Model {
    network: Network;
    $layer: NetworkLayer;
    $record: { id: string; name: string };
    $conn: { from: string; to: string };
}

const m = createModel<Model>();

<Diagram unitSize={48} showGrid center>
    <Flow gap={2} align="center">
        <Repeater records={m.network.layers} recordAlias={m.$layer}>
            <Flow direction="down" gap={0.5}>
                <Repeater records={m.$layer.nodes}>
                    <Cell>
                        <Shape
                            text={m.$record.name}
                            id={m.$record.id}
                            shape="circle"
                            class={{
                                'fill-blue-200 stroke-blue-600': equal(m.$layer.type, "input"),
                                'fill-orange-200 stroke-orange-600': equal(m.$layer.type, "hidden"),
                                'fill-green-200 stroke-green-600': equal(m.$layer.type, "output"),
                            }}
                        />
                    </Cell>
                </Repeater>
            </Flow>
        </Repeater>
    </Flow>
    <Repeater records={m.network.connections} recordAlias={m.$conn}>
        <StraightLine from={m.$conn.from} to={m.$conn.to} stroke="black" />
    </Repeater>
</Diagram>
 
3
 
2
 
5
 
1