-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: 新增透视表 - 树状模式下的连接线示例 #3074
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
s2-site/examples/custom/custom-shape-and-chart/demo/pivot-tree-line.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { Polyline } from '@antv/g'; | ||
import { PivotSheet, S2Options, SpreadSheet, Node, S2Event } from '@antv/s2'; | ||
import { get } from 'lodash'; | ||
|
||
// 存储已绘制的连接线,用于销毁 | ||
const dottedTreeLines: Polyline[] = []; | ||
/** | ||
* 销毁已存在的虚线 | ||
*/ | ||
function destroyDottedLines(lines: Polyline[] = []) { | ||
while (lines.length) { | ||
lines?.pop()?.destroy(); | ||
} | ||
} | ||
|
||
/** | ||
* 获取列头的高度 | ||
*/ | ||
function getColHeaderHeight(s2: SpreadSheet) { | ||
return s2.facet.getColNodes(0)?.[0]?.hierarchy?.height; | ||
} | ||
|
||
/** | ||
* 获取滚动高度 | ||
*/ | ||
function getOffsetHeight(s2: SpreadSheet) { | ||
return s2.facet.getScrollOffset().scrollY; | ||
} | ||
/** | ||
* 获取左侧X轴滚动距离 | ||
*/ | ||
function getHeaderOffsetWidth(s2: SpreadSheet) { | ||
return s2.facet.getScrollOffset().rowHeaderScrollX; | ||
} | ||
|
||
/** | ||
* 获取树级行头嵌套深度 | ||
*/ | ||
function getRowHeaderTreeDepth(s2: SpreadSheet) { | ||
return s2.facet.getRowNodes(0)?.[0]?.hierarchy?.maxLevel; | ||
} | ||
|
||
/** | ||
* 获取视口高度(列头 + 数值区域) | ||
*/ | ||
function getViewportHeight(s2: SpreadSheet) { | ||
return s2.facet.panelBBox.viewportHeight + getColHeaderHeight(s2); | ||
} | ||
|
||
/** | ||
* 获取 tree icon 的信息 | ||
*/ | ||
function getTreeIconCfg(node: Node) { | ||
if ( | ||
node && | ||
get(node, 'belongsCell.treeIcon.cfg') && | ||
!get(node, 'belongsCell.treeIcon.cfg.destroyed') | ||
) { | ||
return get(node, 'belongsCell.treeIcon.cfg'); | ||
} | ||
|
||
return { | ||
'x': 8 + node.level * 14, | ||
'y': node.y + node.height / 2 - 10 / 2, | ||
'width': 10, | ||
'height': 10 | ||
}; | ||
} | ||
|
||
/** | ||
* 画线 | ||
*/ | ||
export function drawDottedLines(s2: SpreadSheet) { | ||
|
||
destroyDottedLines(dottedTreeLines); | ||
const canvas = s2.container; | ||
const colHeaderHeight = getColHeaderHeight(s2); | ||
const viewportHeight = getViewportHeight(s2); | ||
const offsetHeight = getOffsetHeight(s2); | ||
const headerOffsetWidth = getHeaderOffsetWidth(s2); | ||
const rowHeaderTreeDepth = getRowHeaderTreeDepth(s2); | ||
|
||
for (let i = 0; i <= rowHeaderTreeDepth; i++) { | ||
// 获取当前层级的节点 | ||
const rowNodes = s2.facet.getRowNodes(i); | ||
|
||
rowNodes.forEach((rowNode) => { | ||
if (rowNode.children.length) { | ||
// 避免重复画线导致的颜色过深 | ||
const childs: Node[] = []; | ||
|
||
rowNode.children.forEach((child) => { | ||
const rowNodeTreeIconCfg = getTreeIconCfg(rowNode); | ||
const childTreeIconCfg = getTreeIconCfg(child); | ||
|
||
if (rowNodeTreeIconCfg && childTreeIconCfg) { | ||
const x1 = | ||
rowNode.x + rowNodeTreeIconCfg.x + rowNodeTreeIconCfg.width / 2 - headerOffsetWidth; | ||
let y1 = | ||
colHeaderHeight + | ||
rowNodeTreeIconCfg.y + | ||
rowNodeTreeIconCfg.height; | ||
|
||
if (childs?.length > 0) { | ||
const preChild = childs?.pop(); | ||
if (preChild){ | ||
const preChildTreeIconCfg = getTreeIconCfg(preChild); | ||
y1 = | ||
colHeaderHeight + | ||
preChildTreeIconCfg.y + | ||
preChildTreeIconCfg.height / 2; | ||
} | ||
} | ||
|
||
childs.push(child); | ||
const x2 = child.x + childTreeIconCfg.x - headerOffsetWidth; | ||
const y2 = | ||
colHeaderHeight + | ||
childTreeIconCfg.y + | ||
childTreeIconCfg.height / 2; | ||
const points = [ | ||
[ | ||
x1, | ||
Math.min( | ||
Math.max(y1 - offsetHeight, colHeaderHeight), | ||
viewportHeight | ||
) | ||
], | ||
[ | ||
x1, | ||
Math.min( | ||
Math.max(y2 - offsetHeight, colHeaderHeight), | ||
viewportHeight | ||
) | ||
] | ||
]; | ||
|
||
if ( | ||
y2 - offsetHeight >= colHeaderHeight && | ||
y2 - offsetHeight <= viewportHeight | ||
) { | ||
points.push([x2, y2 - offsetHeight]); | ||
} | ||
|
||
const dottedLine = canvas.appendChild( | ||
new Polyline({ | ||
'style': { | ||
'points': points as any, | ||
'stroke': '#000', | ||
'lineDash': [2, 3], | ||
'lineWidth': 1, | ||
'lineJoin': 'round', | ||
'zIndex': 9999 | ||
} | ||
}) | ||
); | ||
|
||
dottedTreeLines.push(dottedLine); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
||
|
||
fetch( | ||
'https://gw.alipayobjects.com/os/bmw-prod/2a5dbbc8-d0a7-4d02-b7c9-34f6ca63cff6.json', | ||
) | ||
.then((res) => res.json()) | ||
.then(async (dataCfg) => { | ||
const container = document.getElementById('container'); | ||
|
||
const s2Options: S2Options = { | ||
width: 600, | ||
height: 480, | ||
hierarchyType: 'tree', | ||
}; | ||
|
||
const s2 = new PivotSheet(container, dataCfg, s2Options); | ||
|
||
s2.on(S2Event.LAYOUT_AFTER_RENDER, () => { | ||
drawDottedLines(s2); | ||
}); | ||
s2.on(S2Event.GLOBAL_SCROLL, () => { | ||
drawDottedLines(s2); | ||
}); | ||
s2.on(S2Event.LAYOUT_DESTROY, () => { | ||
destroyDottedLines(dottedTreeLines); | ||
}); | ||
|
||
await s2.render(); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
fetch
here without error handling could lead to unhandled promise rejections if the network request fails. Consider adding a.catch()
block to handle potential errors gracefully.