Skip to content
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

feat(View): View center supports string #16904

Merged
merged 7 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/action/roamHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import type Geo from '../coord/geo/Geo';
import type View from '../coord/View';
import type ExtensionAPI from '../core/ExtensionAPI';
import type { Payload } from '../util/types';

export interface RoamPaylod extends Payload {
Expand All @@ -42,7 +43,8 @@ export function updateCenterAndZoom(
zoomLimit?: {
min?: number,
max?: number
}
},
api?: ExtensionAPI
) {
const previousZoom = view.getZoom();
const center = view.getCenter();
Expand All @@ -56,7 +58,7 @@ export function updateCenterAndZoom(
point[0] -= payload.dx;
point[1] -= payload.dy;

view.setCenter(getCenterCoord(view, point));
view.setCenter(getCenterCoord(view, point), api);
}
if (zoom != null) {
if (zoomLimit) {
Expand All @@ -79,7 +81,7 @@ export function updateCenterAndZoom(

view.updateTransform();
// Get the new center
view.setCenter(getCenterCoord(view, point));
view.setCenter(getCenterCoord(view, point), api);
view.setZoom(zoom * previousZoom);
}

Expand Down
2 changes: 1 addition & 1 deletion src/chart/graph/createView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default function createViewCoordSys(ecModel: GlobalModel, api: ExtensionA
);

// Update roam info
viewCoordSys.setCenter(seriesModel.get('center'));
viewCoordSys.setCenter(seriesModel.get('center'), api);
viewCoordSys.setZoom(seriesModel.get('zoom'));

viewList.push(viewCoordSys);
Expand Down
5 changes: 3 additions & 2 deletions src/chart/graph/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import GraphSeriesModel from './GraphSeries';
import { RoamPaylod, updateCenterAndZoom } from '../../action/roamHelper';
import GlobalModel from '../../model/Global';
import { noop } from 'zrender/src/core/util';
import type ExtensionAPI from '../../core/ExtensionAPI';

const actionInfo = {
type: 'graphRoam',
Expand Down Expand Up @@ -72,13 +73,13 @@ export function install(registers: EChartsExtensionInstallRegisters) {
}, noop);

// Register roam action.
registers.registerAction(actionInfo, function (payload: RoamPaylod, ecModel: GlobalModel) {
registers.registerAction(actionInfo, function (payload: RoamPaylod, ecModel: GlobalModel, api: ExtensionAPI) {
ecModel.eachComponent({
mainType: 'series', query: payload
}, function (seriesModel: GraphSeriesModel) {
const coordSys = seriesModel.coordinateSystem as View;

const res = updateCenterAndZoom(coordSys, payload);
const res = updateCenterAndZoom(coordSys, payload, undefined, api);

seriesModel.setCenter
&& seriesModel.setCenter(res.center);
Expand Down
6 changes: 3 additions & 3 deletions src/chart/tree/TreeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class TreeView extends ChartView {
group.y = layoutInfo.y;
}

this._updateViewCoordSys(seriesModel);
this._updateViewCoordSys(seriesModel, api);
this._updateController(seriesModel, ecModel, api);

const oldData = this._data;
Expand Down Expand Up @@ -224,7 +224,7 @@ class TreeView extends ChartView {
this._data = data;
}

_updateViewCoordSys(seriesModel: TreeSeriesModel) {
_updateViewCoordSys(seriesModel: TreeSeriesModel, api: ExtensionAPI) {
const data = seriesModel.getData();
const points: number[][] = [];
data.each(function (idx) {
Expand Down Expand Up @@ -257,7 +257,7 @@ class TreeView extends ChartView {

viewCoordSys.setBoundingRect(min[0], min[1], max[0] - min[0], max[1] - min[1]);

viewCoordSys.setCenter(seriesModel.get('center'));
viewCoordSys.setCenter(seriesModel.get('center'), api);
viewCoordSys.setZoom(seriesModel.get('zoom'));

// Here we use viewCoordSys just for computing the 'position' and 'scale' of the group
Expand Down
5 changes: 3 additions & 2 deletions src/chart/tree/treeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Payload } from '../../util/types';
import TreeSeriesModel from './TreeSeries';
import GlobalModel from '../../model/Global';
import { EChartsExtensionInstallRegisters } from '../../extension';
import type ExtensionAPI from '../../core/ExtensionAPI';

export interface TreeExpandAndCollapsePayload extends Payload {
dataIndex: number
Expand Down Expand Up @@ -51,12 +52,12 @@ export function installTreeAction(registers: EChartsExtensionInstallRegisters) {
// the layout. So don't need to go through the whole update process, such
// as 'dataPrcocess', 'coordSystemUpdate', 'layout' and so on.
update: 'none'
}, function (payload: RoamPaylod, ecModel: GlobalModel) {
}, function (payload: RoamPaylod, ecModel: GlobalModel, api: ExtensionAPI) {
ecModel.eachComponent({
mainType: 'series', subType: 'tree', query: payload
}, function (seriesModel: TreeSeriesModel) {
const coordSys = seriesModel.coordinateSystem;
const res = updateCenterAndZoom(coordSys, payload);
const res = updateCenterAndZoom(coordSys, payload, undefined, api);

seriesModel.setCenter
&& seriesModel.setCenter(res.center);
Expand Down
5 changes: 3 additions & 2 deletions src/component/geo/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { updateCenterAndZoom, RoamPaylod } from '../../action/roamHelper';
import MapSeries from '../../chart/map/MapSeries';
import GeoView from './GeoView';
import geoSourceManager from '../../coord/geo/geoSourceManager';
import type ExtensionAPI from '../../core/ExtensionAPI';

type RegisterMapParams = Parameters<typeof geoSourceManager.registerMap>;
function registerMap(
Expand Down Expand Up @@ -114,7 +115,7 @@ export function install(registers: EChartsExtensionInstallRegisters) {
type: 'geoRoam',
event: 'geoRoam',
update: 'updateTransform'
}, function (payload: RoamPaylod, ecModel: GlobalModel) {
}, function (payload: RoamPaylod, ecModel: GlobalModel, api: ExtensionAPI) {
const componentType = payload.componentType || 'series';

ecModel.eachComponent(
Expand All @@ -126,7 +127,7 @@ export function install(registers: EChartsExtensionInstallRegisters) {
}

const res = updateCenterAndZoom(
geo, payload, (componentModel as GeoModel).get('scaleLimit')
geo, payload, (componentModel as GeoModel).get('scaleLimit'), api
);

componentModel.setCenter
Expand Down
11 changes: 7 additions & 4 deletions src/coord/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import Transformable from 'zrender/src/core/Transformable';
import { CoordinateSystemMaster, CoordinateSystem } from './CoordinateSystem';
import GlobalModel from '../model/Global';
import { ParsedModelFinder, ParsedModelFinderKnown } from '../util/model';
import { parsePercent } from '../util/number';
import type ExtensionAPI from '../core/ExtensionAPI';

const v2ApplyTransform = vector.applyTransform;

Expand Down Expand Up @@ -82,7 +84,6 @@ class View extends Transformable implements CoordinateSystemMaster, CoordinateSy
*/
private _viewRect: BoundingRect;


constructor(name?: string) {
super();
this.name = name;
Expand Down Expand Up @@ -127,12 +128,14 @@ class View extends Transformable implements CoordinateSystemMaster, CoordinateSy
/**
* Set center of view
*/
setCenter(centerCoord?: number[]): void {
setCenter(centerCoord?: (number | string)[], api?: ExtensionAPI): void {
if (!centerCoord) {
return;
}
this._center = centerCoord;

this._center = [
parsePercent(centerCoord[0], api.getWidth()),
parsePercent(centerCoord[1], api.getHeight())
];
this._updateCenterAndZoom();
}

Expand Down
1 change: 1 addition & 0 deletions src/coord/geo/Geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ParsedModelFinder, ParsedModelFinderKnown, SINGLE_REFERRING } from '../
import GeoModel from './GeoModel';
import { resizeGeoType } from './geoCreator';
import { warn } from '../../util/log';
import type ExtensionAPI from '../../core/ExtensionAPI';

const GEO_DEFAULT_PARAMS: {
[type in GeoResource['type']]: {
Expand Down
2 changes: 1 addition & 1 deletion src/coord/geo/geoCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function resizeGeo(this: Geo, geoModel: ComponentModel<GeoOption | MapSeriesOpti

this.setViewRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);

this.setCenter(geoModel.get('center'));
this.setCenter(geoModel.get('center'), api);
this.setZoom(geoModel.get('zoom'));
}

Expand Down
2 changes: 1 addition & 1 deletion src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ export interface RoamOptionMixin {
/**
* Current center position.
*/
center?: number[]
center?: (number | string)[]
/**
* Current zoom level. Default is 1
*/
Expand Down
1 change: 1 addition & 0 deletions test/graph-simple.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/tree-legend.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.