-
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.
feat(shape): add gauge shape round (#6069)
Co-authored-by: wb-xcf804241 <wb-xcf804241@alibaba-inc.com>
- Loading branch information
1 parent
9e35333
commit e0cefbb
Showing
8 changed files
with
639 additions
and
1 deletion.
There are no files selected for viewing
506 changes: 506 additions & 0 deletions
506
__tests__/integration/snapshots/static/gaugeRoundShape.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,29 @@ | ||
import { G2Spec } from '../../../src'; | ||
|
||
export function gaugeRoundShape(): G2Spec { | ||
return { | ||
type: 'gauge', | ||
data: { | ||
value: { | ||
target: 159, | ||
total: 400, | ||
name: 'score', | ||
thresholds: [100, 200, 400], | ||
}, | ||
}, | ||
scale: { | ||
color: { | ||
range: ['#F4664A', '#FAAD14', 'green'], | ||
}, | ||
}, | ||
legend: false, | ||
style: { | ||
arcShape: 'round', | ||
arcLineWidth: 2, | ||
arcStroke: '#fff', | ||
textContent: (target, total) => { | ||
return `得分:${target}\n占比:${(target / total) * 100}%`; | ||
}, | ||
}, | ||
}; | ||
} |
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,32 @@ | ||
import { Chart } from '@antv/g2'; | ||
|
||
const chart = new Chart({ | ||
container: 'container', | ||
autoFit: true, | ||
}); | ||
|
||
chart | ||
.gauge() | ||
.data({ | ||
value: { | ||
target: 159, | ||
total: 400, | ||
name: 'score', | ||
thresholds: [100, 200, 400], | ||
}, | ||
}) | ||
.scale('color', { | ||
range: ['#F4664A', '#FAAD14', 'green'], | ||
}) | ||
.style({ | ||
arcShape: 'round', | ||
arcLineWidth: 2, | ||
arcStroke: '#fff', | ||
}) | ||
.style( | ||
'textContent', | ||
(target, total) => `得分:${target}\n占比:${(target / total) * 100}%`, | ||
) | ||
.legend(false); | ||
|
||
chart.render(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { omit } from '@antv/util'; | ||
import type { Vector2, ShapeComponent as SC } from '../../runtime'; | ||
|
||
export type RoundOptions = Record<string, any>; | ||
|
||
// Get point1 point2 radius. | ||
const getR = (point1, point2) => { | ||
return ( | ||
Math.sqrt( | ||
Math.pow(point1[0] - point2[0], 2) + Math.pow(point1[1] - point2[1], 2), | ||
) / 2 | ||
); | ||
}; | ||
|
||
// Gauge round. | ||
export const Round: SC<RoundOptions> = (options, context) => { | ||
if (!context) return; | ||
const { coordinate } = context; | ||
if (!coordinate?.getCenter) return; | ||
// Get coordinate center point. | ||
const center = coordinate.getCenter() as Vector2; | ||
|
||
return (points, cfg, defaultCfg) => { | ||
const { document } = context.canvas; | ||
const { color, index } = cfg; | ||
|
||
const g = document.createElement('g', {}); | ||
|
||
const minR = getR(points[0], points[1]); | ||
const maxR = getR(points[0], center) * 2; | ||
|
||
/** | ||
* MinR small circle radius, maxR big circle radius. | ||
* Draw four arcs. | ||
* Style lineWidth and stroke for the time being inset. | ||
*/ | ||
const roundPath = document.createElement('path', { | ||
style: { | ||
path: [ | ||
['M', ...points[0]], | ||
['A', minR, minR, 0, 1, 0, ...points[1]], | ||
['A', maxR + minR * 2, maxR + minR * 2, 0, 0, 0, ...points[2]], | ||
['A', minR, minR, 0, 1, index === 0 ? 0 : 1, ...points[3]], | ||
['A', maxR, maxR, 0, 0, 1, ...points[0]], | ||
['Z'], | ||
], | ||
...defaultCfg, | ||
...omit(options, ['shape', 'last', 'first']), | ||
fill: color || defaultCfg.color, | ||
}, | ||
}); | ||
|
||
g.appendChild(roundPath); | ||
|
||
return g; | ||
}; | ||
}; |
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