From 9cfcd6dc4c6701ff0226043dc0d956f8e5052292 Mon Sep 17 00:00:00 2001 From: Nadir Ali <36660597+Xnadir@users.noreply.github.com> Date: Mon, 23 Nov 2026 05:36:59 +0500 Subject: [PATCH] fix(graph): drop the view transform when a graph moves to cartesian2d. close #21857 GraphView applies its own view coordinate system's transform to its main group, but left the group untouched when the series has no view of its own. After a graph on the default view coordinate system, a graph set on cartesian2d kept that scale and offset, so its nodes and edges did not line up with the axes. The main group's transform is now reset when the series is not on its own view coordinate system. Co-Authored-By: Claude Opus 5.5 --- src/chart/graph/GraphView.ts | 7 ++++ test/ut/spec/series/graph.test.ts | 70 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 test/ut/spec/series/graph.test.ts diff --git a/src/chart/graph/GraphView.ts b/src/chart/graph/GraphView.ts index f5672a0e9d..cb56a1da7f 100644 --- a/src/chart/graph/GraphView.ts +++ b/src/chart/graph/GraphView.ts @@ -120,6 +120,13 @@ class GraphView extends ChartView implements RoamHostView { this._firstRender ? null : seriesModel, ); } + else { + // On cartesian2d (or any coordinate system that is not its own view), + // nodes are laid out in global coordinates. Drop the transform a + // previous render on a view coordinate system left on the group. + mainGroup.stopAnimation(); + mainGroup.attr({x: 0, y: 0, scaleX: 1, scaleY: 1}); + } // Fix edge contact point with node adjustEdge(seriesModel.getGraph(), getNodeGlobalScale(seriesModel)); diff --git a/test/ut/spec/series/graph.test.ts b/test/ut/spec/series/graph.test.ts new file mode 100644 index 0000000000..e30f86f7a4 --- /dev/null +++ b/test/ut/spec/series/graph.test.ts @@ -0,0 +1,70 @@ +/* +* 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 } from '../../core/utHelper'; + + +describe('graph', function () { + + const viewOption = { + animation: false, + series: [{ + type: 'graph', + layout: 'none', + data: [{ name: 'a', x: 0, y: 0 }, { name: 'b', x: 1000, y: 800 }], + links: [{ source: 'a', target: 'b' }] + }] + }; + + const cartesianOption = { + animation: false, + xAxis: { type: 'value', min: 0, max: 10 }, + yAxis: { type: 'value', min: 0, max: 10 }, + series: [{ + type: 'graph', + coordinateSystem: 'cartesian2d', + data: [[2, 3], [8, 7]], + links: [{ source: 0, target: 1 }] + }] + }; + + function mainGroupTransform(chart: any) { + const group = chart._chartsViews[0]._mainGroup; + return [group.x, group.y, group.scaleX, group.scaleY]; + } + + it('places a cartesian2d graph on the grid after a graph on the view coordinate system', function () { + const switched = createChart({width: 400, height: 300}); + const direct = createChart({width: 400, height: 300}); + + try { + switched.setOption(viewOption); + switched.setOption(cartesianOption, true); + direct.setOption(cartesianOption); + + expect(mainGroupTransform(switched)).toEqual(mainGroupTransform(direct)); + expect(mainGroupTransform(switched)).toEqual([0, 0, 1, 1]); + } + finally { + switched.dispose(); + direct.dispose(); + } + }); +});