-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add custom symbol demo (#6388)
* docs: add custom symbol demo * docs: replace image url
- Loading branch information
1 parent
38b6711
commit 0f28c11
Showing
2 changed files
with
63 additions
and
0 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,55 @@ | ||
import { Chart, register, type SymbolFactor } from '@antv/g2'; | ||
|
||
const customSquare = Object.assign<SymbolFactor, Partial<SymbolFactor>>( | ||
(x, y, r) => { | ||
const radius = r / 2; | ||
|
||
return [ | ||
['M', x + radius, y - r], | ||
['L', x - radius, y - r], | ||
['A', radius, radius, 0, 0, 0, x - r, y - radius], | ||
['L', x - r, y + radius], | ||
['A', radius, radius, 0, 0, 0, x - radius, y + r], | ||
['L', x + radius, y + r], | ||
['A', radius, radius, 0, 0, 0, x + r, y + radius], | ||
['L', x + r, y - radius], | ||
['A', radius, radius, 0, 0, 0, x + radius, y - r], | ||
['Z'], | ||
]; | ||
}, | ||
{ | ||
// 空心请设置为 ['stroke', 'lineWidth'] | ||
style: ['fill'] | ||
}, | ||
); | ||
|
||
register('symbol.customSquare', customSquare); | ||
|
||
const chart = new Chart({ | ||
container: 'container', | ||
}); | ||
|
||
const data = [ | ||
{ genre: 'Sports', sold: 275 }, | ||
{ genre: 'Strategy', sold: 115 }, | ||
{ genre: 'Action', sold: 120 }, | ||
{ genre: 'Shooter', sold: 350 }, | ||
{ genre: 'Other', sold: 150 }, | ||
]; | ||
|
||
const colorField = 'genre'; | ||
|
||
chart | ||
.interval() | ||
.data(data) | ||
.encode('x', 'genre') | ||
.encode('y', 'sold') | ||
.encode('color', colorField) | ||
.legend({ | ||
color: { | ||
itemMarker: 'customSquare', | ||
}, | ||
}); | ||
|
||
chart.render(); | ||
|