-
${title}
- ` +
- (hasSubGroup
- ? `
- ${xAxisExt[0].name}:${subGroup}
-
` : ``) +
- `
-
- ${key1}:${v1}
-
-
- ${key2}:${v2}
-
-
-
- `
+ })
+ return items
}
}
}
@@ -1160,3 +1163,75 @@ export function getMeta(chart) {
}
return meta
}
+export function getTooltipContainer(id) {
+ const curDom = document.getElementById(id)
+ if (curDom) {
+ curDom.remove()
+ }
+ const g2Tooltip = document.createElement('div')
+ g2Tooltip.setAttribute('id', id)
+ g2Tooltip.classList.add('g2-tooltip')
+
+ const g2TooltipTitle = document.createElement('div')
+ g2TooltipTitle.classList.add('g2-tooltip-title')
+ g2Tooltip.appendChild(g2TooltipTitle)
+
+ const g2TooltipList = document.createElement('ul')
+ g2TooltipList.classList.add('g2-tooltip-list')
+ g2Tooltip.appendChild(g2TooltipList)
+ const full = document.getElementsByClassName('fullscreen')
+ if (full.length) {
+ full.item(0).appendChild(g2Tooltip)
+ } else {
+ document.body.appendChild(g2Tooltip)
+ }
+ return g2Tooltip
+}
+export function configPlotTooltipEvent(chart, plot) {
+ const customAttr = JSON.parse(chart.customAttr)
+ const t = JSON.parse(JSON.stringify(customAttr.tooltip))
+ if (!t.show) {
+ return
+ }
+ // 手动处理 tooltip 的显示和隐藏事件,需配合源码理解
+ // https://github.com/antvis/G2/blob/master/src/chart/controller/tooltip.ts#showTooltip
+ plot.on('tooltip:show', () => {
+ const tooltipCtl = plot.chart.getController('tooltip')
+ if (!tooltipCtl) {
+ return
+ }
+ if (tooltipCtl.tooltip) {
+ // 处理视图放大后再关闭 tooltip 的 dom 被清除
+ const container = tooltipCtl.tooltip.cfg.container
+ const dom = document.getElementById(container.id)
+ if (!dom) {
+ const full = document.getElementsByClassName('fullscreen')
+ if (full.length) {
+ full.item(0).appendChild(container)
+ } else {
+ document.body.appendChild(container)
+ }
+ }
+ }
+ const event = plot.chart.interactions.tooltip?.context?.event
+ plot.chart.getOptions().tooltip.follow = false
+ tooltipCtl.title = Math.random().toString()
+ plot.chart.getTheme().components.tooltip.x = event.clientX
+ plot.chart.getTheme().components.tooltip.y = event.clientY
+ })
+ // https://github.com/antvis/G2/blob/master/src/chart/controller/tooltip.ts#hideTooltip
+ plot.on('plot:mouseleave', () => {
+ const tooltipCtl = plot.chart.getController('tooltip')
+ if (!tooltipCtl) {
+ return
+ }
+ plot.chart.getOptions().tooltip.follow = true
+ tooltipCtl.hideTooltip()
+ })
+}
+
+export const TOOLTIP_TPL = '