Skip to content

Commit

Permalink
feat(sankey): sankey support roam
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovilia committed Sep 5, 2024
1 parent d2eba0f commit 3962364
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/chart/sankey/SankeySeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ import {
GraphEdgeItemObject,
OptionDataValueNumeric,
DefaultEmphasisFocus,
CallbackDataParams
CallbackDataParams,
RoamOptionMixin
} from '../../util/types';
import GlobalModel from '../../model/Global';
import SeriesData from '../../data/SeriesData';
import { LayoutRect } from '../../util/layout';
import { createTooltipMarkup } from '../../component/tooltip/tooltipMarkup';
import View from '../../coord/View';


type FocusNodeAdjacency = boolean | 'inEdges' | 'outEdges' | 'allEdges';
Expand Down Expand Up @@ -95,7 +97,8 @@ export interface SankeyLevelOption extends SankeyNodeStateOption, SankeyEdgeStat
export interface SankeySeriesOption
extends SeriesOption<SankeyBothStateOption<CallbackDataParams>, ExtraStateOption>,
SankeyBothStateOption<CallbackDataParams>,
BoxLayoutOptionMixin {
BoxLayoutOptionMixin,
RoamOptionMixin {
type?: 'sankey'

/**
Expand Down Expand Up @@ -148,6 +151,8 @@ class SankeySeriesModel extends SeriesModel<SankeySeriesOption> {
static readonly type = 'series.sankey';
readonly type = SankeySeriesModel.type;

coordinateSystem: View;

levelModels: Model<SankeyLevelOption>[];

layoutInfo: LayoutRect;
Expand Down Expand Up @@ -297,6 +302,11 @@ class SankeySeriesModel extends SeriesModel<SankeySeriesOption> {

layoutIterations: 32,

// true | false | 'move' | 'scale', see module:component/helper/RoamController.
roam: false,
center: null,
zoom: 1,

label: {
show: true,
position: 'right',
Expand Down
71 changes: 71 additions & 0 deletions src/chart/sankey/SankeyView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle';
import { getECData } from '../../util/innerStore';
import { isString, retrieve3 } from 'zrender/src/core/util';
import type { GraphEdge } from '../../data/Graph';
import RoamController from '../../component/helper/RoamController';
import type { RoamControllerHost } from '../../component/helper/roamHelper';
import { onIrrelevantElement } from '../../component/helper/cursorHelper';
import * as roamHelper from '../../component/helper/roamHelper';

Check failure on line 37 in src/chart/sankey/SankeyView.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

'../../component/helper/roamHelper' import is duplicated
import View from '../../coord/View';

class SankeyPathShape {
x1 = 0;
Expand Down Expand Up @@ -111,6 +116,17 @@ class SankeyView extends ChartView {

private _data: SeriesData;

private _controller: RoamController;
private _controllerHost: RoamControllerHost;

init(ecModel: GlobalModel, api: ExtensionAPI): void {
this._controller = new RoamController(api.getZr());

this._controllerHost = {
target: this.group
} as RoamControllerHost;
}

render(seriesModel: SankeySeriesModel, ecModel: GlobalModel, api: ExtensionAPI) {
const sankeyView = this;
const graph = seriesModel.getGraph();
Expand All @@ -131,6 +147,14 @@ class SankeyView extends ChartView {
group.x = layoutInfo.x;
group.y = layoutInfo.y;

const viewCoordSys = seriesModel.coordinateSystem = new View();
viewCoordSys.zoomLimit = seriesModel.get('scaleLimit');
viewCoordSys.setBoundingRect(layoutInfo.x, layoutInfo.y, width, height);
viewCoordSys.setCenter(seriesModel.get('center'), api);
viewCoordSys.setZoom(seriesModel.get('zoom'));

this._updateController(seriesModel, ecModel, api);

// generate a bezire Curve for each edge
graph.eachEdge(function (edge) {
const curve = new SankeyPath();
Expand Down Expand Up @@ -346,6 +370,53 @@ class SankeyView extends ChartView {
}

dispose() {
this._controller && this._controller.dispose();
this._controllerHost = null;
}

private _updateController(
seriesModel: SankeySeriesModel,
ecModel: GlobalModel,
api: ExtensionAPI
) {
const controller = this._controller;
const controllerHost = this._controllerHost;
const group = this.group;
controller.setPointerChecker(function (e, x, y) {
const rect = group.getBoundingRect();
rect.applyTransform(group.transform);
return rect.contain(x, y)
&& !onIrrelevantElement(e, api, seriesModel);
});

controller.enable(seriesModel.get('roam'));
controllerHost.zoomLimit = seriesModel.get('scaleLimit');
controllerHost.zoom = seriesModel.coordinateSystem.getZoom();

controller
.off('pan')
.off('zoom')
.on('pan', (e) => {
roamHelper.updateViewOnPan(controllerHost, e.dx, e.dy);
api.dispatchAction({
seriesId: seriesModel.id,
type: 'sankeyRoam',
dx: e.dx,
dy: e.dy
});
})
.on('zoom', (e) => {
roamHelper.updateViewOnZoom(controllerHost, e.scale, e.originX, e.originY);
api.dispatchAction({
seriesId: seriesModel.id,
type: 'sankeyRoam',
zoom: e.scale,
originX: e.originX,
originY: e.originY
});
// Only update label layout on zoom
api.updateLabelLayout();
});
}
}

Expand Down
135 changes: 135 additions & 0 deletions test/sankey-roam.html

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

0 comments on commit 3962364

Please sign in to comment.