优化鱼骨图布局

This commit is contained in:
wanglin2 2023-04-15 09:51:12 +08:00
parent 16fb8eb092
commit ab931901e2
2 changed files with 21 additions and 21 deletions

View File

@ -8,6 +8,8 @@ class Fishbone extends Base {
// 构造函数 // 构造函数
constructor(opt = {}) { constructor(opt = {}) {
super(opt) super(opt)
this.indent = 0.3
this.childIndent = 0.5
} }
// 布局 // 布局
@ -239,21 +241,21 @@ class Fishbone extends Base {
maxx = item.left maxx = item.left
} }
// 水平线段到二级节点的连线 // 水平线段到二级节点的连线
let nodeLineX = item.left + item.width * 0.3 let nodeLineX = item.left
let offset = item.height + node.height / 2 let offset = node.height / 2
let offsetX = offset / Math.tan(degToRad(this.mindMap.opt.fishboneDeg)) let offsetX = offset / Math.tan(degToRad(this.mindMap.opt.fishboneDeg))
let line = this.draw.path() let line = this.draw.path()
if (this.checkIsTop(item)) { if (this.checkIsTop(item)) {
line.plot( line.plot(
`M ${nodeLineX - offsetX},${item.top + offset} L ${nodeLineX},${ `M ${nodeLineX - offsetX},${item.top + item.height + offset} L ${item.left},${
item.top item.top + item.height
}` }`
) )
} else { } else {
line.plot( line.plot(
`M ${nodeLineX - offsetX},${ `M ${nodeLineX - offsetX},${
item.top + item.height - offset item.top - offset
} L ${nodeLineX},${item.top + item.height}` } L ${nodeLineX},${item.top}`
) )
} }
node.style.line(line) node.style.line(line)
@ -277,7 +279,7 @@ class Fishbone extends Base {
let maxy = -Infinity let maxy = -Infinity
let miny = Infinity let miny = Infinity
let maxx = -Infinity let maxx = -Infinity
let x = node.left + node.width * 0.3 let x = node.left + node.width * this.indent
node.children.forEach((item, index) => { node.children.forEach((item, index) => {
if (item.left > maxx) { if (item.left > maxx) {
maxx = item.left maxx = item.left
@ -300,7 +302,7 @@ class Fishbone extends Base {
if (len >= 0) { if (len >= 0) {
let line = this.draw.path() let line = this.draw.path()
expandBtnSize = len > 0 ? expandBtnSize : 0 expandBtnSize = len > 0 ? expandBtnSize : 0
let lineLength = maxx - node.left - node.width * 0.3 let lineLength = maxx - node.left - node.width * this.indent
lineLength = Math.max(lineLength, 0) lineLength = Math.max(lineLength, 0)
let params = { let params = {
node, node,

View File

@ -47,7 +47,7 @@ export default {
computedLeftTopValue({ layerIndex, node, ctx }) { computedLeftTopValue({ layerIndex, node, ctx }) {
if (layerIndex >= 1 && node.children) { if (layerIndex >= 1 && node.children) {
// 遍历三级及以下节点的子节点 // 遍历三级及以下节点的子节点
let startLeft = node.left + node.width * 0.5 let startLeft = node.left + node.width * ctx.childIndent
let totalTop = let totalTop =
node.top + node.top +
node.height + node.height +
@ -80,22 +80,21 @@ export default {
// 将二级节点的子节点移到上方 // 将二级节点的子节点移到上方
if (parent && parent.isRoot) { if (parent && parent.isRoot) {
// 遍历二级节点的子节点 // 遍历二级节点的子节点
let totalHeight = 0 let totalHeight = node.expandBtnSize
node.children.forEach(item => { node.children.forEach(item => {
// 调整top // 调整top
let nodeTotalHeight = ctx.getNodeAreaHeight(item) let nodeTotalHeight = ctx.getNodeAreaHeight(item)
let _top = item.top let _top = item.top
let _left = item.left
item.top = item.top =
node.top - (item.top - node.top) - nodeTotalHeight + node.height node.top - (item.top - node.top) - nodeTotalHeight + node.height
// 调整left // 调整left
let offsetLeft = item.left = node.left + node.width * ctx.indent + (nodeTotalHeight + totalHeight) / Math.tan(degToRad(ctx.mindMap.opt.fishboneDeg))
(nodeTotalHeight + totalHeight) / Math.tan(degToRad(ctx.mindMap.opt.fishboneDeg))
item.left += offsetLeft
totalHeight += nodeTotalHeight totalHeight += nodeTotalHeight
// 同步更新后代节点 // 同步更新后代节点
ctx.updateChildrenPro(item.children, { ctx.updateChildrenPro(item.children, {
top: item.top - _top, top: item.top - _top,
left: offsetLeft left: item.left - _left
}) })
}) })
} }
@ -137,7 +136,7 @@ export default {
computedLeftTopValue({ layerIndex, node, ctx }) { computedLeftTopValue({ layerIndex, node, ctx }) {
if (layerIndex === 1 && node.children) { if (layerIndex === 1 && node.children) {
// 遍历二级节点的子节点 // 遍历二级节点的子节点
let startLeft = node.left + node.width * 0.5 let startLeft = node.left + node.width * ctx.childIndent
let totalTop = let totalTop =
node.top + node.top +
node.height + node.height +
@ -155,7 +154,7 @@ export default {
} }
if (layerIndex > 1 && node.children) { if (layerIndex > 1 && node.children) {
// 遍历三级及以下节点的子节点 // 遍历三级及以下节点的子节点
let startLeft = node.left + node.width * 0.5 let startLeft = node.left + node.width * ctx.childIndent
let totalTop = let totalTop =
node.top - node.top -
(ctx.getNodeActChildrenLength(node) > 0 ? node.expandBtnSize : 0) (ctx.getNodeActChildrenLength(node) > 0 ? node.expandBtnSize : 0)
@ -187,7 +186,7 @@ export default {
if (parent && parent.isRoot) { if (parent && parent.isRoot) {
// 遍历二级节点的子节点 // 遍历二级节点的子节点
let totalHeight = 0 let totalHeight = 0
let totalHeight2 = 0 let totalHeight2 = node.expandBtnSize
node.children.forEach(item => { node.children.forEach(item => {
// 调整top // 调整top
let hasChildren = ctx.getNodeActChildrenLength(item) > 0 let hasChildren = ctx.getNodeActChildrenLength(item) > 0
@ -199,17 +198,16 @@ export default {
(hasChildren ? item.expandBtnSize : 0) (hasChildren ? item.expandBtnSize : 0)
: 0 : 0
let _top = totalHeight + offset let _top = totalHeight + offset
let _left = item.left
item.top += _top item.top += _top
// 调整left // 调整left
let offsetLeft = item.left = node.left + node.width * ctx.indent + (nodeTotalHeight + totalHeight2) / Math.tan(degToRad(ctx.mindMap.opt.fishboneDeg))
(totalHeight2 + nodeTotalHeight) / Math.tan(degToRad(ctx.mindMap.opt.fishboneDeg))
item.left += offsetLeft
totalHeight += offset totalHeight += offset
totalHeight2 += nodeTotalHeight totalHeight2 += nodeTotalHeight
// 同步更新后代节点 // 同步更新后代节点
ctx.updateChildrenPro(item.children, { ctx.updateChildrenPro(item.children, {
top: _top, top: _top,
left: offsetLeft left: item.left - _left
}) })
}) })
} }