Skip to content

Commit 2420da5

Browse files
author
xiaoranran
committed
feat: update tree map
1 parent cdd832e commit 2420da5

File tree

4 files changed

+57
-219
lines changed

4 files changed

+57
-219
lines changed

ios/Flutter/.last_build_id

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13893bd379d1ba2b9cab10e31da25434
1+
9fdedd93144233526c9f0f63902a1889

lib/charts/tree_map/draw_tree_rects.dart

+36-50
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import 'package:flutter/material.dart';
44
import '../../colors.dart';
55
import './tree_node.dart';
66

7-
Paint paint = Paint()
8-
..style = PaintingStyle.stroke
9-
..strokeWidth = 2.0;
10-
117
TextPainter textPainter = TextPainter(
128
text: TextSpan(),
139
textAlign: TextAlign.center,
@@ -22,7 +18,7 @@ void _drawText(double value, Canvas canvas, Offset offset) {
2218
text: value.toString(),
2319
style: TextStyle(
2420
fontSize: 24.0,
25-
color: colors[Random().nextInt(colors.length)],
21+
color: Colors.white,
2622
),
2723
);
2824

@@ -35,48 +31,11 @@ void _drawText(double value, Canvas canvas, Offset offset) {
3531
..paint(canvas, offset);
3632
}
3733

38-
void _drawLeftNode(
39-
TreeNode node, Rect rootRect, TreeNode rootNode, Canvas canvas) {
40-
if (node == null) {
41-
return;
42-
}
43-
44-
if (node.left == null && node.right == null) {
45-
return;
46-
}
47-
48-
double left;
49-
double top;
50-
double width;
51-
double height;
52-
53-
left = rootRect.left;
54-
width = rootRect.width;
55-
top = rootRect.top;
56-
height = (node.left.value / rootNode.value) * rootRect.height;
57-
58-
Rect rectLeft = Rect.fromLTWH(left, top, width, height);
59-
canvas.drawRect(rectLeft, paint);
60-
61-
_drawText(
62-
node.left.value,
63-
canvas,
64-
Offset(left + width / 2 - 12.0, top + height / 2 - 12.0),
65-
);
66-
}
67-
68-
void _drawRihgtNode(TreeNode node) {
69-
if (node == null) {
70-
return;
71-
}
72-
}
73-
74-
int level = 0;
75-
7634
void drawTreeRects(
7735
TreeNode node,
7836
Rect rootRectLeft,
7937
TreeNode rootNodeLeft,
38+
int level,
8039
Canvas canvas,
8140
) {
8241
if (node == null) {
@@ -86,6 +45,17 @@ void drawTreeRects(
8645
return;
8746
}
8847

48+
Paint paint = Paint()
49+
..style = PaintingStyle.fill
50+
..color = colors[Random().nextInt(colors.length)]
51+
..isAntiAlias = true;
52+
53+
Paint linePaint = Paint()
54+
..style = PaintingStyle.stroke
55+
..color = Colors.white
56+
..strokeWidth = 2.0
57+
..isAntiAlias = true;
58+
8959
Rect rectLeft;
9060
Rect rectRight;
9161

@@ -95,15 +65,16 @@ void drawTreeRects(
9565
double width;
9666
double height;
9767

68+
top = rootRectLeft.top;
9869
left = rootRectLeft.left;
9970
width = rootRectLeft.width;
100-
top = rootRectLeft.top;
10171
height = (node.left.value / rootNodeLeft.value) * rootRectLeft.height;
102-
10372
rectLeft = Rect.fromLTWH(left, top, width, height);
104-
canvas.drawRect(rectLeft, paint);
10573

10674
if (node.left.left == null && node.left.right == null) {
75+
canvas.drawRect(rectLeft, paint);
76+
canvas.drawRect(rectLeft, linePaint);
77+
10778
_drawText(
10879
node.left.value,
10980
canvas,
@@ -115,11 +86,12 @@ void drawTreeRects(
11586
top = rootRectLeft.top + height;
11687
width = rootRectLeft.width;
11788
height = (node.right.value / rootNodeLeft.value) * rootRectLeft.height;
118-
11989
rectRight = Rect.fromLTWH(left, top, width, height);
120-
canvas.drawRect(rectRight, paint);
12190

12291
if (node.right.left == null && node.right.right == null) {
92+
canvas.drawRect(rectRight, paint);
93+
canvas.drawRect(rectRight, linePaint);
94+
12395
_drawText(
12496
node.right.value,
12597
canvas,
@@ -136,9 +108,11 @@ void drawTreeRects(
136108
left = rootRectLeft.left;
137109
width = (node.left.value / rootNodeLeft.value) * rootRectLeft.width;
138110
rectLeft = Rect.fromLTWH(left, top, width, height);
139-
canvas.drawRect(rectLeft, paint);
140111

141112
if (node.left.left == null && node.left.right == null) {
113+
canvas.drawRect(rectLeft, paint);
114+
canvas.drawRect(rectLeft, linePaint);
115+
142116
_drawText(
143117
node.left.value,
144118
canvas,
@@ -151,7 +125,17 @@ void drawTreeRects(
151125
left = rootRectLeft.left + width;
152126
width = (node.right.value / rootNodeLeft.value) * rootRectLeft.width;
153127
rectRight = Rect.fromLTWH(left, top, width, height);
154-
canvas.drawRect(rectRight, paint);
128+
129+
if (node.left.left == null && node.left.right == null) {
130+
canvas.drawRect(rectRight, paint);
131+
canvas.drawRect(rectRight, linePaint);
132+
133+
_drawText(
134+
node.right.value,
135+
canvas,
136+
Offset(left + width / 2 - 12.0, top + height / 2 - 12.0),
137+
);
138+
}
155139
}
156140

157141
level++;
@@ -160,13 +144,15 @@ void drawTreeRects(
160144
node.left,
161145
rectLeft,
162146
node.left,
147+
level,
163148
canvas,
164149
);
165150

166151
drawTreeRects(
167152
node.right,
168153
rectRight,
169154
node.right,
155+
level,
170156
canvas,
171157
);
172158
}

lib/charts/tree_map/tree_map.dart

+19-14
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,41 @@ import './draw_tree_rects.dart';
88
// 2. 前序遍历二叉树
99
// 3. 遍历过程中绘制每个字树
1010

11-
class TreeMap extends StatefulWidget {
12-
@override
13-
_TreeMapState createState() => _TreeMapState();
14-
}
11+
class TreeMap extends StatelessWidget {
12+
final List<double> datas;
13+
14+
const TreeMap({@required this.datas});
1515

16-
class _TreeMapState extends State<TreeMap> {
1716
@override
1817
Widget build(BuildContext context) {
19-
return CustomPaint(
20-
painter: TreeMapPainter(),
21-
child: Container(
22-
width: 500,
23-
height: 500,
24-
),
18+
return LayoutBuilder(
19+
builder: (BuildContext context, BoxConstraints constraints) {
20+
return CustomPaint(
21+
painter: TreeMapPainter(
22+
datas: datas,
23+
),
24+
size: constraints.biggest,
25+
);
26+
},
2527
);
2628
}
2729
}
2830

2931
class TreeMapPainter extends CustomPainter {
32+
final List<double> datas;
33+
34+
TreeMapPainter({@required this.datas});
35+
3036
@override
3137
void paint(Canvas canvas, Size size) {
32-
List<double> input = [2, 10, 4, 3, 7, 5, 9, 8, 1, 6];
3338
Paint paint = Paint()
3439
..color = Colors.black12
3540
..style = PaintingStyle.fill;
3641

3742
Rect rootRect = Rect.fromLTWH(0, 0, size.width, size.height);
38-
TreeNode rootNode = parseArrayToBST(input);
43+
TreeNode rootNode = parseArrayToBST(datas);
3944
canvas.drawRect(rootRect, paint);
40-
drawTreeRects(rootNode, rootRect, rootNode, canvas);
45+
drawTreeRects(rootNode, rootRect, rootNode, 0, canvas);
4146
}
4247

4348
@override

lib/pages/tree_map_page.dart

+1-154
Original file line numberDiff line numberDiff line change
@@ -11,159 +11,6 @@ class TreeMapPage extends StatefulWidget {
1111
class _TreeMapPageState extends State<TreeMapPage> {
1212
@override
1313
Widget build(BuildContext context) {
14-
return Center(
15-
child: TreeMap(),
16-
);
14+
return TreeMap(datas: [2, 10, 4, 3, 7, 5, 9, 8, 1, 6]);
1715
}
1816
}
19-
20-
// class Square {
21-
// final double x;
22-
// final double y;
23-
// final double width;
24-
// final double height;
25-
// final Color color;
26-
27-
// Square({
28-
// this.x,
29-
// this.y,
30-
// this.width,
31-
// this.height,
32-
// this.color,
33-
// });
34-
35-
// Map toJson() {
36-
// return {
37-
// "x": x,
38-
// "y": y,
39-
// "width": width,
40-
// "height": height,
41-
// "color": color.toString(),
42-
// };
43-
// }
44-
// }
45-
46-
// class TreeMapPainter extends CustomPainter {
47-
// void _draw(Canvas canvas, List<Square> squares) {
48-
// final Paint paint = Paint()
49-
// ..strokeWidth = 2
50-
// ..isAntiAlias = true
51-
// ..style = PaintingStyle.fill;
52-
53-
// final Paint paint2 = Paint()
54-
// ..strokeWidth = 2
55-
// ..isAntiAlias = true
56-
// ..color = Colors.black54
57-
// ..style = PaintingStyle.stroke;
58-
59-
// for (int i = 0; i < squares.length; i++) {
60-
// final Square square = squares[i];
61-
// final Rect rect = Rect.fromLTWH(
62-
// square.x,
63-
// square.y,
64-
// square.width,
65-
// square.height,
66-
// );
67-
68-
// paint.color = square.color;
69-
// // paint.color = Random().nextBool()
70-
// // ? Colors.white
71-
// // : colors[Random().nextInt(colors.length)].withOpacity(.8);
72-
73-
// canvas.drawRect(rect, paint);
74-
// canvas.drawRect(rect, paint2);
75-
// }
76-
// }
77-
78-
// void _splitSquaresWith(Map coordinates, List<Square> squares) {
79-
// final double x = coordinates["x"];
80-
// final double y = coordinates["y"];
81-
82-
// for (int i = squares.length - 1; i >= 0; i--) {
83-
// final Square square = squares[i];
84-
85-
// if (x != null && x > square.x && x < square.x + square.width) {
86-
// if (Random().nextBool()) {
87-
// squares.removeAt(i);
88-
// _splitOnX(square, x, squares);
89-
// }
90-
// }
91-
92-
// if (y != null && y > square.y && y < square.y + square.height) {
93-
// if (Random().nextBool()) {
94-
// squares.removeAt(i);
95-
// _splitOnY(square, y, squares);
96-
// }
97-
// }
98-
// }
99-
// }
100-
101-
// void _splitOnX(Square square, double splitAt, List<Square> squares) {
102-
// final Square squareA = Square(
103-
// x: square.x,
104-
// y: square.y,
105-
// width: square.width - (square.width - splitAt + square.x),
106-
// height: square.height,
107-
// color: colors[Random().nextInt(colors.length)].withOpacity(.8),
108-
// );
109-
110-
// final Square squareB = Square(
111-
// x: splitAt,
112-
// y: square.y,
113-
// width: square.width - splitAt + square.x,
114-
// height: square.height,
115-
// color: colors[Random().nextInt(colors.length)].withOpacity(.8),
116-
// );
117-
118-
// squares.add(squareA);
119-
// squares.add(squareB);
120-
// }
121-
122-
// void _splitOnY(Square square, double splitAt, List<Square> squares) {
123-
// final Square squareA = Square(
124-
// x: square.x,
125-
// y: square.y,
126-
// width: square.width,
127-
// height: square.height - (square.height - splitAt + square.y),
128-
// color: colors[Random().nextInt(colors.length)].withOpacity(.8),
129-
// );
130-
131-
// final Square squareB = Square(
132-
// x: square.x,
133-
// y: splitAt,
134-
// width: square.width,
135-
// height: square.height - splitAt + square.y,
136-
// color: colors[Random().nextInt(colors.length)].withOpacity(.8),
137-
// );
138-
139-
// squares.add(squareA);
140-
// squares.add(squareB);
141-
// }
142-
143-
// @override
144-
// void paint(Canvas canvas, Size size) {
145-
// final List<Square> squares = [
146-
// Square(
147-
// x: 0,
148-
// y: 0,
149-
// width: size.width,
150-
// height: size.height,
151-
// color: Colors.white,
152-
// ),
153-
// ];
154-
155-
// double step = size.width / 7;
156-
157-
// for (double i = 0; i < size.width; i += step) {
158-
// _splitSquaresWith({"y": i}, squares);
159-
// _splitSquaresWith({"x": i}, squares);
160-
// }
161-
// _draw(canvas, squares);
162-
// }
163-
164-
// @override
165-
// bool shouldRepaint(TreeMapPainter oldDelegate) => false;
166-
167-
// @override
168-
// bool shouldRebuildSemantics(TreeMapPainter oldDelegate) => false;
169-
// }

0 commit comments

Comments
 (0)