logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.49
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 节点通用配置项
      • 圆形节点 Circle
      • 菱形节点 Diamond
      • 甜甜圈节点 Donut
      • 椭圆形节点 Ellipse
      • 六边形节点 Hexagon
      • HTML节点 Html
      • 图片节点 Image
      • 矩形节点 Rect
      • 五角形节点 Star
      • 三角形节点 Triangle
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 边通用配置项
      • 三次贝塞尔曲线边 Cubic
      • 水平三次贝塞尔曲线边 CubicHorizontal
      • 垂直三次贝塞尔曲线边 CubicVertical
      • 直线边 Line
      • 二次贝塞尔曲线边 Quadratic
      • 折线边 Polyline
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 组合通用配置项
      • 圆形组合 Circle
      • 矩形组合 Rect
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 布局通用配置项
    • AntV Dagre 布局 AntvDagre
    • 环形布局 Circular
    • 复合布局 ComboCombined
    • 紧凑树布局 CompactBox
    • 同心圆布局 Concentric
    • 3D D3力导向布局 D3Force3D
    • D3力导向布局 D3Force
    • Dagre 布局
    • 生态树 Dendrogram
    • 鱼骨布局 Fishbone
    • ForceAtlas2 力导向布局
    • Force 力导向布局
    • Fruchterman 力导向布局
    • 网格布局 Grid
    • 缩进树 Indented
    • 高维数据降维布局 MDS
    • 脑图树 Mindmap
    • 径向布局 Radial
    • 随机布局 Random
    • 蛇形布局 Snake
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 缩放画布 ZoomCanvas
    • 标签自适应显示 AutoAdaptLabel
    • 框选 BrushSelect
    • 点击选中 ClickSelect
    • 展开/收起元素 CollapseExpand
    • 创建边 CreateEdge
    • 拖拽画布 DragCanvas
    • 拖拽元素 DragElement
    • 力导向拖拽元素 DragElementForce
    • 缩放画布时固定元素大小 FixElementSize
    • 聚焦元素 FocusElement
    • 悬停激活 HoverActivate
    • 套索选择 LassoSelect
    • 优化视口变换 OptimizeViewportTransform
    • 滚动画布 ScrollCanvas
    • 自定义交互
  • 插件 Plugin
    • 插件总览
    • 背景 Background
    • 气泡集 BubbleSets
    • 上下文菜单 Contextmenu
    • 边绑定 EdgeBundling
    • 边过滤镜 EdgeFilterLens
    • 鱼眼放大镜 Fisheye
    • 全屏展示 Fullscreen
    • 网格线 GridLine
    • 历史记录 History
    • 轮廓包围 Hull
    • 图例 Legend
    • 小地图 Minimap
    • 对齐线 Snapline
    • 时间条 Timebar
    • 工具栏 Toolbar
    • 提示框 Tooltip
    • 水印 Watermark
    • 自定义插件
  • 数据处理 Transform
    • 数据处理总览
    • 动态调整节点大小 MapNodeSize
    • 径向标签 PlaceRadialLabels
    • 平行边 ProcessParallelEdges
    • 自定义数据处理
  • 主题 Theme
    • 主题总览
    • 自定义主题
    • 色板
    • 自定义色板
  • 动画 Animation
    • 动画总览
    • 自定义动画
  • 扩展阅读
    • 事件
    • 渲染器
    • 坐标系
    • 下载图片
    • 使用 iconfont
    • 使用 3D
    • 项目打包
  • 版本特性
    • 新版本特性
    • 升级到 5.0
  • 常见问题
  • 参与贡献

水印 Watermark

上一篇
提示框 Tooltip
下一篇
自定义插件

资源

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

概述

水印插件支持使用文本和图片作为水印,实现原理是在 Graph 容器的 div 上加上 background-image 属性,然后通过 CSS 来控制水印的位置和样式。对于文本水印,会使用隐藏 canvas 将文本转换为图片的方式来实现。

使用场景

  • 为图表添加版权或所有权标识
  • 在演示或预览时标记图表的状态
  • 为敏感数据添加防泄露标记

基本用法

以下是一个简单的 Watermark 插件初始化示例:

const graph = new Graph({
plugins: [
{
type: 'watermark',
text: 'G6 Graph', // 水印文本
opacity: 0.2, // 透明度
rotate: Math.PI / 12, // 旋转角度
},
],
});

在线体验

createGraph(
{
data: { nodes: [{ id: 'node-1' }] },
node: { style: { fill: '#7e3feb' } },
edge: { style: { stroke: '#8b9baf' } },
layout: { type: 'force' },
behaviors: ['drag-canvas'],
plugins: [{ type: 'watermark', key: 'watermark', text: 'G6: Graph Visualization' }],
},
{ width: 600, height: 300 },
(gui, graph) => {
const options = {
type: 'watermark',
width: 200,
height: 100,
opacity: 0.2,
rotate: Math.PI / 12,
text: 'G6: Graph Visualization',
};
const optionFolder = gui.addFolder('Watermark Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'width', 1, 1280, 1);
optionFolder.add(options, 'height', 1, 800, 1);
optionFolder.add(options, 'opacity', 0, 1, 0.1);
optionFolder.add(options, 'rotate', 0, 2 * Math.PI, Math.PI / 12);
optionFolder.add(options, 'text');
optionFolder.onChange(({ property, value }) => {
graph.updatePlugin({
key: 'watermark',
[property]: value,
});
graph.render();
});
},
);

配置项

属性描述类型默认值必选
type插件类型stringwatermark✓
width单个水印的宽度number200
height单个水印的高度number100
opacity水印的透明度number0.2
rotate水印的旋转角度numberMath.PI / 12
imageURL图片水印的地址,优先级高于文本水印string-
text水印文本内容string-
textFill文本水印的颜色string#000
textFontSize文本水印的字体大小number16
textFontFamily文本水印的字体string-
textFontWeight文本水印的字体粗细string-
textFontVariant文本水印的字体变体string-
textAlign文本水印的对齐方式center | end | left | right | startcenter
textBaseline文本水印的基线对齐方式alphabetic | bottom | hanging | ideographic | middle | topmiddle
backgroundRepeat水印的重复方式stringrepeat
backgroundAttachment水印的背景定位行为string-
backgroundBlendMode水印的背景混合模式string-
backgroundClip水印的背景裁剪string-
backgroundColor水印的背景颜色string-
backgroundImage水印的背景图片string-
backgroundOrigin水印的背景原点string-
backgroundPosition水印的背景位置string-
backgroundPositionX水印的背景水平位置string-
backgroundPositionY水印的背景垂直位置string-
backgroundSize水印的背景大小string-

代码示例

文本水印

最简单的文本水印配置:

const graph = new Graph({
plugins: [
{
type: 'watermark',
text: 'G6 Graph',
},
],
});

import { Graph } from '@antv/g6';
const data = {
nodes: [{ id: 'node-0' }, { id: 'node-1' }, { id: 'node-2' }, { id: 'node-3' }, { id: 'node-4' }, { id: 'node-5' }],
edges: [
{ source: 'node-0', target: 'node-1' },
{ source: 'node-0', target: 'node-2' },
{ source: 'node-0', target: 'node-3' },
{ source: 'node-0', target: 'node-4' },
{ source: 'node-1', target: 'node-0' },
{ source: 'node-2', target: 'node-0' },
{ source: 'node-3', target: 'node-0' },
{ source: 'node-4', target: 'node-0' },
{ source: 'node-5', target: 'node-0' },
],
};
const graph = new Graph({
container: 'container',
data,
layout: { type: 'grid' },
behaviors: ['zoom-canvas', 'drag-canvas', 'drag-element'],
plugins: [
{
type: 'watermark',
text: 'G6: Graph Visualization',
textFontSize: 14,
textFontFamily: 'Microsoft YaHei',
fill: 'rgba(0, 0, 0, 0.1)',
rotate: Math.PI / 12,
},
],
});
graph.render();

图片水印

使用图片作为水印:

const graph = new Graph({
plugins: [
{
type: 'watermark',
imageURL: 'https://example.com/logo.png',
width: 100,
height: 50,
opacity: 0.1,
},
],
});

import { Graph } from '@antv/g6';
const data = {
nodes: [{ id: 'node-0' }, { id: 'node-1' }, { id: 'node-2' }, { id: 'node-3' }, { id: 'node-4' }, { id: 'node-5' }],
edges: [
{ source: 'node-0', target: 'node-1' },
{ source: 'node-0', target: 'node-2' },
{ source: 'node-0', target: 'node-3' },
{ source: 'node-0', target: 'node-4' },
{ source: 'node-1', target: 'node-0' },
{ source: 'node-2', target: 'node-0' },
{ source: 'node-3', target: 'node-0' },
{ source: 'node-4', target: 'node-0' },
{ source: 'node-5', target: 'node-0' },
],
};
const graph = new Graph({
container: 'container',
data,
layout: {
type: 'grid',
},
behaviors: ['zoom-canvas', 'drag-canvas', 'drag-element'],
plugins: [
{
type: 'watermark',
width: 200,
height: 100,
rotate: Math.PI / 12,
imageURL: 'https://gw.alipayobjects.com/os/s/prod/antv/assets/image/logo-with-text-73b8a.svg',
},
],
});
graph.render();

自定义样式

可以自定义水印的样式和位置:

const graph = new Graph({
plugins: [
{
type: 'watermark',
text: 'G6 Graph',
textFontSize: 20, // 设置字体大小
textFontFamily: 'Arial', // 设置字体
textFontWeight: 'bold', // 设置字体粗细
textFill: '#1890ff', // 设置文字颜色
rotate: Math.PI / 6, // 设置旋转角度
opacity: 0.15, // 设置透明度
width: 180, // 设置水印宽度
height: 100, // 设置水印高度
backgroundRepeat: 'space', // 设置重复方式
backgroundPosition: 'center', // 设置位置
textAlign: 'center', // 设置文本对齐
textBaseline: 'middle', // 设置基线对齐
},
],
});

实际案例

  • 文本水印
  • 图片水印