-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1639 from VisActor/feat/polygon-sector-crosshair
Feat: polygon sector crosshair
- Loading branch information
Showing
7 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
.../changes/@visactor/vrender-components/feat-polygon-sector-crosshair_2024-12-23-12-33.json
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,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@visactor/vrender-components", | ||
"comment": "feat: support polygon sector crosshair for non-smooth angle axis", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@visactor/vrender-components" | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/vrender-components/__tests__/browser/examples/crosshair-polygon-sector.ts
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,27 @@ | ||
import '@visactor/vrender'; | ||
import render from '../../util/render'; | ||
import { PolygonSectorCrosshair, SectorCrosshair } from '../../../src'; | ||
|
||
export function run() { | ||
const startAngle = 1.3 * Math.PI; | ||
const endAngle = 1.7 * Math.PI; | ||
const crosshair = new PolygonSectorCrosshair({ | ||
center: { | ||
x: 250, | ||
y: 250 | ||
}, | ||
radius: 100, | ||
innerRadius: 30, | ||
startAngle, | ||
endAngle | ||
}); | ||
|
||
const stage = render([crosshair], 'main'); | ||
|
||
stage.addEventListener('pointermove', e => { | ||
crosshair.setLocation({ | ||
x: e.viewX, | ||
y: e.viewY | ||
}); | ||
}); | ||
} |
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
61 changes: 61 additions & 0 deletions
61
packages/vrender-components/src/crosshair/polygon-sector.ts
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,61 @@ | ||
/** | ||
* @description sector 类型 crosshair,用于极坐标系下 | ||
*/ | ||
import type { IGroup } from '@visactor/vrender-core'; | ||
import { merge, getAngleByPoint, radianToDegree, polarToCartesian } from '@visactor/vutils'; | ||
import type { PointLocationCfg } from '../core/type'; | ||
import { POLAR_END_ANGLE, POLAR_START_ANGLE } from '../constant'; | ||
import { CrosshairBase } from './base'; | ||
import type { PolygonSectorCrosshairAttrs } from './type'; | ||
import type { ComponentOptions } from '../interface'; | ||
import { loadPolygonSectorCrosshairComponent } from './register'; | ||
import { getPolygonPath } from '../axis'; | ||
|
||
loadPolygonSectorCrosshairComponent(); | ||
export class PolygonSectorCrosshair extends CrosshairBase<PolygonSectorCrosshairAttrs> { | ||
static defaultAttributes = { | ||
polygonSectorStyle: { | ||
fill: '#b2bacf', | ||
opacity: 0.2 | ||
} | ||
}; | ||
|
||
constructor(attributes: PolygonSectorCrosshairAttrs, options?: ComponentOptions) { | ||
super(options?.skipDefault ? attributes : merge({}, PolygonSectorCrosshair.defaultAttributes, attributes)); | ||
} | ||
|
||
protected renderCrosshair(container: IGroup) { | ||
const { center, radius, innerRadius = 0, polygonSectorStyle } = this.attribute as PolygonSectorCrosshairAttrs; | ||
const { startAngle, endAngle } = this.attribute; | ||
const points = []; | ||
points.push(polarToCartesian(center, innerRadius, startAngle)); | ||
points.push(polarToCartesian(center, radius * Math.cos((endAngle - startAngle) / 2), startAngle)); | ||
points.push(polarToCartesian(center, radius, (startAngle + endAngle) / 2)); | ||
points.push(polarToCartesian(center, radius * Math.cos((endAngle - startAngle) / 2), endAngle)); | ||
points.push(polarToCartesian(center, innerRadius, endAngle)); | ||
|
||
const polygon = container.createOrUpdateChild( | ||
'crosshair-polygon-sector', | ||
{ | ||
path: getPolygonPath(points, true), | ||
...polygonSectorStyle | ||
}, | ||
'path' | ||
); | ||
return polygon; | ||
} | ||
|
||
setLocation(point: PointLocationCfg) { | ||
const { | ||
center, | ||
startAngle = POLAR_START_ANGLE, | ||
endAngle = POLAR_END_ANGLE | ||
} = this.attribute as PolygonSectorCrosshairAttrs; | ||
const sectorAngle = endAngle - startAngle; | ||
const pointAngle = radianToDegree(getAngleByPoint(center, point)); | ||
this.setAttributes({ | ||
startAngle: pointAngle - sectorAngle / 2, | ||
endAngle: pointAngle + sectorAngle / 2 | ||
}); | ||
} | ||
} |
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