Skip to content

Commit 67d6232

Browse files
feat: title plugin (#7294)
* feat: wip title plugin * feat: title plugin 写法优化,及文档完善 * test: add title plugin test * fix: title plugin cr fix --------- Co-authored-by: hustcc <i@hust.cc>
1 parent fd0f66a commit 67d6232

File tree

9 files changed

+810
-0
lines changed

9 files changed

+810
-0
lines changed

packages/g6/__tests__/demos/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export { pluginMinimap } from './plugin-minimap';
143143
export { pluginMiniMapEdgeArrow } from './plugin-minimap-edge-arrow';
144144
export { pluginSnapline } from './plugin-snapline';
145145
export { pluginTimebar } from './plugin-timebar';
146+
export { pluginTitle } from './plugin-title';
146147
export { pluginToolbarBuildIn } from './plugin-toolbar-build-in';
147148
export { pluginToolbarIconfont } from './plugin-toolbar-iconfont';
148149
export { pluginTooltip } from './plugin-tooltip';
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Graph } from '@antv/g6';
2+
3+
export const pluginTitle: TestCase = async (context) => {
4+
const graph = new Graph({
5+
...context,
6+
data: { nodes: Array.from({ length: 12 }).map((_, i) => ({ id: `node${i}` })) },
7+
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element'],
8+
plugins: [
9+
{
10+
key: 'title',
11+
type: 'title',
12+
13+
align: 'center',
14+
spacing: 4,
15+
size: 60,
16+
17+
title: '这是一个标题这是一个标题',
18+
titleFontSize: 28,
19+
titleFontFamily: 'sans-serif',
20+
titleFontWeight: 600,
21+
titleFill: '#fff',
22+
titleFillOpacity: 1,
23+
titleStroke: '#000',
24+
titleLineWidth: 2,
25+
titleStrokeOpacity: 1,
26+
27+
subtitle: '这是一个副标',
28+
subtitleFontSize: 16,
29+
subtitleFontFamily: 'Arial',
30+
subtitleFontWeight: 300,
31+
subtitleFill: '#2989FF',
32+
subtitleFillOpacity: 1,
33+
subtitleStroke: '#000',
34+
subtitleLineWidth: 1,
35+
subtitleStrokeOpacity: 0.5,
36+
},
37+
],
38+
node: {
39+
palette: 'spectral',
40+
style: { labelText: '你好' },
41+
},
42+
layout: {
43+
type: 'circular',
44+
},
45+
autoFit: 'view',
46+
});
47+
48+
await graph.render();
49+
50+
return graph;
51+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { Graph, Label, Title } from '@/src';
2+
import { pluginTitle } from '@@/demos';
3+
import { createDemoGraph } from '../../utils';
4+
5+
describe('plugin title', () => {
6+
let graph: Graph;
7+
let titlePlugin: Title;
8+
9+
const executeTest = () => {
10+
expect(typeof titlePlugin).toBe('object');
11+
12+
const [width] = graph.getSize();
13+
14+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
15+
// @ts-ignore
16+
const [titleLabel, subtitleLabel] = titlePlugin.canvas.getRoot().childNodes;
17+
18+
const titleAttr = (titleLabel as Label).attributes;
19+
const titleX = titleAttr.x;
20+
expect(titleX).toBe(width / 2);
21+
22+
const subtitleAttr = (subtitleLabel as Label).attributes;
23+
const subtitleX = subtitleAttr.x;
24+
expect(subtitleX).toBe(width / 2);
25+
26+
expect(titleAttr.text).toBe('这是一个标题这是一个标题');
27+
expect(subtitleAttr.text).toBe('这是一个副标');
28+
29+
expect(titleAttr.fontSize).toBe(28);
30+
expect(subtitleAttr.fill).toBe('#2989FF');
31+
};
32+
33+
beforeAll(async () => {
34+
graph = await createDemoGraph(pluginTitle);
35+
titlePlugin = graph.getPluginInstance('title');
36+
});
37+
38+
it('title', executeTest);
39+
40+
afterAll(() => {
41+
graph.destroy();
42+
});
43+
});

packages/g6/src/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export {
9292
Minimap,
9393
Snapline,
9494
Timebar,
95+
Title,
9596
Toolbar,
9697
Tooltip,
9798
Watermark,

packages/g6/src/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export { Legend } from './legend';
1414
export { Minimap } from './minimap';
1515
export { Snapline } from './snapline';
1616
export { Timebar } from './timebar';
17+
export { Title } from './title';
1718
export { Toolbar } from './toolbar';
1819
export { Tooltip } from './tooltip';
1920
export { Watermark } from './watermark';
@@ -34,6 +35,7 @@ export type { LegendOptions } from './legend';
3435
export type { MinimapOptions } from './minimap';
3536
export type { SnaplineOptions } from './snapline';
3637
export type { TimebarOptions } from './timebar';
38+
export type { SubTitleStyle, TitleOptions, TitleStyle } from './title';
3739
export type { ToolbarOptions } from './toolbar';
3840
export type { TooltipOptions } from './tooltip';
3941
export type { WatermarkOptions } from './watermark';

0 commit comments

Comments
 (0)