graphview
flutter graphview is used to display data in graph structures. it can display tree layout and directed graph.
the library is designed to support different graph layouts and currently works excellent with small graphs.
you can have a look at the flutter web implementation here:
http://graphview.surge.sh/
layouts
tree
uses walker’s algorithm with buchheim’s runtime improvements (buchheimwalkeralgorithm
class). supports different orientations. all you have to do is using the buchheimwalkerconfiguration.orientation
with either orientation_left_right
, orientation_right_left
, orientation_top_bottom
and
orientation_bottom_top
(default). furthermore parameters like sibling-, level-, subtree separation can be set.
useful for: family tree, hierarchy view, flutter widget tree,
directed graph
directed graph drawing by simulating attraction/repulsion forces. for this the algorithm by fruchterman and reingold (fruchtermanreingoldalgorithm
class) was implemented.
useful for: social network, mind map, cluster, graphs, intercity road network,
usage
currently graphview must be used together with a zoom engine like interactiveviewer. to change the zoom values just use the different attributes described in the interactiveviewer class.
to create a graph, we need to instantiate the graph
class. then we need to pass the layout and also optional the edge renderer.
void main() {
runapp(myapp());
}
class myapp extends statelesswidget {
@override
widget build(buildcontext context) => materialapp(
home: graphviewpage(),
);
}
class graphviewpage extends statefulwidget {
@override
_graphviewpagestate createstate() => _graphviewpagestate();
}
class _graphviewpagestate extends state<graphviewpage> {
@override
widget build(buildcontext context) {
return scaffold(
body: column(
mainaxissize: mainaxissize.max,
children: [
wrap(
children: [
container(
width: 100,
child: textformfield(
initialvalue: builder.siblingseparation.tostring(),
decoration: inputdecoration(labeltext: "sibling sepration"),
onchanged: (text) {
builder.siblingseparation = int.tryparse(text) ?? 100;
this.setstate(() {});
},
),
),
container(
width: 100,
child: textformfield(
initialvalue: builder.levelseparation.tostring(),
decoration: inputdecoration(labeltext: "level seperation"),
onchanged: (text) {
builder.levelseparation = int.tryparse(text) ?? 100;
this.setstate(() {});
},
),
),
container(
width: 100,
child: textformfield(
initialvalue: builder.subtreeseparation.tostring(),
decoration: inputdecoration(labeltext: "subtree separation"),
onchanged: (text) {
builder.subtreeseparation = int.tryparse(text) ?? 100;
this.setstate(() {});
},
),
),
container(
width: 100,
child: textformfield(
initialvalue: builder.orientation.tostring(),
decoration: inputdecoration(labeltext: "orientation"),
onchanged: (text) {
builder.orientation = int.tryparse(text) ?? 100;
this.setstate(() {});
},
),
),
raisedbutton(
onpressed: () {
final node node12 = node(getnodetext());
var edge = graph.getnodeatposition(r.nextint(graph.nodecount()));
print(edge);
graph.addedge(edge, node12);
setstate(() {});
},
child: text("add"),
)
],
),
expanded(
child: interactiveviewer(
constrained: false,
scaleenabled: false,
boundarymargin: edgeinsets.all(100),
minscale: 0.01,
maxscale: 5.6,
child: graphview(
graph: graph,
algorithm: buchheimwalkeralgorithm(builder),
renderer: treeedgerenderer(builder),
)),
),
],
)
);
}
random r = random();
int n = 1;
widget getnodetext() {
return container(
padding: edgeinsets.all(16),
decoration: boxdecoration(
borderradius: borderradius.circular(4),
boxshadow: [
boxshadow(color: colors.blue[100], spreadradius: 1),
],
),
child: text("node ${n++}"));
}
final graph graph = graph();
buchheimwalkerconfiguration builder = buchheimwalkerconfiguration();
@override
void initstate() {
final node node1 = node(getnodetext());
final node node2 = node(getnodetext());
final node node3 = node(getnodetext());
final node node4 = node(getnodetext());
final node node5 = node(getnodetext());
final node node6 = node(getnodetext());
final node node8 = node(getnodetext());
final node node7 = node(getnodetext());
final node node9 = node(getnodetext());
final node node10 = node(getnodetext());
final node node11 = node(getnodetext());
final node node12 = node(getnodetext());
graph.addedge(node1, node2);
graph.addedge(node1, node3);
graph.addedge(node1, node4);
graph.addedge(node2, node5);
graph.addedge(node2, node6);
graph.addedge(node6, node7);
graph.addedge(node6, node8);
graph.addedge(node4, node9);
graph.addedge(node4, node10);
graph.addedge(node4, node11);
graph.addedge(node11, node12);
builder
..siblingseparation = (100)
..levelseparation = (150)
..subtreeseparation = (150)
..orientation = (buchheimwalkerconfiguration.orientation_top_bottom);
}
}
using any widget inside the node
you can use any widget inside the node:
node node = node(getnodetext);
getnodetext() {
return container(
padding: edgeinsets.all(16),
decoration: boxdecoration(
borderradius: borderradius.circular(4),
boxshadow: [
boxshadow(color: colors.blue[100], spreadradius: 1),
],
),
child: text("node ${n++}"));
}
examples
rooted tree
rooted tree (bottom to top)
rooted tree (left to right)
rooted tree (right to left)
directed graph
inspirations
this library is basically a dart representation of the excellent android library graphview by team-blox
i would like to thank them for open sourcing their code for which reason i was able to port their code to dart and use for flutter.
future works
[] add nodeontap
[] add layered graph (prs are welcome)
[] use a builder pattern to draw items on demand.
Comments are closed.