Skip to content

Commit

Permalink
feat: change direction of erDiagram
Browse files Browse the repository at this point in the history
  • Loading branch information
demonsgalore committed Jun 2, 2024
1 parent d6ccd93 commit c6191b1
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 7 deletions.
48 changes: 48 additions & 0 deletions cypress/integration/rendering/erDiagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,52 @@ ORDER ||--|{ LINE-ITEM : contains
{ logLevel: 1 }
);
});

it('should handle the direction statement with TB', () => {
imgSnapshotTest(
`
erDiagram
direction TB
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
`,
{ logLevel: 1 }
);
});

it('should handle the direction statement with BT', () => {
imgSnapshotTest(
`
erDiagram
direction BT
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
`,
{ logLevel: 1 }
);
});

it('should handle the direction statement with LR', () => {
imgSnapshotTest(
`
erDiagram
direction LR
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
`,
{ logLevel: 1 }
);
});

it('should handle the direction statement with RL', () => {
imgSnapshotTest(
`
erDiagram
direction RL
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
`,
{ logLevel: 1 }
);
});
});
13 changes: 7 additions & 6 deletions demos/er.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@

<pre class="mermaid">
erDiagram
direction LR
p[Person] {
string firstName
string lastName
Expand All @@ -130,12 +131,12 @@
_customer_order {
bigint id PK
bigint customer_id FK
text shipping_address
text delivery_method
timestamp_with_time_zone ordered_at
numeric total_tax_amount
numeric total_price
text payment_method
text shipping_address
text delivery_method
timestamp_with_time_zone ordered_at
numeric total_tax_amount
numeric total_price
text payment_method
}
</pre>
<hr />
Expand Down
8 changes: 8 additions & 0 deletions packages/mermaid/src/diagrams/er/erDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ const clear = function () {
commonClear();
};

let direction = 'TB';
const getDirection = () => direction;
const setDirection = (dir) => {
direction = dir;
};

export default {
Cardinality,
Identification,
Expand All @@ -96,4 +102,6 @@ export default {
getAccDescription,
setDiagramTitle,
getDiagramTitle,
setDirection,
getDirection
};
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagrams/er/erRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ export const draw = function (text, id, _version, diagObj) {
compound: false,
})
.setGraph({
rankdir: conf.layoutDirection,
rankdir: diagObj.db.getDirection(),
marginx: 20,
marginy: 20,
nodesep: 100,
Expand Down
18 changes: 18 additions & 0 deletions packages/mermaid/src/diagrams/er/parser/erDiagram.jison
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
%x acc_descr_multiline

%%
.*direction\s+TB[^\n]* return 'direction_tb';
.*direction\s+BT[^\n]* return 'direction_bt';
.*direction\s+RL[^\n]* return 'direction_rl';
.*direction\s+LR[^\n]* return 'direction_lr';
\%\%(?!\{)*[^\n]*(\r?\n?)+ /* skip comments */
\%\%[^\n]*(\r?\n)* /* skip comments */
accTitle\s*":"\s* { this.begin("acc_title");return 'acc_title'; }
<acc_title>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_title_value"; }
accDescr\s*":"\s* { this.begin("acc_descr");return 'acc_descr'; }
Expand Down Expand Up @@ -109,6 +115,7 @@ statement
| entityName SQS entityName SQE BLOCK_START BLOCK_STOP { yy.addEntity($1, $3); }
| entityName SQS entityName SQE { yy.addEntity($1, $3); }
| title title_value { $$=$2.trim();yy.setAccTitle($$); }
| direction
| acc_title acc_title_value { $$=$2.trim();yy.setAccTitle($$); }
| acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); }
| acc_descr_multiline_value { $$=$1.trim();yy.setAccDescription($$); }
Expand Down Expand Up @@ -180,4 +187,15 @@ role
| 'ALPHANUM' { $$ = $1; }
;

direction
: direction_tb
{ yy.setDirection('TB');}
| direction_bt
{ yy.setDirection('BT');}
| direction_rl
{ yy.setDirection('RL');}
| direction_lr
{ yy.setDirection('LR');}
;

%%
9 changes: 9 additions & 0 deletions packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,4 +803,13 @@ describe('when parsing ER diagram it...', function () {
}
);
});

it('should have the default direction TB', function () {
erDiagram.parser.parse('erDiagram\nA ||--|{ B : has');
expect(erDb.getDirection()).toBe('TB');
});

it('should handle the direction statement', function () {
erDiagram.parser.parse('erDiagram\ndirection LR\nA ||--|{ B : has');
});
});
25 changes: 25 additions & 0 deletions packages/mermaid/src/docs/syntax/entityRelationshipDiagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,31 @@ erDiagram
MANUFACTURER only one to zero or more CAR : makes
```

## Setting the direction of the diagram

With entity relationship diagrams you can use the direction statement to set the direction in which the diagram will render:

```mermaid-example
erDiagram
direction LR
CUSTOMER ||--o{ ORDER : places
CUSTOMER {
string name
string custNumber
string sector
}
ORDER ||--|{ LINE-ITEM : contains
ORDER {
int orderNumber
string deliveryAddress
}
LINE-ITEM {
string productCode
int quantity
float pricePerUnit
}
```

### Other Things

- If you want the relationship label to be more than one word, you must use double quotes around the phrase
Expand Down

0 comments on commit c6191b1

Please sign in to comment.