-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(state): 添加 setState 方法,通过条件设置三种状态的激活与否 (#1460)
* feat(state): 添加 setState 方法,通过条件设置三种状态的激活与否 - [x] demo - [ ] testcases - [ ] docs * feat(state): 添加 setState & getStates 方法,通过条件设置三种状态的激活与否 - [x] demo - [ ] testcases - [ ] docs * refactor: 修改 cr 建议 * refactor: 修改 cr 建议 & 添加测试用例 * fix(pie-statistic): 修复中心文本类型定义 & 样式问题
- Loading branch information
Showing
13 changed files
with
285 additions
and
37 deletions.
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
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,82 @@ | ||
import { Pie } from '../../../../src'; | ||
import { POSITIVE_NEGATIVE_DATA } from '../../../data/common'; | ||
import { createDiv } from '../../../utils/dom'; | ||
|
||
describe('pie', () => { | ||
const data = POSITIVE_NEGATIVE_DATA.filter((o) => o.value > 0).map((d, idx) => | ||
idx === 1 ? { ...d, type: 'item1' } : d | ||
); | ||
|
||
const options = { | ||
width: 400, | ||
height: 300, | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
color: ['blue', 'red', 'yellow', 'lightgreen', 'lightblue', 'pink'], | ||
radius: 0.8, | ||
autoFit: false, | ||
}; | ||
|
||
it('set statesStyle', () => { | ||
const pie = new Pie(createDiv(), { | ||
...options, | ||
state: { | ||
selected: { | ||
style: { | ||
lineWidth: 4, | ||
fill: 'red', | ||
}, | ||
}, | ||
inactive: { | ||
style: { | ||
fill: 'blue', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
pie.render(); | ||
|
||
pie.setState('selected', (d: any) => d.type === data[0].type); | ||
const shape = pie.chart.geometries[0].elements[0].shape; | ||
|
||
expect(shape.attr('lineWidth')).toBe(4); | ||
expect(shape.attr('fill')).toBe('red'); | ||
|
||
// // 取消 selected | ||
pie.setState('selected', (d: any) => d.type === data[0].type, false); | ||
pie.setState('inactive', (d: any) => d.type === data[0].type); | ||
expect(shape.attr('fill')).toBe('blue'); | ||
expect(pie.getStates()[0].state).toBe('inactive'); | ||
}); | ||
|
||
it('set statesStyle by theme', () => { | ||
const pie = new Pie(createDiv(), { | ||
...options, | ||
theme: { | ||
geometries: { | ||
interval: { | ||
rect: { | ||
active: { | ||
style: { | ||
fill: 'yellow', | ||
fillOpacity: 0.65, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
pie.render(); | ||
|
||
pie.setState('active', (d: any) => d.type === data[2].type); | ||
const shape = pie.chart.geometries[0].elements[2].shape; | ||
|
||
expect(shape.attr('fill')).toBe('yellow'); | ||
expect(shape.attr('fillOpacity')).toBe(0.65); | ||
expect(pie.getStates()[0].state).toBe('active'); | ||
}); | ||
}); |
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
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,43 @@ | ||
import { Pie } from '@antv/g2plot'; | ||
|
||
const data = [ | ||
{ type: '分类一', value: 27 }, | ||
{ type: '分类二', value: 25 }, | ||
{ type: '分类三', value: 18 }, | ||
{ type: '分类四', value: 15 }, | ||
{ type: '分类五', value: 10 }, | ||
{ type: '其他', value: 5 }, | ||
]; | ||
|
||
const piePlot = new Pie('container', { | ||
appendPadding: 10, | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
radius: 0.8, | ||
label: { | ||
type: 'inner', | ||
content: '{name} {percentage}', | ||
style: { | ||
fill: '#fff', | ||
fontSize: 14, | ||
}, | ||
}, | ||
// 设置 状态样式 | ||
state: { | ||
active: { | ||
style: { | ||
lineWidth: 0, | ||
fillOpacity: 0.65, | ||
}, | ||
}, | ||
}, | ||
// 添加 element 选中和激活交互 | ||
interactions: [{ name: 'element-selected' }, { name: 'element-active' }], | ||
}); | ||
|
||
piePlot.render(); | ||
|
||
// 初始化设置默认状态;状态可叠加,可通过回调设置 | ||
piePlot.setState('active', (data) => (data as any).type === '分类一'); | ||
piePlot.setState('selected', (d) => (data as any).type === '分类一' || (data as any).type === '分类二'); |
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
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
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
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
Oops, something went wrong.