Skip to content

Commit a8758fa

Browse files
authored
feat(sunburst): 丰富旭日图交互, 点击中心可以上卷 (#2903)
1 parent b3920c9 commit a8758fa

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/interactions/actions/drill-down.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Action, IGroup, Util } from '@antv/g2';
2-
import { get, last, isNil } from '@antv/util';
2+
import { get, last, isNil, size } from '@antv/util';
33
import { Data } from '../../types';
44
import { DrillDownCfg } from '../../types/drill-down';
55
import { deepAssign } from '../../utils/deep-assign';
@@ -112,12 +112,21 @@ export class DrillDownAction extends Action {
112112
breadCrumbGroup.setMatrix(matrix);
113113
}
114114

115+
/**
116+
* 返回上一层
117+
*/
118+
public back(): void {
119+
if (size(this.historyCache)) {
120+
this.backTo(this.historyCache.slice(0, -1));
121+
}
122+
}
123+
115124
/**
116125
* 重置
117126
*/
118127
public reset(): void {
119128
if (this.historyCache[0]) {
120-
this.back(this.historyCache.slice(0, 1));
129+
this.backTo(this.historyCache.slice(0, 1));
121130
}
122131
// 清空
123132
this.historyCache = [];
@@ -158,7 +167,7 @@ export class DrillDownAction extends Action {
158167
* 回退事件,点击面包屑时触发
159168
* @param historyCache 当前要回退到的历史
160169
*/
161-
protected back(historyCache: HistoryCache) {
170+
protected backTo(historyCache: HistoryCache) {
162171
if (!historyCache || historyCache.length <= 0) {
163172
return;
164173
}
@@ -236,7 +245,7 @@ export class DrillDownAction extends Action {
236245
const targetId = event.target.get('id');
237246
if (targetId !== last(cache)?.id) {
238247
const newHistoryCache = cache.slice(0, cache.findIndex((d) => d.id === targetId) + 1);
239-
this.back(newHistoryCache);
248+
this.backTo(newHistoryCache);
240249
}
241250
});
242251
// active 效果内置

src/interactions/drill-down.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,29 @@ export function isParentNode(context) {
1010
return isArray(data.children) && data.children.length > 0;
1111
}
1212

13-
registerAction('drill-down-action', DrillDownAction);
13+
/**
14+
* 判断是否在中心
15+
*/
16+
function inCenter(context) {
17+
const coordinate = context.view.getCoordinate();
18+
const { innerRadius } = coordinate;
19+
if (innerRadius) {
20+
const { x, y } = context.event;
21+
const { x: centerX, y: centerY } = coordinate.center;
22+
const r = coordinate.getRadius() * innerRadius;
23+
const distance = Math.sqrt((centerX - x) ** 2 + (centerY - y) ** 2);
24+
return distance < r;
25+
}
26+
return false;
27+
}
1428

29+
registerAction('drill-down-action', DrillDownAction);
1530
registerInteraction('drill-down', {
1631
showEnable: [
1732
{ trigger: 'element:mouseenter', action: 'cursor:pointer', isEnable: isParentNode },
1833
{ trigger: 'element:mouseleave', action: 'cursor:default' },
34+
// 中心处,肯定会触发 element:mouseleave 操作
35+
{ trigger: 'element:mouseleave', action: 'cursor:pointer', isEnable: inCenter },
1936
],
2037
start: [
2138
{
@@ -27,5 +44,11 @@ registerInteraction('drill-down', {
2744
trigger: 'afterchangesize',
2845
action: ['drill-down-action:resetPosition'],
2946
},
47+
{
48+
// 点击中心,返回上一层
49+
trigger: 'click',
50+
isEnable: inCenter,
51+
action: ['drill-down-action:back'],
52+
},
3053
],
3154
});

0 commit comments

Comments
 (0)