-
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(v2/pie-label): add pie options (#1296)
Co-authored-by: kasmine<736929286@qq.com>
- Loading branch information
Showing
3 changed files
with
122 additions
and
4 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,98 @@ | ||
import { Pie } from '../../../../src'; | ||
import { POSITIVE_NEGATIVE_DATA } from '../../../data/common'; | ||
import { createDiv } from '../../../utils/dom'; | ||
import { IGroup } from '@antv/g2/lib/dependents'; | ||
|
||
describe('pie label', () => { | ||
const data = POSITIVE_NEGATIVE_DATA.filter((o) => o.value > 0).map((d, idx) => | ||
idx === 1 ? { ...d, type: 'item1' } : d | ||
); | ||
|
||
const config = { | ||
width: 400, | ||
height: 300, | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
radius: 0.8, | ||
label: {}, | ||
}; | ||
|
||
it('label: visible', () => { | ||
const pie = new Pie(createDiv(), config); | ||
|
||
pie.render(); | ||
|
||
const geometry = pie.chart.geometries[0]; | ||
const elements = geometry.elements; | ||
const labelGroups = geometry.labelsContainer.getChildren(); | ||
|
||
expect(elements.length).toBe(data.length); | ||
expect(labelGroups.length).toBe(data.length); | ||
}); | ||
|
||
it.skip('label: single color(todo-hustcc: 没有 color 字段,label 显示错误)', () => { | ||
const pie = new Pie(createDiv(), { | ||
...config, | ||
colorField: null, | ||
}); | ||
|
||
pie.render(); | ||
|
||
const geometry = pie.chart.geometries[0]; | ||
const labelGroups = geometry.labelsContainer.getChildren(); | ||
|
||
expect(labelGroups.length).toBe(data.length); | ||
}); | ||
|
||
it('label: custom content', () => { | ||
const pie = new Pie(createDiv(), { | ||
...config, | ||
label: { | ||
content: (data, item, idx) => { | ||
if (idx === 0 || idx === 1) { | ||
return 'hello'; | ||
} | ||
const { type, value } = data; | ||
return `${type}: ${value}`; | ||
}, | ||
}, | ||
}); | ||
|
||
pie.render(); | ||
|
||
const geometry = pie.chart.geometries[0]; | ||
const labelGroups = geometry.labelsContainer.getChildren(); | ||
expect(labelGroups.length).toBe(data.length); | ||
const label1 = (labelGroups[0] as IGroup).getChildren(); | ||
expect(label1[0].get('type')).toBe('text'); | ||
expect(label1[1].get('type')).toBe('path'); | ||
expect(label1[0].attr('text')).toBe('hello'); | ||
const label2 = (labelGroups[1] as IGroup).getChildren(); | ||
expect(label2[0].attr('text')).toBe('hello'); | ||
}); | ||
|
||
it('label: custom callback', () => { | ||
const pie = new Pie(createDiv(), { | ||
...config, | ||
label: { | ||
callback: (value, type) => { | ||
return { | ||
content: `${type}: ${value}`, | ||
}; | ||
}, | ||
}, | ||
}); | ||
|
||
pie.render(); | ||
|
||
const geometry = pie.chart.geometries[0]; | ||
const labelGroups = geometry.labelsContainer.getChildren(); | ||
|
||
expect(labelGroups.length).toBe(data.length); | ||
const label1 = (labelGroups[0] as IGroup).getChildren(); | ||
const label3 = (labelGroups[2] as IGroup).getChildren(); | ||
expect(label1[0].attr('text')).toBe(`${data[0].type}: ${data[0].value}`); | ||
expect(label3[0].attr('text')).toBe(`${data[2].type}: ${data[2].value}`); | ||
}); | ||
}); |
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