Skip to content

Commit c0ee551

Browse files
Custom annotations (adrai#209)
* Custom annotations * Update README.md
1 parent cbd5996 commit c0ee551

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,29 @@ If you want to emphasize a specific path in your flowchart, you can additionally
218218
st@>op1({"stroke":"Red"})@>cond({"stroke":"Red","stroke-width":6,"arrow-end":"classic-wide-long"})@>c2({"stroke":"Red"})@>op2({"stroke":"Red"})@>e({"stroke":"Red"})
219219
```
220220

221+
## Custom names for branches
222+
223+
```
224+
st=>start: Start:>http://www.google.com[blank]
225+
e=>end:>http://www.google.com
226+
op1=>operation: My Operation
227+
sub1=>subroutine: My Subroutine
228+
cond=>condition: linear or polynomial :>http://www.google.com
229+
io=>inputoutput: catch something...
230+
231+
st->op1->cond
232+
cond(true@linear)->io->e
233+
cond(false@polynomial)->sub1(right)
234+
sub1(right)->op1
235+
```
236+
<details>
237+
238+
<summary>Demonstration</summary>
239+
240+
![img](https://user-images.githubusercontent.com/37659961/90231386-85a3ed80-de34-11ea-8265-976c36b2f0e2.png)
241+
242+
</details>
243+
221244
## Contributors
222245

223246
via [GitHub](https://github.com/adrai/flowchart.js/graphs/contributors)

src/flowchart.parse.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ function parse(input) {
192192
}
193193
return next;
194194
}
195+
196+
function getAnnotation(s) {
197+
var startIndex = s.indexOf("(") + 1, endIndex = s.indexOf(")");
198+
var tmp = s.substring(startIndex, endIndex);
199+
if(tmp.indexOf(",") > 0) { tmp = tmp.substring(0, tmp.indexOf(",")); }
200+
var tmp_split = tmp.split("@");
201+
if(tmp_split.length > 1)
202+
return startIndex >= 0 && endIndex >= 0 ? tmp_split[1] : "";
203+
}
195204

196205
while (lines.length > 0) {
197206
var line = lines.splice(0, 1)[0].trim();
@@ -277,6 +286,11 @@ function parse(input) {
277286
chart.symbols[symbol.key] = symbol;
278287

279288
} else if (line.indexOf('->') >= 0) {
289+
290+
var ann = getAnnotation(line);
291+
if(ann) {
292+
line = line.replace("@" + ann, "");
293+
}
280294
// flow
281295
var flowSymbols = line.split('->');
282296
for (var iS = 0, lenS = flowSymbols.length; iS < lenS; iS++) {
@@ -290,6 +304,13 @@ function parse(input) {
290304
}
291305

292306
var realSymb = getSymbol(flowSymb);
307+
if(ann) {
308+
if(symbVal == "yes" || symbVal == "true")
309+
realSymb.yes_annotation = ann;
310+
else
311+
realSymb.no_annotation = ann;
312+
ann = null;
313+
}
293314
var next = getNextPath(flowSymb);
294315

295316
var direction = null;

src/flowchart.symbol.condition.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var drawPath = drawAPI.drawPath;
66
function Condition(chart, options) {
77
options = options || {};
88
Symbol.call(this, chart, options);
9+
this.yes_annotation = options.yes_annotation;
10+
this.no_annotation = options.no_annotation;
911
this.textMargin = this.getAttr('text-margin');
1012
this.yes_direction = 'bottom';
1113
this.no_direction = 'right';
@@ -160,11 +162,11 @@ Condition.prototype.render = function() {
160162

161163
Condition.prototype.renderLines = function() {
162164
if (this.yes_symbol) {
163-
this.drawLineTo(this.yes_symbol, this.getAttr('yes-text'), this.yes_direction);
165+
this.drawLineTo(this.yes_symbol, this.yes_annotation ? this.yes_annotation : this.getAttr('yes-text'), this.yes_direction);
164166
}
165167

166168
if (this.no_symbol) {
167-
this.drawLineTo(this.no_symbol, this.getAttr('no-text'), this.no_direction);
169+
this.drawLineTo(this.no_symbol, this.no_annotation ? this.no_annotation : this.getAttr('no-text'), this.no_direction);
168170
}
169171
};
170172

0 commit comments

Comments
 (0)