Ellipse

阅读时间约 4 分钟

G6 内置了  ellipse 节点,其默认样式如下。标签文本位于椭圆中央。

img

使用方法

内置节点 一节所示,配置节点的方式有三种:实例化图时全局配置,在数据中动态配置,使用 graph.node(nodeFn) 函数配置。这几种配置方法可以同时使用,优先级:

使用 graph.node(nodeFn) 配置 > 数据中动态配置 > 实例化图时全局配置

⚠️ 注意:idlabel 应当配置到每个节点数据中外,其余的 节点的通用属性 以及各个节点类型的特有属性(见内置节点类型)均支持这三种配置方式。

1 实例化图时全局配置

用户在实例化 Graph 时候可以通过 defaultNode 指定 type'ellipse',即可使用 ellipse 节点。

const graph = new G6.Graph({
  container: 'mountNode',
  width: 800,
  height: 600,
  defaultNode: {
    type: 'ellipse',
    // 其他配置
  },
});

2 在数据中动态配置

如果需要使不同节点有不同的配置,可以将配置写入到节点数据中。这种配置方式可以通过下面代码的形式直接写入数据,也可以通过遍历数据的方式写入。

const data = {
  nodes: [{
	  id: 'node0',
    type: 'ellipse',
    ... // 其他配置
    },
    ... // 其他节点
  ],
  edges: [
    ... // 边
  ]
}

配置项说明

ellipse  节点支持 节点通用配置,下表对部分属性进行解释。对于 Object 类型的配置项将在后面有详细讲解:

名称含义类型备注
size椭圆的大小Number / Arraysize 为 Number 时,效果为一个圆形。为 Array 时,size[0] 为椭圆长轴长度,size[1] 为椭圆短轴长度
style椭圆的默认样式ObjectCanvas 支持的属性
label标签文本内容String
labelCfg标签文本配置项Object
stateStyles各状态下的样式Object详见配置状态样式
linkPoints视觉上的四个锚点Object默认不显示,应与 anchorPoints 配合使用。二者区别请看 linkPoints
icon椭圆上 icon 配置Object默认不显示 icon

样式属性 style

Object 类型。支持 节点通用样式。通过 style 配置来修改节点的填充色、描边等属性。下面代码演示在实例化图时全局配置方法中配置 style,使之达到如下图效果。
img

const data = {
  nodes: [
    {
      x: 100,
      y: 100,
      type: 'ellipse',
      label: 'ellipse',
    },
  ],
};
const graph = new G6.Graph({
  container: 'mountNode',
  width: 800,
  height: 600,
  defaultNode: {
    // type: 'ellipse',  // 在数据中已经指定 type,这里可以不用再此指定
    size: [130, 80],
    style: {
      fill: '#bae637',
      stroke: '#eaff8f',
      lineWidth: 5,
    },
  },
});
graph.data(data);
graph.render();

标签文本配置  labelCfg

Object 类型。通过 labelCfg 配置标签文本。支持 节点通用标签配置。基于上面 样式属性 style 中的代码,下面代码在 defaultNode 中增加了  labelCfg  配置项进行文本的配置,使之达到如下图效果。
img

const data = {
  // ... data 内容
};
const graph = new G6.Graph({
  // ... 图的其他属性
  defaultNode: {
    // ... 节点其他属性
    labelCfg: {
      offset: 20,
      style: {
        fill: '#9254de',
        fontSize: 18,
      },
    },
  },
});
// ...

linkPoints

Object 类型。通过配置 linkPoints ,可以指定节点上「上、下、左、右」四个小圆点。

⚠️ 注意: 区分于 anchorPointsanchorPoints 是真正用于指定该节点相关边的连入位置的「数组」,见 anchorPoints;而 linkPoints 仅是指定是否「绘制」出四个圆点,不起实际的连接相关边的作用。二者常常配合使用。
名称含义类型备注
top是否显示上部的圆点Boolean默认为 false
bottom是否显示底部的圆点Boolean默认为 false
left是否显示左侧的圆点Boolean默认为 false
right是否显示右侧的圆点Boolean默认为 false
size圆点的大小Number默认为 3
fill圆点的填充色String默认为 '#72CC4A'
stroke圆点的边框颜色String默认为 '#72CC4A'
lineWidth圆点边框的宽度Number默认为 1

基于上面 样式属性 style 中的代码,下面代码在 defaultNode 中增加了  linkPoints  配置项进行连入点的配置,使之达到如下图效果。
img

const data = {
  // ... data 内容
};
const graph = new G6.Graph({
  // ... 图的其他属性
  defaultNode: {
    // ... 节点其他属性
    linkPoints: {
      top: true,
      bottom: true,
      left: true,
      right: true,
      size: 5,
      fill: '#fff',
    },
  },
});
// ...

图标  icon

Object 类型。通过配置 icon,可以在圆上显示小图标。

名称含义类型备注
show是否显示 iconBoolean默认为 false,不显示
widthicon 的宽度Number默认为 16
heighticon 的高度Number默认为 16
imgicon 的地址或 base64String若配置则表示使用图片作为 icon
texticon 的 iconfontString若配置则表示使用 iconfont 作为 icon

基于上面 样式属性 style 中的代码,下面代码在 defaultNode 中增加了 icon  配置项进行图标的配置,使之达到如下图效果。
img

const data = {
  // ... data 内容
};
const graph = new G6.Graph({
  // ... 图的其他属性
  defaultNode: {
    // ... 节点其他属性
    icon: {
      show: true,
      width: 25,
      height: 25,
      // img: '...', 可更换为其他图片地址
      // text: '...', 使用 iconfont
    },
  },
});
// ...