Skip to content

Commit

Permalink
Merge pull request #845 from Qix-/style-subgraph
Browse files Browse the repository at this point in the history
Support styling of subgraphs
  • Loading branch information
knsv authored May 28, 2019
2 parents d742211 + f51596e commit 23693bd
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 110 deletions.
29 changes: 29 additions & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,35 @@
a1-->a2
end
</div>
<div class="mermaid">
graph TB
A
B
subgraph foo[Foo SubGraph]
C
D
end
subgraph bar[Bar SubGraph]
E
F
end
G

A-->B
B-->C
C-->D
B-->D
D-->E
E-->A
E-->F
F-->D
F-->G
B-->G
G-->D

style foo fill:#F99,stroke-width:2px,stroke:#F0F
style bar fill:#999,stroke-width:10px,stroke:#0F0
</div>
<div class="mermaid">
graph LR
456ac9b0d15a8b7f1e71073221059886[1051 AAA fa:fa-check]
Expand Down
27 changes: 22 additions & 5 deletions src/diagrams/flowchart/flowDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let vertices = {}
let edges = []
let classes = []
let subGraphs = []
let subGraphLookup = {}
let tooltips = {}
let subCount = 0
let direction
Expand All @@ -18,8 +19,9 @@ let funs = []
* @param text
* @param type
* @param style
* @param classes
*/
export const addVertex = function (id, text, type, style) {
export const addVertex = function (id, text, type, style, classes) {
let txt

if (typeof id === 'undefined') {
Expand Down Expand Up @@ -52,6 +54,13 @@ export const addVertex = function (id, text, type, style) {
})
}
}
if (typeof classes !== 'undefined') {
if (classes !== null) {
classes.forEach(function (s) {
vertices[id].classes.push(s)
})
}
}
}

/**
Expand Down Expand Up @@ -143,6 +152,10 @@ export const setClass = function (ids, className) {
if (typeof vertices[id] !== 'undefined') {
vertices[id].classes.push(className)
}

if (typeof subGraphLookup[id] !== 'undefined') {
subGraphLookup[id].classes.push(className)
}
})
}

Expand Down Expand Up @@ -283,6 +296,7 @@ export const clear = function () {
funs = []
funs.push(setupToolTips)
subGraphs = []
subGraphLookup = {}
subCount = 0
tooltips = []
}
Expand All @@ -297,7 +311,7 @@ export const defaultStyle = function () {
/**
* Clears the internal graph db so that a new graph can be parsed.
*/
export const addSubGraph = function (list, title) {
export const addSubGraph = function (id, list, title) {
function uniq (a) {
const prims = { 'boolean': {}, 'number': {}, 'string': {} }
const objs = []
Expand All @@ -315,10 +329,13 @@ export const addSubGraph = function (list, title) {

nodeList = uniq(nodeList.concat.apply(nodeList, list))

const subGraph = { id: 'subGraph' + subCount, nodes: nodeList, title: title.trim() }
subGraphs.push(subGraph)
id = id || ('subGraph' + subCount)
title = title || ''
subCount = subCount + 1
return subGraph.id
const subGraph = { id: id, nodes: nodeList, title: title.trim(), classes: [] }
subGraphs.push(subGraph)
subGraphLookup[id] = subGraph
return id
}

const getPosForId = function (id) {
Expand Down
2 changes: 1 addition & 1 deletion src/diagrams/flowchart/flowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export const draw = function (text, id) {
const subGraphs = flowDb.getSubGraphs()
for (let i = subGraphs.length - 1; i >= 0; i--) {
subG = subGraphs[i]
flowDb.addVertex(subG.id, subG.title, 'group', undefined)
flowDb.addVertex(subG.id, subG.title, 'group', undefined, subG.classes)
}

// Fetch the verices/nodes and edges/links from the parsed graph definition
Expand Down
10 changes: 7 additions & 3 deletions src/diagrams/flowchart/parser/flow.jison
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,14 @@ statement
{$$=[];}
| clickStatement separator
{$$=[];}
| subgraph text separator document end
{$$=yy.addSubGraph($4,$2);}
| subgraph SPACE alphaNum SQS text SQE separator document end
{$$=yy.addSubGraph($3,$8,$5);}
| subgraph SPACE STR separator document end
{$$=yy.addSubGraph(undefined,$5,$3);}
| subgraph SPACE alphaNum separator document end
{$$=yy.addSubGraph($3,$5,$3);}
| subgraph separator document end
{$$=yy.addSubGraph($3,undefined);}
{$$=yy.addSubGraph(undefined,$3,undefined);}
;

separator: NEWLINE | SEMI | EOF ;
Expand Down
Loading

0 comments on commit 23693bd

Please sign in to comment.