Skip to content

Commit 0789cf5

Browse files
committed
feat: support specifying legends and legend text build.
1 parent 8914e38 commit 0789cf5

File tree

5 files changed

+107
-26
lines changed

5 files changed

+107
-26
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
## 1.1.2
2+
- feat: support specifying legends and legend text build.
3+
4+
example:
5+
```dart
6+
Options()
7+
..legendBuilder = (color, i) { // default
8+
return RectangleComponent.fromRect(
9+
Rect.fromLTWH(40, 50.0 + 30 * i, 30, 18),
10+
paint: Paint()..color = color,
11+
);
12+
}
13+
..legendTextBuilder = (tag, i, color, position) { // default
14+
return TextComponent(
15+
text: tag,
16+
position: Vector2(position.x + 40, position.y - 6),
17+
// position: Vector2(position.x + 40, position.y - 2),
18+
// textRenderer: TextPaint(
19+
// style: TextStyle(
20+
// fontSize: 17.0,
21+
// color: Colors.white,
22+
// ),
23+
// ),
24+
);
25+
}
26+
```
27+
128
## 1.1.1
229
- fix usage of both Scale and Pan on mobile platforms. ( [#12](https://github.com/graph-cn/flutter_graph_view/pull/12), via: [Mykyta Sadchenko](https://github.com/muknta))
330
- feat: support zooming through gestures.

example/pubspec.lock

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ packages:
7676
path: ".."
7777
relative: true
7878
source: path
79-
version: "1.0.3"
79+
version: "1.1.1"
8080
flutter_lints:
8181
dependency: "direct dev"
8282
description:
@@ -95,6 +95,30 @@ packages:
9595
description: flutter
9696
source: sdk
9797
version: "0.0.0"
98+
leak_tracker:
99+
dependency: transitive
100+
description:
101+
name: leak_tracker
102+
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
103+
url: "https://pub.dev"
104+
source: hosted
105+
version: "10.0.4"
106+
leak_tracker_flutter_testing:
107+
dependency: transitive
108+
description:
109+
name: leak_tracker_flutter_testing
110+
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
111+
url: "https://pub.dev"
112+
source: hosted
113+
version: "3.0.3"
114+
leak_tracker_testing:
115+
dependency: transitive
116+
description:
117+
name: leak_tracker_testing
118+
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
119+
url: "https://pub.dev"
120+
source: hosted
121+
version: "3.0.1"
98122
lints:
99123
dependency: transitive
100124
description:
@@ -107,26 +131,26 @@ packages:
107131
dependency: transitive
108132
description:
109133
name: matcher
110-
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
134+
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
111135
url: "https://pub.dev"
112136
source: hosted
113-
version: "0.12.16"
137+
version: "0.12.16+1"
114138
material_color_utilities:
115139
dependency: transitive
116140
description:
117141
name: material_color_utilities
118-
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
142+
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
119143
url: "https://pub.dev"
120144
source: hosted
121-
version: "0.5.0"
145+
version: "0.8.0"
122146
meta:
123147
dependency: transitive
124148
description:
125149
name: meta
126-
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
150+
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
127151
url: "https://pub.dev"
128152
source: hosted
129-
version: "1.10.0"
153+
version: "1.12.0"
130154
ordered_set:
131155
dependency: transitive
132156
description:
@@ -139,10 +163,10 @@ packages:
139163
dependency: transitive
140164
description:
141165
name: path
142-
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
166+
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
143167
url: "https://pub.dev"
144168
source: hosted
145-
version: "1.8.3"
169+
version: "1.9.0"
146170
plugin_platform_interface:
147171
dependency: transitive
148172
description:
@@ -200,10 +224,10 @@ packages:
200224
dependency: transitive
201225
description:
202226
name: test_api
203-
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
227+
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
204228
url: "https://pub.dev"
205229
source: hosted
206-
version: "0.6.1"
230+
version: "0.7.0"
207231
url_launcher:
208232
dependency: "direct main"
209233
description:
@@ -276,6 +300,14 @@ packages:
276300
url: "https://pub.dev"
277301
source: hosted
278302
version: "2.1.4"
303+
vm_service:
304+
dependency: transitive
305+
description:
306+
name: vm_service
307+
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
308+
url: "https://pub.dev"
309+
source: hosted
310+
version: "14.2.1"
279311
web:
280312
dependency: transitive
281313
description:
@@ -285,5 +317,5 @@ packages:
285317
source: hosted
286318
version: "0.3.0"
287319
sdks:
288-
dart: ">=3.2.3 <4.0.0"
289-
flutter: ">=3.16.6"
320+
dart: ">=3.3.0 <4.0.0"
321+
flutter: ">=3.18.0-18.0.pre.54"

lib/core/options.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,23 @@ class Options {
112112
///
113113
/// @zh: overlay消失的延迟
114114
Duration? panelDelay;
115+
116+
/// @en: legend component builder
117+
///
118+
/// @zh: 图例组件
119+
PositionComponent Function(Color color, int i) legendBuilder = (color, i) {
120+
return RectangleComponent.fromRect(Rect.fromLTWH(40, 50.0 + 30 * i, 30, 18),
121+
paint: Paint()..color = color);
122+
};
123+
124+
/// @en: default legend text builder
125+
///
126+
/// @zh: 默认图例文字构建器
127+
TextComponent Function(String tag, int i, Color color, Vector2 position)
128+
legendTextBuilder = (tag, i, color, position) {
129+
return TextComponent(
130+
text: tag,
131+
position: Vector2(position.x + 40, position.y - 6),
132+
);
133+
};
115134
}

lib/widgets/graph_component.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,21 @@ class GraphComponent extends FlameGame
216216
for (var i = legendCount; i < graph.allTags.length; i++) {
217217
var tag = graph.allTags[i];
218218

219-
add(
220-
RectangleComponent.fromRect(
221-
Rect.fromLTWH(40, 50.0 + 30 * i, 30, 18),
222-
paint: Paint()
223-
..color = graphStyle.colorByTag(tag, graph.allTags) ??
224-
(i < graphStyle.tagColorByIndex.length
225-
? graphStyle.tagColorByIndex[i]
226-
: graphStyle.defaultColor()[0]),
227-
),
228-
);
229-
230-
add(TextComponent(text: tag, position: Vector2(40 + 40, 44.0 + 30 * i)));
219+
var legendColor = graphStyle.colorByTag(tag, graph.allTags) ??
220+
(i < graphStyle.tagColorByIndex.length
221+
? graphStyle.tagColorByIndex[i]
222+
: graphStyle.defaultColor()[0]);
223+
224+
// add legend
225+
// 添加图例
226+
var legendItem = options.legendBuilder(legendColor, i);
227+
add(legendItem);
228+
229+
// add legend text
230+
// 添加图例文字
231+
var legendText =
232+
options.legendTextBuilder(tag, i, legendColor, legendItem.position);
233+
add(legendText);
231234

232235
legendCount = i;
233236
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_graph_view
22
description: Widgets for beautiful graphic data structures, such as force-oriented diagrams.
3-
version: 1.1.1
3+
version: 1.1.2
44
repository: https://github.com/dudu-ltd/flutter_graph_view
55
homepage: https://graph-cn.github.io/flutter_graph_view
66

0 commit comments

Comments
 (0)