-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path015.吊线图.html
303 lines (274 loc) · 18.9 KB
/
015.吊线图.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./assets/global.css">
</head>
<body>
<canvas id="canvas" width="1000" height="1000"></canvas>
<script>
// 字体大小
let fontSize = 10;
// 字间距
let fontSpacing = 12;
// 行高
let fontLineHeight = fontSpacing * 5;
// 绘制宽度
let drawWidth = 0;
// 绘制高度
let drawHeight = 0;
// 外边距
let margin = 20;
let marginLeft = margin;
let marginRight = margin;
let marginTop = margin;
let marginBottom = margin;
// 内边距
let padding = 20;
let paddingLeft = padding;
let paddingRight = padding;
let paddingTop = padding;
let paddingBottom = padding;
let maxX = 0;
let maxY = 0;
let fillCircleRadius = fontSize / 5;
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
let persons = [
{
name: "林自强"
},
{
name: "林自励",
children: [
{
name: '林懋進',
children: [
{
name: "林腾芳",
children: [
{
name: "林雨澍",
children: [
{
name: "林琨树",
children: [
{
name: "林弘任",
children: [
{
name: "林世荣",
children: [
{
name: '林元章',
},
{
name: "林耑",
children: [
{
name: "林蔚",
children: [
{
name: '林梦鳌',
children: [
{
name: '林中方',
children: [
{
name: "林松山",
},
{
name: "林藻英",
children: [
{
name: '林雲骕',
children: [
{
name: "林丰泰",
}
]
},
{
name: "林雲祥",
children: [
{
name: "林丰有",
},
{
name: "林丰裕",
},
{
name: "林丰益",
}
]
}
]
},
{
name: "林藻思",
children: [
{
name: "林雲龍",
children: [
{
name: "林丰敬",
},
{
name: "林丰和",
}
]
},
{
name: "林雲会",
children: [
{
name: "林丰年",
}
]
},
{
name: "林雲鶴",
},
{
name: "林雲台",
children: [
{
name: '林丰魁',
}
]
},
{
name: "林雲仙",
children: [
{
name: "林丰義",
},
{
name: "林丰安",
}
]
},
{
name: "林雲亭",
children: [
{
name: "林丰合",
},
{
name: "林丰庆",
},
{
name: "林丰恒",
}
]
}
]
},
]
},
{
name: '林義方',
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
},
{
name: "林自用"
},
{
name: "林自好"
},
{
name: "林自觀"
},
]
function fillTextByVertical(ctx, text, x, y, fontSize) {
ctx.textBaseline = "top";
ctx.font = `${fontSize}px Arial`;
for (let i = 0; i < text.length; i++) {
ctx.fillText(text[i], x, y + fontSize * i);
}
}
function fillCircle(x, y, r) {
ctx.beginPath();
ctx.arc(x, y, r, 0, 2 * Math.PI);
ctx.stroke();
}
function fillLine(sx, sy, ex, ey) {
ctx.moveTo(sx, sy);
ctx.lineTo(ex, ey);
ctx.stroke();
}
// 不考虑性能
function renderPerson(persons, x, y, px, py) {
console.log("父节点位置", x, y, persons, px, py);
let brotherX;
let brotherY;
if (px && py) {
fillLine(px + (fontSpacing - fillCircleRadius) / 2, py + fontSpacing * 3, px + (fontSpacing - fillCircleRadius) / 2, py + fontSpacing * 4)
}
for (let i = 0; i < persons.length; i++) {
let person = persons[i];
let cx = parseInt(weightPerson(person) / 2);
let tx = x + cx * fontSpacing
fillTextByVertical(ctx, person.name, tx, y, fontSize)
if (px && py) {
fillCircle(tx + (fontSpacing - fillCircleRadius) / 2, y - fontSpacing, fillCircleRadius)
fillLine(tx + (fontSpacing - fillCircleRadius) / 2, py + fontSpacing * 4, tx + (fontSpacing - fillCircleRadius) / 2, py + fontSpacing * 5)
if (brotherX && brotherY) {
fillLine(brotherX + (fontSpacing - fillCircleRadius) / 2, brotherY - fontSpacing, tx + (fontSpacing - fillCircleRadius) / 2, y - fontSpacing)
}
brotherX = tx;
brotherY = y;
}
if (person.children) renderPerson(person.children, x, y + fontLineHeight, tx, y)
x += weightPerson(person) * fontSpacing
}
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
// 权重
function weightPerson(person) {
if (!person.children) return 1;
let weight = 0;
for (const p of person.children) {
let childCount = p.children ? weightPerson(p) : 0;
childCount = childCount + (childCount % 2 == 0 ? 1 : 0)
weight += p.children ? childCount : 1
}
return weight
}
function renderRect() {
ctx.fillStyle = "#000000";
ctx.moveTo(marginLeft, marginTop);
ctx.lineTo(maxX + paddingRight, marginTop);
ctx.lineTo(maxX + paddingRight, maxY + fontLineHeight + paddingBottom);
ctx.lineTo(marginLeft, maxY + fontLineHeight + paddingBottom);
ctx.lineTo(marginLeft, marginTop);
ctx.stroke();
}
renderPerson(persons, marginLeft + paddingLeft, marginTop + paddingTop)
renderRect()
</script>
</body>
</html>