百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

11.mxGraph的mxCell和Styles样式(graph style)

zhezhongyun 2025-07-09 00:20 39 浏览

3.1.3 mxCell

[翻译]
mxCell 是顶点和边的单元对象。mxCell 复制了模型中可用的许多功能。使用上的关键区别是,使用模型方法会创建适当的事件通知和撤销,而使用单元进行更改时没有更改记录。这对于临时视觉效果(如动画或鼠标悬停时的更改)可能有用。但作为一般规则,除非遇到此机制的特定问题,否则使用模型编辑 API。

[原文]
mxCell is the cell object for both vertices and edges. mxCell duplicates many of the functions available in the model. The key difference in usage is that using the model methods creates the appropriate event notifications and undo, using the cell makes the change but there is no record of the change. This can be useful for temporary visual effects such as animations or changes on a mouse over, for example. As a general rule though, use the model editing API unless you encounter a specific problem with this mechanism.

  • duplicates /'djuplkets/ 复制
  • notifications /notf'kenz/ 通知
  • undo /n'du/ 撤销
  • temporary /'tempreri/ 临时的
  • animations /aen'menz/ 动画

[翻译]
在创建新单元时,构造函数中需要三件事:值(用户对象)、几何和样式。我们现在将探讨这三个概念,然后返回到单元。

[原文]
When creating a new cell, three things are required in the constructor, a value (user object), a geometry and a style. We will now explore these 3 concepts before returning to the cell.

  • constructor /kn'strktr/ 构造函数
  • value /'vaelju/ 值
  • geometry /di'mtri/ 几何
  • style /stal/ 样式
  • explore /k'splr/ 探讨

3.1.3.1 Styles 样式

[翻译]
样式和样式表的概念在概念上类似于 CSS 样式表,但请注意,mxGraph 中实际使用 CSS,但仅用于影响 HTML 页面 DOM 中的全局样式。打开 util.mxConstants.js 文件,在编辑器中搜索“STYLE_”的第一次匹配。如果您向下滚动,将看到大量以该前缀定义的字符串,用于所有可用样式。一些样式适用于顶点,一些适用于边,一些两者都适用。如您所见,这些定义了作用于元素的视觉属性。

[原文]
The concept of styles and stylesheets in conceptually similar to CSS stylesheets, though note that CSS are actually used in mxGraph, but only to affect global styles in the DOM of the HTML page. Open up the util.mxConstants.js file in your editor and search for the first match on “STYLE_”. If you scroll down you will see a large number of strings defined for all the various styles available with this prefix. Some of styles apply to vertices, some to edges and some to both. As you can see, these define visual attributes on the element they act upon.

  • stylesheets /'stalits/ 样式表
  • CSS /si es 'es/ 层叠样式表
  • DOM /di o 'em/ 文档对象模型
  • prefix /'prifks/ 前缀
  • attributes /'aetrbjuts/ 属性

[翻译]
mxStylesheet 持有一个对象 styles,这是一个将样式名称映射到样式数组的哈希表:

[原文]
The mxStylesheet holds one object, styles, which is a hashtable mapping style names to an array of styles:

  • holds /holdz/ 持有
  • hashtable /'haetebl/ 哈希表
  • mapping /'maep/ 映射
  • array /'re/ 数组
  • styles /stalz/ 样式

样式集合中的样式数组 /Style arrays within the styles collection

  • collection /k'lekn/ 集合

[翻译]
在上图中,蓝色框表示 mxStyleSheet 中的样式哈希表。字符串 'defaultVertex' 是键,对应一个字符串/值对的数组,这些是实际的样式。请注意,mxGraph 创建了两个默认样式,一个用于顶点,一个用于边。如果您回顾 helloworld 示例,没有样式传递到 insertVertex 或 insertEdge 的可选样式参数。在这种情况下,将使用这些单元的默认样式。

[原文]
In the above image the blue box represents the styles hashtable in mxStyleSheet. The string 'defaultVertex' is the key to an array of string/value pairs, which are the actual styles. Note that mxGraph creates two default styles, one for vertices and one for edges. If you look back to the helloworld example, no style was passed into the optional style parameter of insertVertex or insertEdge. In this case the default style would be used for those cells.

  • represents /repr'zents/ 表示
  • hashtable /'haetebl/ 哈希表
  • default /d'flt/ 默认
  • optional /'ɑpnl/ 可选的
  • parameter /p'raemtr/ 参数

设置单元的样式/Setting the Style of a Cell

[翻译] 如果您想为单元指定除默认样式外的其他样式,您必须在创建单元时(mxGraph 的 insertVertex 和 insertEdge 都有一个可选参数)或使用 model.setStyle() 将该样式传递给单元。

[原文] If you wanted to specify a style other than the default for a cell, you must pass that new style either to the cell when it is created (mxGraph's insertVertex and insertEdge both have an optional parameter for this) or pass that style to the cell using model.setStyle().

  • specify /'spesfa/ 指定
  • style /stal/ 样式
  • created /kri'etd/ 创建
  • optional /'ɑpnl/ 可选的
  • pass /paes/ 传递

[翻译]
您传递的样式格式为 stylename。注意,样式名称和键/值对可以以任何顺序出现。以下是展示此概念的示例,改编自我们在 helloworld 中看到的 insertVertex 调用:

[原文]
The style that you pass has the form stylename. ,note that the stylenames and key/value pairs may be in any order. Below are examples to demonstrate this concept, adapting the insertVertex call we saw in helloworld:

  • format /'frmaet/ 格式
  • stylename /'stalnem/ 样式名称
  • key/value /ki 'vaelju/ 键/值
  • adapting /'daept/ 改编
  • demonstrate /'demnstret/ 展示

[翻译]
1.已创建了一个名为 'ROUNDED' 的新样式,要将其应用于顶点:

[原文]
1.A new style called 'ROUNDED' has been created, to apply this to a vertex:

var v1 = graph.insertVertex(parent, null, 'Hello', 20, 20, 80, 30, 'ROUNDED');
  • created /kri'etd/ 创建
  • apply /'pla/ 应用
  • vertex /'vrtks/ 顶点

[翻译]
2.要创建具有 ROUNDED 样式的新顶点,覆盖描边和填充颜色:

[原文]
2.To create a new vertex with the ROUNDED style, overriding the stroke and fill colors:

var v1 = graph.insertVertex(parent, null, 'Hello',  20, 20, 80, 30, 'ROUNDED;strokeColor=red;fillColor=green');
  • overriding /ovr'rad/ 覆盖
  • stroke /strok/ 描边
  • fill /fl/ 填充

[翻译]
3.要创建没有全局样式但具有本地描边和填充颜色的新顶点:

[原文]
3.To create a new vertex with no global style, but with local stroke and fill colors:

var v1 = graph.insertVertex(parent, null, 'Hello', 20, 20, 80, 30, ';strokeColor=red;fillColor=green');
  • global /'ɡlobl/ 全局的
  • local /'lokl/ 本地的
  • stroke /strok/ 描边

[翻译]
4.要创建使用 defaultVertex 样式但具有本地填充颜色的顶点:

[原文]
4.To create a vertex that uses the defaultVertex style, but a local value of the fill color:

var v1 = graph.insertVertex(parent, null, 'Hello', 20, 20, 80, 30, 'defaultVertex;fillColor=blue');
  • default /d'flt/ 默认
  • local /'lokl/ 本地的
  • fill /fl/ 填充

[翻译]
请注意,在这种情况下必须明确命名默认样式,省略样式会在字符串以分号开始时为单元设置无全局样式。如果字符串开头没有分号,则使用默认样式。

[原文]
Note that default style must be explicitly named in this case, missing the style out sets no global style on the cell when the semi-colon starts the string. If the string starts with no semi-colon, the default style is used.

  • explicitly /k'splstli/ 明确地
  • semi-colon /'sem koln/ 分号
  • default /d'flt/ 默认
  • global /'ɡlobl/ 全局的
  • style /stal/ 样式

[翻译]
同样,mxGraph 类提供了实用函数,形成访问和更改单元样式的核心 API:

核心 API 函数:


  • mxGraph.setCellStyle(style, cells) – 在 begin/end 更新调用内为单元数组设置样式。
  • mxGraph.getCellStyle(cell) – 返回指定单元的样式,合并任何本地样式和该单元类型的默认样式。

[原文]
Again, the mxGraph class provides utility functions that form the core API for accessing and changing the styles of cells:

Core API functions:

  • mxGraph.setCellStyle(style, cells) – Sets the style for the array of cells, encapsulated in a begin/end update.
  • mxGraph.getCellStyle(cell) – Returns the style for the specified cell, merging the styles from any local style and the default style for that cell type.

  • utility /ju'tlti/ 实用
  • accessing /'aekses/ 访问
  • encapsulated /n'kaepsjletd/ 封装
  • merging /'mrd/ 合并
  • default /d'flt/ 默认

创建新的全局样式/Creating a New Global Style

[翻译]
要创建上述描述的 ROUNDED 全局样式,您可以遵循以下模板创建样式并将其注册到 mxStyleSheet:

[原文] To create the ROUNDED global style described above, you can follow this template to create a style and register it with mxStyleSheet:

var style = new Object();
style[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_RECTANGLE;
style[mxConstants.STYLE_OPACITY] = 50;
style[mxConstants.STYLE_FONTCOLOR]= '#774400';
graph.getStylesheet().putCellStyle('ROUNDED',style);
  • global /'ɡlobl/ 全局的
  • template /'templt/ 模板
  • register /'redstr/ 注册
  • opacity /o'paesti/ 不透明度
  • font /fnt/ 字体

相关推荐

Opinion丨Struggle Against U.S. Mind colonization in the Global South

Editor'snote:Thismonth,XinhuaNewsAgency'sThinkTankreleasedareporttitled"Colonizationof...

爱可可AI论文推介(2020.11.4)_爱可可女装旗舰店

LG-机器学习CV-计算机视觉CL-计算与语言AS-音频与语音RO-机器人(*表示值得重点关注)1、[LG]*CombiningLabelPropagationan...

何新:罗马伪史考英文版序言_罗马史学

2019-10-2514:48:27何新:罗马伪史考序言(英文译本)HeXin:PreambleofResearchonPseudo-historyofRome1Afewyear...

XPeng Stock Rises Over 4% after Q2 Revenue and EV Margin Set Records

TMTPOST--TheAmericandepositaryreceipts(ADRs)ofXPengInc.rosearound4.2%onTuesdayaftert...

英汉世界语部首(八)_英文部首字典

本节讲八个部首,分别是:弓gōng【ECWLrad】bow廾gǒng【ECWLrad】twen广guǎng【ECWLrad】vast己jǐ【ECWLrad】self已yǐ...

一课译词:划水_划水是什么地方的方言

[Photo/SIPA]懒惰是人类的天性,因此才总有人会在工作时“划水”。“划水【huáshuǐ】”,本意是指“用胳膊划的动作(makestrokeswithone’sarms)”,延伸为“...

首测!GPT-4o做Code Review可行吗?

编辑|言征出品|51CTO技术栈(微信号:blog51cto)近日,OpenAI一记重拳,推出了GPT-4o(“o”表示“omni”),将语音识别和对话方面的优势展示的淋漓尽致。几乎可以肯定,...

C++|漫谈STL细节及内部原理_c++ stl详解

1988年,AlexanderStepanov开始进入惠普的PaloAlto实验室工作,在随后的4年中,他从事的是有关磁盘驱动器方面的工作。直到1992年,由于参加并主持了实验室主任BillWo...

C++ inline关键字深度解析:不止于优化的头文件定义许可

在C++开发中,几乎每个程序员都用过inline关键字,但多数人只停留在“内联优化”的表层理解。事实上,inline的真正威力在于它打破了C++的单一定义规则(ODR)限制,成为头文件中安全定义函数的...

实用 | 10分钟教你搭建一个嵌入式web服务器

之前分享的文章中提到了几种可以在嵌入式中使用的web服务器。嵌入式web服务器就是把web服务器移植到嵌入式系统的服务器。它仍然是基于http文本协议进行通信的,具有标准的接口形式,对客户端...

中间语言格式_中间格式文本是什么

在通常情况下,编译器会将目标语言转换成某种中间语言格式,而不是直接将源代码转换成二进制机器指令,不少c语言编译器,都会将代码编译成汇编语言,然后再通过汇编语言编译器将汇编代码转换成目标机器可执行的二进...

一线开发大牛带你深度解析探讨模板解释器,解释器的生成

解释器生成解释器的机器代码片段都是在TemplateInterpreterGenerator::generate_all()中生成的,下面将分小节详细展示该函数的具体细节,以及解释器某个组件的机器代码...

干货,Web开发和前端开发逆天工具大全

微信ID:WEB_wysj(点击关注)◎◎◎◎◎◎◎◎◎一┳═┻︻▄(点击页底“阅读原文”前往下载)●●●逆天工具CDN资源库国内Bootstrap中文网开源项目免费CDN服务36...

移动端rem+vw适配_移动端web页面适配方案

rem:rem是相对单位,设置根元素html的font-size,比如给html设置字体大小为100px,1rem=100px;rem缺点:1.和根元素font-size值强耦合,系统字...

从零搭建 React 开发 H5 模板_react html5

项目创建创建项目文件夹mkdir react-democd react-demonpm init -y依赖安装yarn add rea...