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(graph): make graph node draggable in none and circular layout #15428

Merged
merged 15 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(graph): support draggable for all graph layout ('none' | 'circul…
…ar' | 'force')
  • Loading branch information
kongmoumou committed Jul 31, 2021
commit ae38c73f1bab18bd72c259756fff3e3644ffe731
74 changes: 29 additions & 45 deletions src/chart/graph/GraphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Line from '../helper/Line';
import { getECData } from '../../util/innerStore';

import { simpleLayoutEdge } from './simpleLayoutHelper';
import { circularLayout, rotateNodeLabel } from './circularLayoutHelper';

function isViewCoordSys(coordSys: CoordinateSystem): coordSys is View {
return coordSys.type === 'view';
Expand Down Expand Up @@ -124,6 +125,8 @@ class GraphView extends ChartView {
this._startForceLayoutIteration(forceLayout, layoutAnimation);
}

const layout = seriesModel.get('layout');

data.graph.eachNode((node) => {
const idx = node.dataIndex;
const el = node.getGraphicEl() as Symbol;
Expand All @@ -133,20 +136,30 @@ class GraphView extends ChartView {
const draggable = itemModel.get('draggable');
if (draggable) {
el.on('drag', () => {
if (forceLayout) {
forceLayout.warmUp();
!this._layouting
&& this._startForceLayoutIteration(forceLayout, layoutAnimation);
forceLayout.setFixed(idx);
// Write position back to layout
data.setItemLayout(idx, [el.x, el.y]);
}
// handle simple layout dragging
if (!seriesModel.get('layout') || seriesModel.get('layout') === 'none') {
data.setItemLayout(idx, [el.x, el.y]);
// update edge
simpleLayoutEdge(seriesModel.getGraph(), seriesModel);
this.updateLayout(seriesModel);
switch (layout) {
case 'force':
forceLayout.warmUp();
!this._layouting
&& this._startForceLayoutIteration(forceLayout, layoutAnimation);
forceLayout.setFixed(idx);
// Write position back to layout
data.setItemLayout(idx, [el.x, el.y]);
break;
case 'circular':
// mark node fixed
node.setFixed(true);
data.setItemLayout(idx, [el.x, el.y]);
// recalculate circular layout
circularLayout(seriesModel, 'symbolSize', node);
this.updateLayout(seriesModel);
break;
case 'none':
default:
data.setItemLayout(idx, [el.x, el.y]);
// update edge
simpleLayoutEdge(seriesModel.getGraph(), seriesModel);
this.updateLayout(seriesModel);
break;
}
}).on('dragend', () => {
if (forceLayout) {
Expand Down Expand Up @@ -179,37 +192,8 @@ class GraphView extends ChartView {
&& seriesModel.get(['circular', 'rotateLabel']);
const cx = data.getLayout('cx');
const cy = data.getLayout('cy');
data.eachItemGraphicEl(function (el: Symbol, idx) {
const itemModel = data.getItemModel<GraphNodeItemOption>(idx);
let labelRotate = itemModel.get(['label', 'rotate']) || 0;
const symbolPath = el.getSymbolPath();
if (circularRotateLabel) {
const pos = data.getItemLayout(idx);
let rad = Math.atan2(pos[1] - cy, pos[0] - cx);
if (rad < 0) {
rad = Math.PI * 2 + rad;
}
const isLeft = pos[0] < cx;
if (isLeft) {
rad = rad - Math.PI;
}
const textPosition = isLeft ? 'left' as const : 'right' as const;

symbolPath.setTextConfig({
rotation: -rad,
position: textPosition,
origin: 'center'
});
const emphasisState = symbolPath.ensureState('emphasis');
zrUtil.extend(emphasisState.textConfig || (emphasisState.textConfig = {}), {
position: textPosition
});
}
else {
symbolPath.setTextConfig({
rotation: labelRotate *= Math.PI / 180
});
}
data.graph.eachNode((node) => {
rotateNodeLabel(node, circularRotateLabel, cx, cy);
});

this._firstRender = false;
Expand Down
11 changes: 11 additions & 0 deletions src/data/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ class GraphNode {
// Used in traverse of Graph
__visited: boolean;

// NOTE: only use in circular layout
private _fixed: boolean = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a better place to store the fixed status rather than the very basic graph structure. In fact, series-graph.node has fixed option. But currently it's only for force layout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently store in the fixed option of nodeModel


constructor(id?: string, dataIndex?: number) {
this.id = id == null ? '' : id;
this.dataIndex = dataIndex == null ? -1 : dataIndex;
Expand Down Expand Up @@ -387,6 +390,14 @@ class GraphNode {
}
return dataIndices;
}

setFixed(fixed: boolean) {
this._fixed = fixed;
}

getFixed() {
return this._fixed;
}
}


Expand Down