FamilyRelationGraph.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <template>
  2. <!-- 全 Canvas 关系图谱 - Force-directed 物理布局 -->
  3. <view class="family-graph-wrapper" v-if="simNodes && simNodes.length >= 1">
  4. <canvas
  5. class="family-graph-canvas"
  6. canvas-id="forceGraphCanvas"
  7. id="forceGraphCanvas"
  8. type="2d"
  9. :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"
  10. @touchstart="onTouchStart"
  11. @touchmove="onTouchMove"
  12. @touchend="onTouchEnd" />
  13. </view>
  14. <!-- 空状态 -->
  15. <view class="family-graph-empty" v-else-if="members && members.length === 0">
  16. <text class="empty-text">暂无家庭成员</text>
  17. </view>
  18. </template>
  19. <script>
  20. /**
  21. * FamilyRelationGraph - 全 Canvas force-directed 家庭关系图
  22. *
  23. * 物理引擎: 手动实现的 force-directed 布局
  24. * - 节点间斥力 (repulsion)
  25. * - 边弹簧引力 (spring attraction)
  26. * - 中心引力 (centering)
  27. * - 阻尼 (damping)
  28. *
  29. * 渲染: 全 Canvas 绘制 (节点 + 连线)
  30. * 交互: 节点拖拽 + 点击检测
  31. */
  32. export default {
  33. name: 'FamilyRelationGraph',
  34. props: {
  35. dimensionCode: {
  36. type: String,
  37. required: true,
  38. validator: function(val) {
  39. return ['body', 'mind', 'action', 'home', 'wisdom'].indexOf(val) !== -1
  40. }
  41. },
  42. selfId: {
  43. type: [Number, String],
  44. default: null
  45. },
  46. members: {
  47. type: Array,
  48. default: function() { return [] }
  49. },
  50. energyMap: {
  51. type: Object,
  52. default: function() { return {} }
  53. },
  54. intimacyMap: {
  55. type: Object,
  56. default: function() { return {} }
  57. },
  58. interactive: {
  59. type: Boolean,
  60. default: true
  61. },
  62. compatibilityMap: {
  63. type: Object,
  64. default: function() { return {} }
  65. }
  66. },
  67. data: function() {
  68. return {
  69. canvasWidth: 340,
  70. canvasHeight: 280,
  71. dpr: 1,
  72. // 物理模拟节点
  73. simNodes: [],
  74. // 物理参数
  75. physics: {
  76. repulsion: 800,
  77. springLength: 120,
  78. springStrength: 0.04,
  79. centering: 0.03,
  80. damping: 0.88,
  81. maxSpeed: 12,
  82. minDistance: 30
  83. },
  84. // 拖拽状态
  85. dragging: null,
  86. dragStartPos: null,
  87. // 动画
  88. animFrameId: null,
  89. isSimulating: false,
  90. // 主题色
  91. themeColors: {
  92. body: '#8D6E63',
  93. mind: '#FF6B35',
  94. action: '#4CAF50',
  95. home: '#5B9BD5',
  96. wisdom: '#FFD700'
  97. }
  98. }
  99. },
  100. computed: {
  101. themeColor: function() {
  102. return this.themeColors[this.dimensionCode] || '#8D6E63'
  103. },
  104. // 将 members 转换为 simNodes
  105. nodeData: function() {
  106. var self = this
  107. if (!this.members || this.members.length === 0) return []
  108. var centerX = this.canvasWidth / 2
  109. var centerY = this.canvasHeight / 2
  110. return this.members.map(function(m, idx) {
  111. var fid = m.memberId || m.id
  112. var energy = self.getEnergy(fid)
  113. var radius = 24 + (Math.max(0, Math.min(100, energy)) / 100) * 16
  114. var nick = m.nickname || m.name || '成员'
  115. if (nick === '用户') nick = '成员'
  116. var isSelf = (fid == self.selfId)
  117. var displayName = nick.charAt(0)
  118. // 圆形布局初始位置
  119. var angle = (2 * Math.PI * idx) / Math.max(1, self.members.length)
  120. var r = Math.min(self.canvasWidth, self.canvasHeight) * 0.32
  121. var initX = centerX + r * Math.cos(angle)
  122. var initY = centerY + r * Math.sin(angle)
  123. return {
  124. id: fid,
  125. nickname: nick,
  126. displayName: displayName,
  127. isSelf: isSelf,
  128. memberType: m.memberType || 'child',
  129. relationshipType: m.relationshipType || '',
  130. radius: radius,
  131. x: initX,
  132. y: initY,
  133. vx: 0,
  134. vy: 0,
  135. fx: 0,
  136. fy: 0
  137. }
  138. })
  139. },
  140. // 构建边 (self 连接到其他所有成员)
  141. edges: function() {
  142. var self = this
  143. var selfNode = this.nodeData.find(function(n) { return n.isSelf })
  144. if (!selfNode) return []
  145. var result = []
  146. this.nodeData.forEach(function(n) {
  147. if (n.id !== selfNode.id) {
  148. var pairKey = selfNode.id < n.id
  149. ? selfNode.id + '-' + n.id
  150. : n.id + '-' + selfNode.id
  151. var comp = self.compatibilityMap[pairKey]
  152. var trust = self.getIntimacy(n.id, 'trust')
  153. var comm = self.getIntimacy(n.id, 'communication')
  154. var closeness = self.getIntimacy(n.id, 'closeness')
  155. result.push({
  156. source: selfNode,
  157. target: n,
  158. trust: trust,
  159. communication: comm,
  160. closeness: closeness,
  161. compatibility: comp && comp.overallScore !== undefined ? comp.overallScore : null
  162. })
  163. }
  164. })
  165. return result
  166. }
  167. },
  168. watch: {
  169. members: function() {
  170. this.resetSimulation()
  171. },
  172. canvasWidth: function() {
  173. this.resetSimulation()
  174. },
  175. canvasHeight: function() {
  176. this.resetSimulation()
  177. }
  178. },
  179. mounted: function() {
  180. var self = this
  181. this.dpr = uni.getSystemInfoSync().pixelRatio || 1
  182. this.$nextTick(function() {
  183. self.updateCanvasSize()
  184. self.$nextTick(function() {
  185. self.initSimulation()
  186. })
  187. })
  188. },
  189. beforeDestroy: function() {
  190. this.stopSimulation()
  191. },
  192. methods: {
  193. // ===== 物理引擎 =====
  194. resetSimulation: function() {
  195. this.stopSimulation()
  196. var centerX = this.canvasWidth / 2
  197. var centerY = this.canvasHeight / 2
  198. var self = this
  199. this.nodeData.forEach(function(n, idx) {
  200. var angle = (2 * Math.PI * idx) / Math.max(1, self.nodeData.length)
  201. var r = Math.min(self.canvasWidth, self.canvasHeight) * 0.32
  202. n.x = centerX + r * Math.cos(angle)
  203. n.y = centerY + r * Math.sin(angle)
  204. n.vx = 0
  205. n.vy = 0
  206. n.fx = 0
  207. n.fy = 0
  208. })
  209. this.startSimulation()
  210. },
  211. initSimulation: function() {
  212. if (!this.nodeData || this.nodeData.length === 0) return
  213. // 复制到 simNodes
  214. this.simNodes = this.nodeData.slice()
  215. this.startSimulation()
  216. },
  217. startSimulation: function() {
  218. if (this.isSimulating) return
  219. this.isSimulating = true
  220. this.simulationTick()
  221. },
  222. stopSimulation: function() {
  223. this.isSimulating = false
  224. if (this.animFrameId) {
  225. cancelAnimationFrame(this.animFrameId)
  226. this.animFrameId = null
  227. }
  228. },
  229. simulationTick: function() {
  230. if (!this.isSimulating) return
  231. var self = this
  232. var nodes = this.simNodes
  233. var p = this.physics
  234. // 1. 清零力
  235. for (var i = 0; i < nodes.length; i++) {
  236. nodes[i].fx = 0
  237. nodes[i].fy = 0
  238. }
  239. // 2. 斥力
  240. for (var a = 0; a < nodes.length; a++) {
  241. for (var b = a + 1; b < nodes.length; b++) {
  242. var dx = nodes[b].x - nodes[a].x
  243. var dy = nodes[b].y - nodes[a].y
  244. var distSq = dx * dx + dy * dy
  245. var dist = Math.sqrt(distSq) || 0.1
  246. var force = p.repulsion / distSq
  247. var fx = (dx / dist) * force
  248. var fy = (dy / dist) * force
  249. nodes[a].fx -= fx
  250. nodes[a].fy -= fy
  251. nodes[b].fx += fx
  252. nodes[b].fy += fy
  253. }
  254. }
  255. // 3. 弹簧引力
  256. for (var e = 0; e < this.edges.length; e++) {
  257. var edge = this.edges[e]
  258. var s = edge.source
  259. var t = edge.target
  260. var dx = t.x - s.x
  261. var dy = t.y - s.y
  262. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  263. var displacement = dist - p.springLength
  264. var force = p.springStrength * displacement
  265. var fx = (dx / dist) * force
  266. var fy = (dy / dist) * force
  267. s.fx += fx
  268. s.fy += fy
  269. t.fx -= fx
  270. t.fy -= fy
  271. }
  272. // 4. 中心引力
  273. var cx = this.canvasWidth / 2
  274. var cy = this.canvasHeight / 2
  275. for (var n = 0; n < nodes.length; n++) {
  276. nodes[n].fx += (cx - nodes[n].x) * p.centering
  277. nodes[n].fy += (cy - nodes[n].y) * p.centering
  278. }
  279. // 5. 更新速度 + 位置
  280. for (var i = 0; i < nodes.length; i++) {
  281. var node = nodes[i]
  282. node.vx += node.fx
  283. node.vy += node.fy
  284. node.vx *= p.damping
  285. node.vy *= p.damping
  286. var speed = Math.sqrt(node.vx * node.vx + node.vy * node.vy)
  287. if (speed > p.maxSpeed) {
  288. node.vx = (node.vx / speed) * p.maxSpeed
  289. node.vy = (node.vy / speed) * p.maxSpeed
  290. }
  291. node.x += node.vx
  292. node.y += node.vy
  293. // 边界
  294. var r = node.radius + 4
  295. if (node.x < r) { node.x = r; node.vx *= -0.5 }
  296. if (node.x > this.canvasWidth - r) { node.x = this.canvasWidth - r; node.vx *= -0.5 }
  297. if (node.y < r) { node.y = r; node.vy *= -0.5 }
  298. if (node.y > this.canvasHeight - r) { node.y = this.canvasHeight - r; node.vy *= -0.5 }
  299. }
  300. // 6. 渲染
  301. this.render()
  302. // 7. 下一帧
  303. this.animFrameId = requestAnimationFrame(function() {
  304. self.simulationTick()
  305. })
  306. },
  307. // ===== 渲染 =====
  308. render: function() {
  309. var self = this
  310. var query = uni.createSelectorQuery().in(this)
  311. query.select('#forceGraphCanvas')
  312. .fields({ node: true, size: true })
  313. .exec(function(res) {
  314. if (!res || !res[0] || !res[0].node) {
  315. self.renderLegacy()
  316. return
  317. }
  318. var canvas = res[0].node
  319. var ctx = canvas.getContext('2d')
  320. canvas.width = self.canvasWidth * self.dpr
  321. canvas.height = self.canvasHeight * self.dpr
  322. ctx.scale(self.dpr, self.dpr)
  323. ctx.clearRect(0, 0, self.canvasWidth, self.canvasHeight)
  324. self.drawEdges(ctx)
  325. self.drawNodes(ctx)
  326. })
  327. },
  328. drawEdges: function(ctx) {
  329. var edges = this.edges
  330. for (var i = 0; i < edges.length; i++) {
  331. var edge = edges[i]
  332. var s = edge.source
  333. var t = edge.target
  334. var trust = edge.trust || 75
  335. var comm = edge.communication || 50
  336. var hue = 120 * trust / 100
  337. ctx.strokeStyle = 'hsla(' + hue + ', 75%, 45%, 0.6)'
  338. ctx.lineWidth = 1 + (comm / 100) * 4
  339. ctx.beginPath()
  340. ctx.moveTo(s.x, s.y)
  341. ctx.lineTo(t.x, t.y)
  342. ctx.stroke()
  343. }
  344. },
  345. drawNodes: function(ctx) {
  346. var nodes = this.simNodes
  347. for (var i = 0; i < nodes.length; i++) {
  348. var node = nodes[i]
  349. var r = node.radius
  350. if (node.isSelf) {
  351. // 自我:双层圆环
  352. ctx.beginPath()
  353. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  354. ctx.fillStyle = '#FFFFFF'
  355. ctx.fill()
  356. ctx.strokeStyle = this.themeColor
  357. ctx.lineWidth = 3
  358. ctx.stroke()
  359. // 内环
  360. ctx.beginPath()
  361. ctx.arc(node.x, node.y, r * 0.7, 0, 2 * Math.PI)
  362. ctx.strokeStyle = this.themeColor + '88'
  363. ctx.lineWidth = 2
  364. ctx.stroke()
  365. } else if (node.relationshipType === 'spouse') {
  366. // 配偶:心形
  367. this.drawHeart(ctx, node.x, node.y, r)
  368. ctx.fillStyle = this.themeColor + '22'
  369. ctx.fill()
  370. } else if (node.relationshipType === 'parent') {
  371. // 父母:圆角方形
  372. this.drawRoundedRect(ctx, node.x, node.y, r * 2, r * 2, r * 0.3)
  373. ctx.fillStyle = this.themeColor + '22'
  374. ctx.fill()
  375. } else if (node.relationshipType === 'sibling') {
  376. // 兄弟姐妹:八边形
  377. this.drawPolygon(ctx, node.x, node.y, r, 8)
  378. ctx.fillStyle = this.themeColor + '22'
  379. ctx.fill()
  380. } else {
  381. // 孩子/默认:圆形
  382. ctx.beginPath()
  383. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  384. ctx.fillStyle = this.themeColor + '22'
  385. ctx.fill()
  386. }
  387. ctx.fillStyle = node.isSelf ? this.themeColor : '#4A5568'
  388. ctx.font = 'bold ' + (r * 0.9) + 'px sans-serif'
  389. ctx.textAlign = 'center'
  390. ctx.textBaseline = 'middle'
  391. ctx.fillText(node.displayName, node.x, node.y)
  392. ctx.fillStyle = node.isSelf ? this.themeColor : '#94A3B8'
  393. ctx.font = (r * 0.4) + 'px sans-serif'
  394. ctx.fillText(node.nickname.substring(0, 3), node.x, node.y + r + 10)
  395. }
  396. },
  397. renderLegacy: function() {
  398. var ctx = uni.createCanvasContext('forceGraphCanvas', this)
  399. var self = this
  400. ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
  401. var edges = this.edges
  402. for (var i = 0; i < edges.length; i++) {
  403. var edge = edges[i]
  404. var trust = edge.trust || 75
  405. var comm = edge.communication || 50
  406. var hue = 120 * trust / 100
  407. ctx.setStrokeStyle('hsla(' + hue + ', 75%, 45%, 0.6)')
  408. ctx.setLineWidth(1 + (comm / 100) * 4)
  409. ctx.beginPath()
  410. ctx.moveTo(edge.source.x, edge.source.y)
  411. ctx.lineTo(edge.target.x, edge.target.y)
  412. ctx.stroke()
  413. }
  414. var nodes = this.simNodes
  415. for (var j = 0; j < nodes.length; j++) {
  416. var node = nodes[j]
  417. var r = node.radius
  418. if (node.isSelf) {
  419. ctx.beginPath()
  420. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  421. ctx.setFillStyle('#FFFFFF')
  422. ctx.fill()
  423. ctx.setStrokeStyle(this.themeColor)
  424. ctx.setLineWidth(3)
  425. ctx.stroke()
  426. // inner ring
  427. ctx.beginPath()
  428. ctx.arc(node.x, node.y, r * 0.7, 0, 2 * Math.PI)
  429. ctx.setStrokeStyle(this.themeColor + '88')
  430. ctx.setLineWidth(2)
  431. ctx.stroke()
  432. } else if (node.relationshipType === 'spouse') {
  433. this.drawHeartLegacy(ctx, node.x, node.y, r)
  434. ctx.setFillStyle(this.themeColor + '22')
  435. ctx.fill()
  436. } else if (node.relationshipType === 'parent') {
  437. this.drawRoundedRectLegacy(ctx, node.x, node.y, r * 2, r * 2, r * 0.3)
  438. ctx.setFillStyle(this.themeColor + '22')
  439. ctx.fill()
  440. } else if (node.relationshipType === 'sibling') {
  441. this.drawPolygonLegacy(ctx, node.x, node.y, r, 8)
  442. ctx.setFillStyle(this.themeColor + '22')
  443. ctx.fill()
  444. } else {
  445. ctx.beginPath()
  446. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  447. ctx.setFillStyle(this.themeColor + '22')
  448. ctx.fill()
  449. }
  450. ctx.setFillStyle(node.isSelf ? this.themeColor : '#4A5568')
  451. ctx.setFont('bold ' + (r * 0.9) + 'px sans-serif')
  452. ctx.setTextAlign('center')
  453. ctx.setTextBaseline('middle')
  454. ctx.fillText(node.displayName, node.x, node.y)
  455. ctx.setFillStyle(node.isSelf ? this.themeColor : '#94A3B8')
  456. ctx.setFont((r * 0.4) + 'px sans-serif')
  457. ctx.fillText(node.nickname.substring(0, 3), node.x, node.y + r + 10)
  458. }
  459. ctx.draw()
  460. },
  461. // ===== 交互 =====
  462. getTouchNode: function(touchX, touchY) {
  463. var nodes = this.simNodes
  464. for (var i = 0; i < nodes.length; i++) {
  465. var node = nodes[i]
  466. var dx = touchX - node.x
  467. var dy = touchY - node.y
  468. var dist = Math.sqrt(dx * dx + dy * dy)
  469. if (dist <= node.radius + 8) {
  470. return node
  471. }
  472. }
  473. return null
  474. },
  475. onTouchStart: function(e) {
  476. if (!this.interactive) return
  477. var touch = e.touches ? e.touches[0] : e.detail
  478. if (!touch) return
  479. var self = this
  480. var rect = {}
  481. var query = uni.createSelectorQuery().in(this)
  482. query.select('#forceGraphCanvas').boundingClientRect()
  483. query.exec(function(res) {
  484. if (res && res[0]) {
  485. rect = res[0]
  486. var x = touch.clientX - rect.left
  487. var y = touch.clientY - rect.top
  488. var node = self.getTouchNode(x, y)
  489. if (node) {
  490. self.dragging = { node: node, offsetX: x - node.x, offsetY: y - node.y }
  491. self.dragStartPos = { x: x, y: y }
  492. }
  493. }
  494. })
  495. },
  496. onTouchMove: function(e) {
  497. if (!this.dragging) return
  498. e.preventDefault && e.preventDefault()
  499. var touch = e.touches ? e.touches[0] : e.detail
  500. if (!touch) return
  501. var self = this
  502. var rect = {}
  503. var query = uni.createSelectorQuery().in(this)
  504. query.select('#forceGraphCanvas').boundingClientRect()
  505. query.exec(function(res) {
  506. if (res && res[0]) {
  507. rect = res[0]
  508. var x = touch.clientX - rect.left
  509. var y = touch.clientY - rect.top
  510. var node = self.dragging.node
  511. node.x = x - self.dragging.offsetX
  512. node.y = y - self.dragging.offsetY
  513. }
  514. })
  515. },
  516. onTouchEnd: function(e) {
  517. if (!this.dragging) return
  518. var wasDragged = this.dragStartPos && (
  519. Math.abs(this.dragging.node.x - this.dragStartPos.x) > 5 ||
  520. Math.abs(this.dragging.node.y - this.dragStartPos.y) > 5
  521. )
  522. var node = this.dragging.node
  523. if (!wasDragged && this.interactive) {
  524. this.$emit('memberTap', {
  525. memberId: node.id,
  526. memberType: node.memberType,
  527. nickname: node.nickname
  528. })
  529. }
  530. this.dragging = null
  531. this.dragStartPos = null
  532. },
  533. // ===== 工具方法 =====
  534. getEnergy: function(memberId) {
  535. var data = this.energyMap[memberId]
  536. if (!data) return 0
  537. var key = this.dimensionCode + 'Score'
  538. var val = data[key]
  539. return (typeof val !== 'undefined' && val !== null) ? val : 0
  540. },
  541. getIntimacy: function(memberId, key) {
  542. var data = this.intimacyMap[memberId]
  543. if (!data) {
  544. var defaults = { closeness: 75, communication: 50, trust: 85 }
  545. return defaults[key]
  546. }
  547. var val = data[key]
  548. return (typeof val !== 'undefined' && val !== null) ? val : 75
  549. },
  550. updateCanvasSize: function() {
  551. var self = this
  552. var query = uni.createSelectorQuery().in(this)
  553. query.select('.family-graph-wrapper').boundingClientRect(function(rect) {
  554. if (rect) {
  555. self.canvasWidth = rect.width || 340
  556. self.canvasHeight = rect.height || 280
  557. }
  558. }).exec()
  559. },
  560. // ===== 形状绘制辅助方法 =====
  561. drawHeart: function(ctx, cx, cy, size) {
  562. ctx.beginPath()
  563. var topX = cx
  564. var topY = cy - size * 0.4
  565. ctx.moveTo(topX, topY)
  566. // 左曲线
  567. ctx.bezierCurveTo(
  568. cx - size * 0.9, cy - size * 0.7,
  569. cx - size * 0.6, cy + size * 0.5,
  570. cx, cy + size * 0.8
  571. )
  572. // 右曲线
  573. ctx.bezierCurveTo(
  574. cx + size * 0.6, cy + size * 0.5,
  575. cx + size * 0.9, cy - size * 0.7,
  576. cx, cy - size * 0.4
  577. )
  578. ctx.closePath()
  579. },
  580. drawRoundedRect: function(ctx, cx, cy, w, h, r) {
  581. ctx.beginPath()
  582. var x = cx - w / 2
  583. var y = cy - h / 2
  584. ctx.moveTo(x + r, y)
  585. ctx.arcTo(x + w, y, x + w, y + h, r)
  586. ctx.arcTo(x + w, y + h, x, y + h, r)
  587. ctx.arcTo(x, y + h, x, y, r)
  588. ctx.arcTo(x, y, x + w, y, r)
  589. ctx.closePath()
  590. },
  591. drawPolygon: function(ctx, cx, cy, r, sides) {
  592. ctx.beginPath()
  593. var startAngle = -Math.PI / 2
  594. for (var i = 0; i < sides; i++) {
  595. var angle = startAngle + (2 * Math.PI * i) / sides
  596. var px = cx + r * Math.cos(angle)
  597. var py = cy + r * Math.sin(angle)
  598. if (i === 0) {
  599. ctx.moveTo(px, py)
  600. } else {
  601. ctx.lineTo(px, py)
  602. }
  603. }
  604. ctx.closePath()
  605. },
  606. drawHeartLegacy: function(ctx, cx, cy, size) {
  607. ctx.beginPath()
  608. ctx.moveTo(cx, cy - size * 0.4)
  609. ctx.bezierCurveTo(cx - size * 0.9, cy - size * 0.7, cx - size * 0.6, cy + size * 0.5, cx, cy + size * 0.8)
  610. ctx.bezierCurveTo(cx + size * 0.6, cy + size * 0.5, cx + size * 0.9, cy - size * 0.7, cx, cy - size * 0.4)
  611. ctx.closePath()
  612. },
  613. drawRoundedRectLegacy: function(ctx, cx, cy, w, h, r) {
  614. ctx.beginPath()
  615. var x = cx - w / 2
  616. var y = cy - h / 2
  617. ctx.moveTo(x + r, y)
  618. ctx.arcTo(x + w, y, x + w, y + h, r)
  619. ctx.arcTo(x + w, y + h, x, y + h, r)
  620. ctx.arcTo(x, y + h, x, y, r)
  621. ctx.arcTo(x, y, x + w, y, r)
  622. ctx.closePath()
  623. },
  624. drawPolygonLegacy: function(ctx, cx, cy, r, sides) {
  625. ctx.beginPath()
  626. var startAngle = -Math.PI / 2
  627. for (var i = 0; i < sides; i++) {
  628. var angle = startAngle + (2 * Math.PI * i) / sides
  629. var px = cx + r * Math.cos(angle)
  630. var py = cy + r * Math.sin(angle)
  631. if (i === 0) {
  632. ctx.moveTo(px, py)
  633. } else {
  634. ctx.lineTo(px, py)
  635. }
  636. }
  637. ctx.closePath()
  638. }
  639. }
  640. }
  641. </script>
  642. <style scoped>
  643. .family-graph-wrapper {
  644. position: relative;
  645. width: 100%;
  646. height: 280px;
  647. margin: 10rpx 0;
  648. background: #FFFFFF;
  649. border-radius: 24rpx;
  650. overflow: hidden;
  651. }
  652. .family-graph-canvas {
  653. position: absolute;
  654. top: 0;
  655. left: 0;
  656. z-index: 1;
  657. }
  658. .family-graph-empty {
  659. display: flex;
  660. align-items: center;
  661. justify-content: center;
  662. padding: 60rpx 0;
  663. background: #FFFFFF;
  664. border-radius: 24rpx;
  665. margin: 10rpx 0;
  666. }
  667. .empty-text {
  668. font-size: 28rpx;
  669. color: #94A3B8;
  670. }
  671. </style>