Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "修复设置 overflow: 'scroll' 的桑基图无法滚动的问题(#4577)。",
"type": "none",
"packageName": "@visactor/vchart"
}
],
"packageName": "@visactor/vchart",
"email": "dingling112@gmail.com"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { assertionError } from '../../../assertions.mjs';
import { interactionFrame } from '../../../interaction-helpers.mjs';
import sankeyScrollbar from './sankey-scrollbar.mjs';

/**
* 验证目的:组合图在已有 Stage 上启用桑基滚动后可查看被裁剪的内容。
* 图表类型:common,包含 sankey 系列。
* 关键配置:通过 updateSpecSync 为系列添加 overflow: 'scroll'。
* 场景条件:三层固定流向,缩窄绘图区后链接仍宽于视口。
* 最终检查:Stage 被复用,且真实滚轮和滑块拖动都能移动内容。
* 覆盖边界:VChart 创建的 Stage 和横向溢出。
*/
function createSpec(overflow) {
const { data, overflow: _overflow, ...series } = sankeyScrollbar.createSpec();
return {
type: 'common',
data,
series: [{ ...series, dataIndex: 0, ...(overflow ? { overflow } : {}) }]
};
}

export default {
createSpec: () => createSpec(),
async exercise(page) {
const reusedStage = await page.evaluate(spec => {
const chart = window.__visualChart;
const stage = chart.getStage();
chart.updateSpecSync({ ...spec, animation: false });
return chart.getStage() === stage;
}, createSpec('scroll'));
if (!reusedStage) {
throw assertionError('开启桑基滚动时应复用组合图的 Stage');
}
await interactionFrame(page);
await sankeyScrollbar.exercise(page);
},
verify: sankeyScrollbar.verify
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { assertionError } from '../../../assertions.mjs';
import { verifyRendered } from '../../../helpers.mjs';
import { interactionFrame, interactionTarget } from '../../../interaction-helpers.mjs';

/**
* 验证目的:防止 #4577 的桑基图横向滚动失效。
* 图表类型:sankey。
* 关键配置:overflow: 'scroll'、固定 linkWidth。
* 场景条件:三层固定流向,缩窄绘图区后链接仍宽于视口。
* 最终检查:真实滚轮和滑块拖动均移动内容,根组视口随绘图区调整。
* 覆盖边界:横向溢出和桌面指针交互。
*/
const wheelDelta = 100;

async function readViewport(page) {
return page.evaluate(() => {
const [series] = window.__visualChart.getChart().getAllSeries();
const { width, height } = series.getRootMark().getProduct().attribute;
return { width, height, region: series.getRegion().getLayoutRect() };
});
}

async function readScroll(page) {
return page.evaluate(() => {
const [series] = window.__visualChart.getChart().getAllSeries();
const group = series.getRootMark().getProduct();
const scrollbar = group.parent.children.find(
child => child.isScrollBar && child.attribute.direction === 'horizontal'
);
return {
offset: group.attribute.scrollX ?? 0,
range: scrollbar?.attribute.range,
name: scrollbar?.name
};
});
}

export default {
createSpec() {
return {
type: 'sankey',
overflow: 'scroll',
linkWidth: 450,
nodeWidth: 20,
categoryField: 'name',
nodeKey: 'name',
sourceField: 'source',
targetField: 'target',
valueField: 'value',
data: [
{
values: [
{
nodes: [{ name: 'Source' }, { name: 'Process' }, { name: 'Target' }],
links: [
{ source: 'Source', target: 'Process', value: 8 },
{ source: 'Process', target: 'Target', value: 8 }
]
}
]
}
]
};
},

async exercise(page) {
const initial = await readViewport(page);
await page.evaluate(() => window.__visualChart.resize(640, 520));
await interactionFrame(page);
const resized = await readViewport(page);
const point = await interactionTarget(page, 'node');
await page.mouse.move(point.x, point.y);
await page.mouse.wheel(wheelDelta, 0);
await interactionFrame(page);

const wheel = await readScroll(page);
if (wheel.offset !== -wheelDelta || !wheel.name || !(wheel.range?.[0] > 0)) {
throw assertionError(`横向滚轮应将内容偏移至 -${wheelDelta}px 并显示滚动条,实际为 ${JSON.stringify(wheel)}`);
}

// 从已渲染的滑块起拖,避免把轨道点击误判为拖动。
const slider = await page.evaluate(name => {
const scrollbar = window.__visualChart.getStage().find(graphic => graphic.name === name, true);
const thumb = scrollbar.find(graphic => graphic.name === 'slider', true);
if (!thumb) throw new Error('缺少可拖动的桑基图滚动条滑块');
const { x1, y1, x2, y2 } = thumb.globalAABBBounds;
return { x: (x1 + x2) / 2, y: (y1 + y2) / 2 };
}, wheel.name);
await page.mouse.move(slider.x, slider.y);
await page.mouse.down();
await page.mouse.move(slider.x + 60, slider.y, { steps: 5 });
await page.mouse.up();
await interactionFrame(page);
await page.evaluate(result => (window.__sankeyScrollExercise = result), { initial, resized, wheel });
},

async verify(page) {
await verifyRendered(page);
const { initial, resized, wheel } = await page.evaluate(() => window.__sankeyScrollExercise);
if (
initial.width !== initial.region.width ||
initial.height !== initial.region.height ||
resized.width !== resized.region.width ||
resized.height !== resized.region.height ||
resized.width >= initial.width ||
resized.height >= initial.height
) {
throw assertionError('桑基图滚动视口的宽高应随绘图区缩小');
}
const dragged = await readScroll(page);
if (!(dragged.offset < wheel.offset && dragged.range?.[0] > wheel.range[0])) {
throw assertionError(`拖动滑块应继续移动桑基图内容,实际为 ${JSON.stringify(dragged)}`);
}
}
};
11 changes: 11 additions & 0 deletions packages/vchart/__tests__/visual/cases/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,17 @@ export const cases = [
purpose: '来源滚动条拖动后点击轴标签更新柱颜色',
file: './components/scrollbar/scrollbar-axis-click-update.mjs'
},
{
id: 'sankey-scrollbar',
purpose: '桑基图横向滚轮与滑块拖动移动内容,滚动视口随绘图区调整',
file: './charts/sankey/sankey-scrollbar.mjs',
sourceExample: 'docs/assets/examples/zh/sankey-chart/sankey-with-scrollbar.md'
},
{
id: 'common-sankey-scrollbar',
purpose: '组合图复用 Stage 开启桑基滚动后,滚轮和滑块均移动内容',
file: './charts/sankey/common-sankey-scrollbar.mjs'
},
{
id: 'sankey-selected-adjacency',
purpose: '点击桑基节点时仅高亮相邻节点与连接,空白点击恢复',
Expand Down
4 changes: 3 additions & 1 deletion packages/vchart/__tests__/visual/cases/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaVersion": 1,
"indexDigest": "0f05d547be269348230381c38230b6846f2461dbdb31c85cb8f5e82158ace216",
"indexDigest": "f3e0a8c119c9afc6314a72e74376b61f99e3d2401406b358c68dff08c7118c22",
"ids": [
"title-resize-legend-layout",
"injected-stage",
Expand Down Expand Up @@ -256,6 +256,8 @@
"scrollbar-auto",
"scrollbar-crosshair",
"scrollbar-axis-click-update",
"sankey-scrollbar",
"common-sankey-scrollbar",
"sankey-selected-adjacency",
"sankey-adjacency",
"sankey-node-state",
Expand Down
2 changes: 0 additions & 2 deletions packages/vchart/src/chart/sankey/sankey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { ISeries } from '../../series/interface';
import type { IMark, IMarkGraphic } from '../../mark/interface/common';
import type { IRegionQuerier } from '../../typings/params';
import { isArray } from '@visactor/vutils';
import { loadScrollbar } from '@visactor/vrender-components';
import { registerMarkTooltipProcessor } from '../../component/tooltip/processor/mark-tooltip';
import { getDatumOfGraphic } from '../../util/mark';

Expand Down Expand Up @@ -71,7 +70,6 @@ export class SankeyChart<T extends ISankeyChartSpec = ISankeyChartSpec> extends

export const registerSankeyChart = () => {
registerMarkTooltipProcessor();
loadScrollbar();
registerSankeySeries();

Factory.registerChart(SankeyChart.type, SankeyChart);
Expand Down
22 changes: 15 additions & 7 deletions packages/vchart/src/core/vchart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { IDataValues, IMarkStateSpec, IInitOption } from '../typings/spec/c
// eslint-disable-next-line no-duplicate-imports
import { RenderModeEnum } from '../typings/spec/common';
import type { ISeriesConstructor } from '../series/interface';
import { SeriesTypeEnum } from '../series/interface/type';
import {
ChartTypeEnum,
type DimensionIndexOption,
Expand Down Expand Up @@ -426,19 +427,26 @@ export class VChart implements IVChart {
const runtimePluginInstallers: ((app?: IApp) => void)[] = [];

if (poptip !== false) {
const poptipInstaller = Factory.getRuntimePluginInstaller('poptipForText');
if (poptipInstaller) {
poptipInstaller();
runtimePluginInstallers.push(poptipInstaller);
}
pluginList.push('poptipForText');
}

if (spec.type === ChartTypeEnum.sankey) {
// 桑基图默认记载滚动条组件
const { type, series } = this._spec as ISpec;
// 组合图可在 updateSpec 时为已有桑基系列开启滚动,届时仍使用原来的 Stage。
if (
type === ChartTypeEnum.sankey ||
(type === ChartTypeEnum.common && series?.some(seriesSpec => seriesSpec.type === SeriesTypeEnum.sankey))
) {
pluginList.push('scrollbar');
}

for (const name of pluginList) {
const installer = Factory.getRuntimePluginInstaller(name);
if (installer) {
installer();
runtimePluginInstallers.push(installer);
}
}

const logger = new Logger(this._option.logLevel ?? LoggerLevel.Error);
Logger.setInstance(logger);
if (this._option?.onError) {
Expand Down
30 changes: 28 additions & 2 deletions packages/vchart/src/series/sankey/sankey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { getDatumOfGraphic } from '../../util';
import { addRuntimeState } from '../../mark/utils/glyph';
import { sankey } from '../../theme/builtin/common/series/sankey';
import { addGraphicState, removeGraphicState } from '../../util/graphic-state';
import { installScrollbarToApp, loadScrollbar } from '@visactor/vrender-components';
import type { IApp } from '@visactor/vrender-core';

export class SankeySeries<T extends ISankeySeriesSpec = ISankeySeriesSpec> extends CartesianSeries<T> {
static readonly type: string = SeriesTypeEnum.sankey;
Expand Down Expand Up @@ -238,11 +240,25 @@ export class SankeySeries<T extends ISankeySeriesSpec = ISankeySeriesSpec> exten
}

initMark(): void {
const { overflow } = this._spec;
// 为了让sankey 正常的滚动,interactive 需要设置为true,不然在空白处会滚动不了
this._rootMark.setMarkConfig({
overflow: this._spec.overflow,
interactive: !!this._spec.overflow
overflow,
interactive: !!overflow
});
if (overflow && overflow !== 'hidden' && this.getStage()?.getPluginsByName('scrollbar').length) {
this._rootMark.setMarkConfig({ clip: true });
Comment thread
xile611 marked this conversation as resolved.
Resolved
this.setMarkStyle(
this._rootMark,
{
width: () => this._region.getLayoutRect().width,
height: () => this._region.getLayoutRect().height
},
'normal',
AttributeLevel.Built_In
);
}

const nodeMark = this._createMark(SankeySeries.mark.node, {
isSeriesMark: true,
dataView: this._nodesSeriesData
Expand Down Expand Up @@ -1211,7 +1227,17 @@ export class SankeySeries<T extends ISankeySeriesSpec = ISankeySeriesSpec> exten
}
}

const installScrollbar = (app?: IApp) => {
if (app) {
installScrollbarToApp(app);
} else {
loadScrollbar();
}
};

export const registerSankeySeries = () => {
Factory.registerRuntimePluginInstaller('scrollbar', installScrollbar);
installScrollbar();
registerRectMark();
registerLinkPathMark();
registerTextMark();
Expand Down
Loading