Skip to content

Commit

Permalink
Merge pull request #18834 from linghaoSu/fix/sankey-emphasis
Browse files Browse the repository at this point in the history
fix(sankey): fix other state color is 'target/source/gradient'
  • Loading branch information
plainheart authored Jul 1, 2023
2 parents 82153b6 + f22ee7e commit 7866509
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 26 deletions.
70 changes: 44 additions & 26 deletions src/chart/sankey/SankeyView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import * as graphic from '../../util/graphic';
import { enterEmphasis, leaveEmphasis, toggleHoverEmphasis, setStatesStylesFromModel } from '../../util/states';
import { LayoutOrient, ECElement } from '../../util/types';
import { PathProps } from 'zrender/src/graphic/Path';
import type { PathProps, PathStyleProps } from 'zrender/src/graphic/Path';
import SankeySeriesModel, { SankeyEdgeItemOption, SankeyNodeItemOption } from './SankeySeries';
import ChartView from '../../view/Chart';
import GlobalModel from '../../model/Global';
Expand All @@ -30,6 +30,7 @@ import { RectLike } from 'zrender/src/core/BoundingRect';
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';

class SankeyPathShape {
x1 = 0;
Expand Down Expand Up @@ -195,30 +196,8 @@ class SankeyView extends ChartView {

curve.useStyle(lineStyleModel.getItemStyle());
// Special color, use source node color or target node color
switch (curve.style.fill) {
case 'source':
curve.style.fill = edge.node1.getVisual('color');
curve.style.decal = edge.node1.getVisual('style').decal;
break;
case 'target':
curve.style.fill = edge.node2.getVisual('color');
curve.style.decal = edge.node2.getVisual('style').decal;
break;
case 'gradient':
const sourceColor = edge.node1.getVisual('color');
const targetColor = edge.node2.getVisual('color');
if (isString(sourceColor) && isString(targetColor)) {
curve.style.fill = new graphic.LinearGradient(
0, 0, +(orient === 'horizontal'), +(orient === 'vertical'), [{
color: sourceColor,
offset: 0
}, {
color: targetColor,
offset: 1
}]
);
}
}
applyCurveStyle(curve.style, orient, edge);


const defaultEdgeLabelText = `${edgeModel.get('value')}`;
const edgeLabelStateModels = getLabelStatesModels(edgeModel, 'edgeLabel');
Expand Down Expand Up @@ -250,7 +229,13 @@ class SankeyView extends ChartView {

const emphasisModel = edgeModel.getModel('emphasis');

setStatesStylesFromModel(curve, edgeModel, 'lineStyle', (model) => model.getItemStyle());
setStatesStylesFromModel(curve, edgeModel, 'lineStyle', (model) => {
const style = model.getItemStyle();

applyCurveStyle(style, orient, edge);

return style;
});

group.add(curve);

Expand Down Expand Up @@ -362,6 +347,39 @@ class SankeyView extends ChartView {
}
}

/**
* Special color, use source node color or target node color
* @param curveProps curve's style to parse
* @param orient direction
* @param edge current curve data
*/
function applyCurveStyle(curveProps: PathStyleProps, orient: 'horizontal' | 'vertical', edge: GraphEdge) {
switch (curveProps.fill) {
case 'source':
curveProps.fill = edge.node1.getVisual('color');
curveProps.decal = edge.node1.getVisual('style').decal;
break;
case 'target':
curveProps.fill = edge.node2.getVisual('color');
curveProps.decal = edge.node2.getVisual('style').decal;
break;
case 'gradient':
const sourceColor = edge.node1.getVisual('color');
const targetColor = edge.node2.getVisual('color');
if (isString(sourceColor) && isString(targetColor)) {
curveProps.fill = new graphic.LinearGradient(
0, 0, +(orient === 'horizontal'), +(orient === 'vertical'), [{
color: sourceColor,
offset: 0
}, {
color: targetColor,
offset: 1
}]
);
}
}
}

// Add animation to the view
function createGridClipShape(rect: RectLike, seriesModel: SankeySeriesModel, cb: () => void) {
const rectEl = new graphic.Rect({
Expand Down
1 change: 1 addition & 0 deletions test/runTest/actions/__meta__.json

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

1 change: 1 addition & 0 deletions test/runTest/actions/sankey-emphasis-lineStyle.json

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

98 changes: 98 additions & 0 deletions test/sankey-emphasis-lineStyle.html

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

0 comments on commit 7866509

Please sign in to comment.