-
Notifications
You must be signed in to change notification settings - Fork 213
feat: support resize zoom chart plugin #4241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
common/changes/@visactor/vchart/feat-support-resize-zoom_2025-10-28-06-39.json
This file contains hidden or 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,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "comment": "feat: support resize zoom chart plugin\n\n", | ||
| "type": "none", | ||
| "packageName": "@visactor/vchart" | ||
| } | ||
| ], | ||
| "packageName": "@visactor/vchart", | ||
| "email": "lixuef1313@163.com" | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| export * from './zoom'; |
This file contains hidden or 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,102 @@ | ||
| import type { IChartPlugin, IChartPluginService } from '../interface'; | ||
| import { BasePlugin } from '../../base/base-plugin'; | ||
| import { registerChartPlugin } from '../register'; | ||
| import type { IPoint } from '../../../typings'; | ||
| import { getDefaultTriggerEventByMode } from '../../../component/common/trigger/config'; | ||
|
|
||
| const MIN_ZOOM = 0.1; | ||
| const MAX_ZOOM = 10; | ||
|
|
||
| export class ChartResizeZoomPlugin extends BasePlugin<IChartPluginService> implements IChartPlugin { | ||
| static readonly pluginType: 'chart'; | ||
| static readonly specKey = 'resizeZoom'; | ||
| static readonly type: string = 'ChartResizeZoomPlugin'; | ||
| readonly type: string = 'ChartResizeZoomPlugin'; | ||
|
|
||
| protected _container?: HTMLElement; | ||
| protected _triggerEvent?: string; | ||
| protected _minZoom?: number; | ||
| protected _maxZoom?: number; | ||
| protected _zoom: number = 1; | ||
| protected _rate?: number; | ||
|
|
||
| constructor() { | ||
| super(ChartResizeZoomPlugin.type); | ||
| } | ||
|
|
||
| onAfterInitChart(service: IChartPluginService, chartSpec: any) { | ||
| const chart = service.globalInstance; | ||
| const spec = chart.getSpec()?.[ChartResizeZoomPlugin.specKey] ?? { enabled: false }; | ||
| if (spec.enabled !== true) { | ||
| return; | ||
| } | ||
| this._minZoom = spec.min ?? MIN_ZOOM; | ||
| this._maxZoom = spec.max ?? MAX_ZOOM; | ||
| this._rate = spec.rate ?? 0.1; | ||
| this._container = chart.getContainer(); | ||
| this._triggerEvent = getDefaultTriggerEventByMode(service.globalInstance.getChart().getOption().mode).zoom; | ||
| if (this._container && this._triggerEvent) { | ||
| this._container.addEventListener(this._triggerEvent as keyof HTMLElementEventMap, this._onWheel as EventListener); | ||
| } | ||
| } | ||
|
|
||
| protected _onWheel = (e: WheelEvent) => { | ||
| e.preventDefault(); | ||
| e.stopImmediatePropagation(); | ||
|
|
||
| const zoom = Math.pow(1.005, -e.deltaY * Math.pow(16, e.deltaMode) * 0.2 * this._rate); | ||
| const center = { x: e.offsetX, y: e.offsetY }; | ||
| this.zoom(zoom, center); | ||
| }; | ||
|
|
||
| /** | ||
| * 缩放图表 | ||
| * @param zoom 缩放比例 | ||
| * @param pointerPos 缩放中心,即鼠标位置 | ||
| */ | ||
| zoom(zoom: number, pointerPos?: IPoint) { | ||
| const vchart = this.service.globalInstance; | ||
| if (!vchart) { | ||
| return; | ||
| } | ||
| const oldZoom = this._zoom; | ||
| let tempZoom = this._zoom * zoom; | ||
| if ((tempZoom <= this._minZoom && zoom < 1) || (tempZoom >= this._maxZoom && zoom > 1)) { | ||
| if (tempZoom <= this._minZoom && this._zoom > this._minZoom) { | ||
| tempZoom = this._minZoom; | ||
| } else if (tempZoom >= this._maxZoom && this._zoom < this._maxZoom) { | ||
| tempZoom = this._maxZoom; | ||
| } else { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (tempZoom === oldZoom) { | ||
| return; | ||
| } | ||
|
|
||
| const actualZoomRatio = tempZoom / oldZoom; | ||
| this._zoom = tempZoom; | ||
|
|
||
| vchart.resize(vchart.getCurrentSize().width * this._zoom, vchart.getCurrentSize().height * this._zoom); | ||
| // 滚动容器滚动, 保持当前鼠标位置在缩放后不变 | ||
| if (pointerPos && this._container) { | ||
| const { scrollLeft, scrollTop } = this._container; | ||
| this._container.scrollLeft = scrollLeft + pointerPos.x * (actualZoomRatio - 1); | ||
| this._container.scrollTop = scrollTop + pointerPos.y * (actualZoomRatio - 1); | ||
| } | ||
| } | ||
|
|
||
| release(): void { | ||
| if (this._container && this._triggerEvent) { | ||
| this._container.removeEventListener( | ||
| this._triggerEvent as keyof HTMLElementEventMap, | ||
| this._onWheel as EventListener | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const registerChartResizeZoomPlugin = () => { | ||
| registerChartPlugin(ChartResizeZoomPlugin); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.