Skip to content

Commit dc0dc02

Browse files
committed
feat: add legend in graph.
1 parent a4740b5 commit dc0dc02

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 0.0.1+9
22
- feat: create random color for tag.
3+
- feat: add legend in graph.
34

45
## 0.0.1+8
56
- feat(convert): cache the edge names and vertex tags in graph.

lib/widgets/graph_component.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class GraphComponent extends FlameGame
4747
graph = convertor.convertGraph(data);
4848
graph.vertexes = graph.vertexes.toSet().toList()
4949
..sort((key1, key2) => key1.degree - key2.degree > 0 ? -1 : 1);
50-
50+
setDefaultVertexColor();
5151
for (var edge in graph.edges) {
5252
var ec = EdgeComponent(edge, graph, context)..scaleNotifier = scale;
5353
edge.cpn = ec;
@@ -59,9 +59,19 @@ class GraphComponent extends FlameGame
5959
vertex.cpn = vc;
6060
add(vc);
6161
}
62+
63+
createLegend();
6264
options.graphStyle.graphColor(graph);
6365
}
6466

67+
setDefaultVertexColor() {
68+
var tagColorByIndex = options.graphStyle.tagColorByIndex;
69+
var needCount = graph.allTags.length - tagColorByIndex.length;
70+
for (var i = 0; i < needCount; i++) {
71+
tagColorByIndex.add(options.graphStyle.defaultColor()[0]);
72+
}
73+
}
74+
6575
updateViewport(x, y) {
6676
camera.viewport = FixedResolutionViewport(Vector2(x, y));
6777
}
@@ -102,4 +112,24 @@ class GraphComponent extends FlameGame
102112
algorithm.onZoomEdge(edge, pointLocation!, delta);
103113
}
104114
}
115+
116+
void createLegend() {
117+
for (var i = 0; i < graph.allTags.length; i++) {
118+
var tag = graph.allTags[i];
119+
120+
add(
121+
RectangleComponent.fromRect(
122+
Rect.fromLTWH(
123+
40,
124+
50.0 + 30 * i,
125+
30,
126+
18,
127+
),
128+
paint: Paint()
129+
..color = options.graphStyle.colorByTag(tag, graph.allTags)!),
130+
);
131+
132+
add(TextComponent(text: tag, position: Vector2(40 + 40, 44.0 + 30 * i)));
133+
}
134+
}
105135
}

0 commit comments

Comments
 (0)