Skip to content

Commit 0e9ff68

Browse files
authored
环图 Statistics 区域优化 (#1248)
* fix: pie label do not capture event * fix: 统一环图中检区域 html 模板以及入参结构同时修复 htmlContent 不生效的问题 * feat: 环图 Statistic 支持自定义 html,同时完善自定义函数参数内容
1 parent ce28701 commit 0e9ff68

File tree

5 files changed

+50
-39
lines changed

5 files changed

+50
-39
lines changed

__tests__/bugs/donut-statistic-spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ describe('Statistics scale format', () => {
5858
angleField: 'value',
5959
colorField: 'type',
6060
animation: false,
61+
statistic: {
62+
htmlContent: (data) => {
63+
const { name, value, color } = data;
64+
return `<div>
65+
<h3 class="ring-guide-name" style="color: ${color}">${name}</h3>
66+
<p class="ring-guide-value">${value}</p>
67+
</div>`;
68+
},
69+
},
6170
});
6271
donutPlot.render();
6372
// @ts-ignore

src/plots/donut/component/ring-statistic.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import { View } from '../../../dependents';
22
import StatisticHtml, { IStatisticHtml } from './statistic';
3-
import * as statisticTemplate from './statistic-template';
4-
import { debounce, each, isString, isObject, isFunction, keys } from '@antv/util';
5-
import { LooseMap } from '../../../interface/types';
3+
import { getTemplate } from './statistic-template';
4+
import { debounce, each } from '@antv/util';
65
import Ring, { DonutViewConfig } from '../layer';
6+
import { LooseMap } from '../../../interface/types';
77

88
interface IRingStatistic extends IStatisticHtml {
99
view: View;
1010
plot: any;
1111
}
1212

13+
interface StatisticData {
14+
name: string;
15+
value: string;
16+
itemData?: LooseMap;
17+
color?: string;
18+
}
19+
1320
export default class RingStatistic extends StatisticHtml {
1421
protected view: View;
1522
protected plot: Ring;
@@ -29,7 +36,7 @@ export default class RingStatistic extends StatisticHtml {
2936
this.view.on(
3037
`interval:${triggerOnEvent}`,
3138
debounce((e) => {
32-
const displayData = this.parseStatisticData(e.data.data);
39+
const displayData = this.parseStatisticData('item', e.data.data, e.data.color);
3340
const htmlString = this.getStatisticHtmlString(displayData);
3441
this.updateHtml(htmlString);
3542
}, 150)
@@ -51,7 +58,7 @@ export default class RingStatistic extends StatisticHtml {
5158
} else {
5259
/** 用户没有指定文本内容时,默认显示总计 */
5360
const data = this.getTotalValue();
54-
displayData = this.parseStatisticData(data);
61+
displayData = this.parseStatisticData('total', data);
5562
}
5663
/** 中心文本显示 */
5764
let htmlString;
@@ -87,33 +94,29 @@ export default class RingStatistic extends StatisticHtml {
8794
return data;
8895
}
8996

90-
private parseStatisticData(data) {
97+
private parseStatisticData(type: string, data, color?: string) {
9198
const plot = this.plot;
9299
const { angleField, colorField } = plot.options;
93100
const angleScale = plot.getScaleByField(angleField);
94101
const colorScale = plot.getScaleByField(colorField);
95102

96-
if (colorField) {
97-
return {
98-
name: colorScale.getText(data[colorField]),
99-
value: angleScale.getText(data[angleField]),
100-
};
103+
const statisticData: StatisticData = {
104+
name: colorScale ? colorScale.getText(data[colorField]) : null,
105+
value: angleScale.getText(data[angleField]),
106+
};
107+
108+
if (type === 'item') {
109+
// 每一个扇形区域的数据
110+
statisticData.itemData = data;
111+
statisticData.color = color;
101112
}
102113

103-
return angleScale.getText(data[angleField]);
114+
return statisticData;
104115
}
105116

106117
private getStatisticTemplate(data) {
107118
const size = this.getStatisticSize();
108-
let htmlString;
109-
/** 如果文本内容为string或单条数据 */
110-
if (isString(data)) {
111-
htmlString = statisticTemplate.getSingleDataTemplate(data, this.statisticClass, size);
112-
} else if (isObject(data) && keys(data).length === 2) {
113-
/** 如果文本内容为两条数据 */
114-
const content = data as LooseMap;
115-
htmlString = statisticTemplate.getTwoDataTemplate(content.name, content.value, this.statisticClass, size);
116-
}
119+
const htmlString = getTemplate(data.name, data.value, this.statisticClass, size);
117120
/** 更为复杂的文本要求用户自行制定html模板 */
118121
return htmlString;
119122
}
@@ -127,15 +130,14 @@ export default class RingStatistic extends StatisticHtml {
127130
}
128131

129132
private getStatisticHtmlString(data): string {
130-
const triggerOnConfig = this.options.triggerOn;
133+
const htmlContent = this.options.htmlContent;
131134
let htmlString: string;
132-
if (isString(triggerOnConfig)) {
135+
if (htmlContent) {
136+
htmlString = htmlContent(data);
137+
} else {
133138
htmlString = this.getStatisticTemplate(data);
134139
}
135-
if (isFunction(triggerOnConfig)) {
136-
htmlString = triggerOnConfig(data);
137-
htmlString = `<div class="ring-guide-html ${this.statisticClass}">${htmlString}</div>`;
138-
}
140+
139141
return htmlString;
140142
}
141143
}
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
/*tslint:disable*/
22
const containerStyle =
33
"color:#4d4d4d;font-size:14px;text-align:center;line-height:2;font-family:'-apple-system',BlinkMacSystemFont,'SegoeUI',Roboto,'HelveticaNeue',Helvetica,'PingFangSC','HiraginoSansGB','MicrosoftYaHei',SimSun,'sans-serif';pointer-events:none;";
4-
54
const nameStyle = 'font-weight:300;white-space: nowrap;text-overflow: ellipsis;';
6-
75
const valueStyle = 'font-size:32px;font-weight:bold;color:#4D4D4D';
86

9-
function getSingleDataTemplate(value, classId, size) {
7+
export function getTemplate(name, value, classId, size) {
108
const domStyle = `${containerStyle}width:${size}px;`;
11-
return `<div class="ring-guide-html ${classId}" style=${domStyle}><span class="ring-guide-value" style=${valueStyle}>${value}</span></div>`;
12-
}
9+
const nameDomStr = name ? `<span class="ring-guide-name" style=${nameStyle}>${name}</span><br/>` : '';
10+
const valueDomStr = `<span class="ring-guide-value" style=${valueStyle}>${value}</span>`;
1311

14-
function getTwoDataTemplate(name, value, classId, size) {
15-
const domStyle = `${containerStyle}width:${size}px;`;
16-
return `<div class="ring-guide-html ${classId}" style=${domStyle}><span class="ring-guide-name" style=${nameStyle}>${name}</span><br/><span class="ring-guide-value" style=${valueStyle}>${value}</span></div>`;
12+
return `<div class="ring-guide-html ${classId}" style=${domStyle}>${nameDomStr}${valueDomStr}</div>`;
1713
}
18-
19-
export { getSingleDataTemplate, getTwoDataTemplate };

src/plots/pie/component/label/base-label.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ export default abstract class PieBaseLabel {
174174
idx
175175
)
176176
: shapeInfo.name;
177-
const itemGroup = this.container.addGroup({ name: 'itemGroup', index: idx });
177+
const itemGroup = this.container.addGroup({
178+
name: 'itemGroup',
179+
index: idx,
180+
capture: false, // 不捕获事件,否则鼠标 hover 到图形会失焦
181+
});
178182
const textShape = itemGroup.addShape('text', {
179183
attrs: deepMix({}, attrs, {
180184
x: shapeInfo.x + offsetX,

src/plots/pie/component/label/spider-label.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ export default class SpiderLabel {
141141
}
142142
texts = formatted;
143143
}
144-
const textGroup = this.container.addGroup();
144+
const textGroup = this.container.addGroup({
145+
capture: false, // 不捕获事件,否则鼠标 hover 到图形会失焦
146+
});
145147
const textAttrs: IAttrs = {
146148
x: 0,
147149
y: 0,

0 commit comments

Comments
 (0)