From 005afe3476c087f5319d330518b50e6b9d5ec196 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 26 Nov 2026 07:16:53 +0000 Subject: [PATCH] fix(graph): fix thumbnail of graph with SVG path or image symbols. close #21514 Co-Authored-By: Rahul Kashayp --- src/chart/graph/GraphView.ts | 42 +++++----- test/ut/spec/series/graph-thumbnail.test.ts | 89 +++++++++++++++++++++ 2 files changed, 113 insertions(+), 18 deletions(-) create mode 100644 test/ut/spec/series/graph-thumbnail.test.ts diff --git a/src/chart/graph/GraphView.ts b/src/chart/graph/GraphView.ts index f5672a0e9d..bce14e4a03 100644 --- a/src/chart/graph/GraphView.ts +++ b/src/chart/graph/GraphView.ts @@ -46,7 +46,8 @@ import { getECData } from '../../util/innerStore'; import { simpleLayoutEdge } from './simpleLayoutHelper'; import { circularLayout, rotateNodeLabel } from './circularLayoutHelper'; -import { clone, extend } from 'zrender/src/core/util'; +import { clone } from 'zrender/src/core/util'; +import { createSymbol } from '../../util/symbol'; import ECLinePath from '../helper/LinePath'; import { NullUndefined, RoamHostView, RoamPayload } from '../../util/types'; import { getThumbnailBridge, ThumbnailBridge } from '../../component/helper/thumbnailBridge'; @@ -382,25 +383,30 @@ class GraphView extends ChartView implements RoamHostView { bridgeGroup.add(symbolGroup); bridgeGroup.add(lineGroup); + const data = seriesModel.getData(); + // TODO: reuse elements for performance in large graph? for (let i = 0; i < symbolNodes.length; i++) { - const node = symbolNodes[i]; - const sub = (node as graphic.Group).children()[0]; - const x = (node as Symbol).x; - const y = (node as Symbol).y; - const subShape = clone((sub as graphic.Path).shape); - const shape = extend(subShape, { - width: sub.scaleX, - height: sub.scaleY, - x: x - sub.scaleX / 2, - y: y - sub.scaleY / 2 - }); - const style = clone((sub as graphic.Path).style); - const subThumbnail = new (sub as any).constructor({ - shape, - style, - z2: 151 - }); + const node = symbolNodes[i] as Symbol; + const sub = node.getSymbolPath(); + const width = sub.scaleX; + const height = sub.scaleY; + // Recreate the symbol rather than cloning `sub` via its constructor and shape, + // since SVG path (`path://`) and image (`image://`) symbols are not described by shape. + const subThumbnail = createSymbol( + node.getSymbolType(), + node.x - width / 2, + node.y - height / 2, + width, + height, + null, + data.getItemVisual(getECData(node).dataIndex, 'symbolKeepAspect') + ); + // Image symbols keep their geometry in style. + if (!(subThumbnail instanceof graphic.Image)) { + subThumbnail.useStyle(clone(sub.style)); + } + subThumbnail.z2 = 151; symbolGroup.add(subThumbnail); } diff --git a/test/ut/spec/series/graph-thumbnail.test.ts b/test/ut/spec/series/graph-thumbnail.test.ts new file mode 100644 index 0000000000..73971f21b9 --- /dev/null +++ b/test/ut/spec/series/graph-thumbnail.test.ts @@ -0,0 +1,89 @@ +/* +* 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 { createChart, removeChart } from '../../core/utHelper'; +import { EChartsType } from '../../../../src/echarts'; + + +function getThumbnailContentRect(chart: EChartsType) { + const thumbnailModel = (chart as any).getModel().getComponent('thumbnail'); + const thumbnailView = (chart as any).getViewOfComponentModel(thumbnailModel); + return thumbnailView._bridgeRendered.group.getBoundingRect(); +} + +function renderGraph(chart: EChartsType, symbol: string) { + chart.setOption({ + animation: false, + thumbnail: {}, + series: [{ + type: 'graph', + layout: 'none', + symbol, + symbolSize: 20, + data: [ + {name: 'a', x: 100, y: 100}, + {name: 'b', x: 200, y: 100}, + {name: 'c', x: 150, y: 200} + ], + links: [ + {source: 'a', target: 'b'}, + {source: 'b', target: 'c'} + ] + }] + }); +} + +describe('graph thumbnail', function () { + + let chart: EChartsType; + beforeEach(function () { + chart = createChart(); + }); + + afterEach(function () { + removeChart(chart); + }); + + it('should not shift or zoom out content when symbol is an SVG path', function () { + renderGraph(chart, 'circle'); + const circleRect = getThumbnailContentRect(chart); + + renderGraph(chart, 'path://M0,0 L10,0 L10,10 L0,10 Z'); + const pathRect = getThumbnailContentRect(chart); + + expect(pathRect.x).toBeCloseTo(circleRect.x); + expect(pathRect.y).toBeCloseTo(circleRect.y); + expect(pathRect.width).toBeCloseTo(circleRect.width); + expect(pathRect.height).toBeCloseTo(circleRect.height); + }); + + it('should not shift or zoom out content when symbol is an image', function () { + renderGraph(chart, 'circle'); + const circleRect = getThumbnailContentRect(chart); + + renderGraph(chart, 'image://data:image/gif;base64,R0lGODlhAQABAAAAACw='); + const imageRect = getThumbnailContentRect(chart); + + expect(imageRect.x).toBeCloseTo(circleRect.x); + expect(imageRect.y).toBeCloseTo(circleRect.y); + expect(imageRect.width).toBeCloseTo(circleRect.width); + expect(imageRect.height).toBeCloseTo(circleRect.height); + }); + +});