From dc0dc02a134f7ec8b1d30226fa1d7d7c630d33fa Mon Sep 17 00:00:00 2001 From: CorvusYe Date: Wed, 1 Feb 2023 08:23:05 +0800 Subject: [PATCH] feat: add legend in graph. --- CHANGELOG.md | 1 + lib/widgets/graph_component.dart | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d280d3b..57362a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.0.1+9 - feat: create random color for tag. +- feat: add legend in graph. ## 0.0.1+8 - feat(convert): cache the edge names and vertex tags in graph. diff --git a/lib/widgets/graph_component.dart b/lib/widgets/graph_component.dart index 94e7173..3cb1ba4 100644 --- a/lib/widgets/graph_component.dart +++ b/lib/widgets/graph_component.dart @@ -47,7 +47,7 @@ class GraphComponent extends FlameGame graph = convertor.convertGraph(data); graph.vertexes = graph.vertexes.toSet().toList() ..sort((key1, key2) => key1.degree - key2.degree > 0 ? -1 : 1); - + setDefaultVertexColor(); for (var edge in graph.edges) { var ec = EdgeComponent(edge, graph, context)..scaleNotifier = scale; edge.cpn = ec; @@ -59,9 +59,19 @@ class GraphComponent extends FlameGame vertex.cpn = vc; add(vc); } + + createLegend(); options.graphStyle.graphColor(graph); } + setDefaultVertexColor() { + var tagColorByIndex = options.graphStyle.tagColorByIndex; + var needCount = graph.allTags.length - tagColorByIndex.length; + for (var i = 0; i < needCount; i++) { + tagColorByIndex.add(options.graphStyle.defaultColor()[0]); + } + } + updateViewport(x, y) { camera.viewport = FixedResolutionViewport(Vector2(x, y)); } @@ -102,4 +112,24 @@ class GraphComponent extends FlameGame algorithm.onZoomEdge(edge, pointLocation!, delta); } } + + void createLegend() { + for (var i = 0; i < graph.allTags.length; i++) { + var tag = graph.allTags[i]; + + add( + RectangleComponent.fromRect( + Rect.fromLTWH( + 40, + 50.0 + 30 * i, + 30, + 18, + ), + paint: Paint() + ..color = options.graphStyle.colorByTag(tag, graph.allTags)!), + ); + + add(TextComponent(text: tag, position: Vector2(40 + 40, 44.0 + 30 * i))); + } + } }