From 80ca2787ae40dca5d6345422fa8a0af874770139 Mon Sep 17 00:00:00 2001 From: Yucheng Du <256645790+yucheng-du@users.noreply.github.com> Date: Sat, 28 Nov 2026 01:37:50 -0800 Subject: [PATCH] feat(sankey): distinguish node and link colors with ARIA decals. close #15513 Generated-by: OpenAI Codex (GPT-6.2 Astra Ultra) --- src/chart/sankey/SankeySeries.ts | 35 ++++++++++ src/chart/sankey/SankeyView.ts | 5 ++ test/ut/spec/series/sankey-decal.test.ts | 82 ++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 test/ut/spec/series/sankey-decal.test.ts diff --git a/src/chart/sankey/SankeySeries.ts b/src/chart/sankey/SankeySeries.ts index dfc47dbdcf..2cd8773416 100644 --- a/src/chart/sankey/SankeySeries.ts +++ b/src/chart/sankey/SankeySeries.ts @@ -17,7 +17,9 @@ * under the License. */ +import { extend, isString } from 'zrender/src/core/util'; import SeriesModel from '../../model/Series'; +import { getDecalFromPalette } from '../../model/mixin/palette'; import createGraphFromNodeEdge from '../helper/createGraphFromNodeEdge'; import Model from '../../model/Model'; import { @@ -29,6 +31,8 @@ import { LineStyleOption, LayoutOrient, ColorString, + InnerDecalObject, + ZRColor, StatesOptionMixin, OptionDataItemObject, GraphEdgeItemObject, @@ -251,6 +255,37 @@ class SankeySeriesModel extends SeriesModel implements RoamH return this.getGraph().edgeData; } + enableAriaDecal() { + const graph = this.getGraph(); + const scope = {}; + const decalForColor = (color: ZRColor, fallback: string) => { + const decal: InnerDecalObject = getDecalFromPalette( + this.ecModel, isString(color) ? 'color:' + color : fallback, scope + ); + if (decal) { + decal.dirty = true; + } + return decal; + }; + + graph.eachNode(node => { + // Sankey colors come from flow values, levels or individual node options. + const paletteDecal = decalForColor(node.getVisual('color'), 'node:' + node.id); + const specifiedDecal = node.getModel().get(['itemStyle', 'decal']); + node.setVisual('decal', specifiedDecal && specifiedDecal !== 'none' + ? extend(extend({}, paletteDecal), specifiedDecal) : specifiedDecal || paletteDecal); + }); + + graph.eachEdge(edge => { + const color = edge.getModel().get(['lineStyle', 'color']); + // Source and target links already inherit the node's rendered pattern. + // A gradient interpolates between two colors and has no single color key. + if (color && color !== 'source' && color !== 'target' && color !== 'gradient') { + edge.setVisual('decal', decalForColor(color, 'edge:' + edge.dataIndex)); + } + }); + } + formatTooltip( dataIndex: number, multipleSeries: boolean, diff --git a/src/chart/sankey/SankeyView.ts b/src/chart/sankey/SankeyView.ts index 21876a48e6..b2fb478e76 100644 --- a/src/chart/sankey/SankeyView.ts +++ b/src/chart/sankey/SankeyView.ts @@ -18,6 +18,7 @@ */ import * as graphic from '../../util/graphic'; +import { createOrUpdatePatternFromDecal } from '../../util/decal'; import { enterEmphasis, leaveEmphasis, toggleHoverEmphasis, setStatesStylesFromModel } from '../../util/states'; import { LayoutOrient, ECElement, RoamHostView, RoamPayload } from '../../util/types'; import type { PathProps, PathStyleProps } from 'zrender/src/graphic/Path'; @@ -216,6 +217,10 @@ class SankeyView extends ChartView implements RoamHostView { }); curve.useStyle(lineStyleModel.getItemStyle()); + const decal = edge.getVisual('decal'); + if (decal) { + curve.style.decal = createOrUpdatePatternFromDecal(decal, api); + } // Special color, use source node color or target node color applyCurveStyle(curve.style, orient, edge); diff --git a/test/ut/spec/series/sankey-decal.test.ts b/test/ut/spec/series/sankey-decal.test.ts new file mode 100644 index 0000000000..7e02c1e7f8 --- /dev/null +++ b/test/ut/spec/series/sankey-decal.test.ts @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import type Path from 'zrender/src/graphic/Path'; +import { EChartsType } from '@/src/echarts'; +import SankeySeriesModel, { SankeySeriesOption } from '@/src/chart/sankey/SankeySeries'; +import { createChart, getECModel } from '../../core/utHelper'; + +describe('Sankey ARIA decals', function () { + let chart: EChartsType; + + beforeEach(() => { + chart = createChart({opts: {renderer: 'svg'}}); + }); + afterEach(() => chart.dispose()); + + function setSankey(series: SankeySeriesOption) { + chart.setOption({ + animation: false, + aria: {enabled: true, decal: {show: true, decals: [{symbol: 'rect'}, {symbol: 'circle'}, {symbol: 'diamond'}]}}, + series: [{type: 'sankey', ...series}] + }); + return (getECModel(chart).getSeries()[0] as SankeySeriesModel).getGraph(); + } + + it('uses one pattern per node color and reuses it for matching link colors', () => { + const graph = setSankey({ + data: [ + {name: 'Red source', itemStyle: {color: '#c00'}}, + {name: 'Red target', itemStyle: {color: '#c00'}}, + {name: 'Blue', itemStyle: {color: '#03c'}} + ], + links: [ + {source: 'Red source', target: 'Blue', value: 3, lineStyle: {color: 'source'}}, + {source: 'Blue', target: 'Red target', value: 2, lineStyle: {color: 'target'}}, + {source: 'Red source', target: 'Red target', value: 1, lineStyle: {color: '#03c'}} + ] + }); + const red = graph.nodes[0].getVisual('style').decal; + const blue = graph.nodes[2].getVisual('style').decal; + expect(red).toBeTruthy(); + expect(blue).toBeTruthy(); + expect(red).not.toBe(blue); + expect(graph.nodes[1].getVisual('style').decal).toBe(red); + expect(graph.edges.map(edge => (edge.getGraphicEl() as Path).style.decal)).toEqual([red, red, blue]); + + chart.setOption({aria: {decal: {show: false}}}); + const updated = (getECModel(chart).getSeries()[0] as SankeySeriesModel).getGraph(); + expect(updated.nodes.every(node => !node.getVisual('style').decal)).toBe(true); + expect(updated.edges.every(edge => !(edge.getGraphicEl() as Path).style.decal)).toBe(true); + }); + + it('uses resolved value colors and preserves level and node overrides', () => { + const graph = setSankey({ + data: [{name: 'A'}, {name: 'B'}, {name: 'C', itemStyle: {decal: {symbol: 'triangle'}}}], + links: [{source: 'A', target: 'B', value: 5}, {source: 'B', target: 'C', value: 2}], + levels: [{depth: 0, itemStyle: {decal: {symbolSize: 0.5}}}] + }); + const [a, b, c] = graph.nodes; + expect(a.getVisual('color')).toBe(b.getVisual('color')); + expect(a.getVisual('color')).not.toBe(c.getVisual('color')); + expect(a.getVisual('decal')).toMatchObject({symbol: 'rect', symbolSize: 0.5}); + expect(b.getVisual('decal').symbol).toBe('rect'); + expect(c.getVisual('decal').symbol).toBe('triangle'); + }); +});