mxgraph中mxStencil使用经验

简介: mxgraph中mxStencil使用经验

mxgraph中mxStencil使用经验

官方文档:https://jgraph.github.io/mxgraph/docs/js-api/files/shape/mxStencil-js.html

标签嵌套关系

<!--基本结构-->
<shapes>
 <shape name="linearrow" h="1" w="4" aspect="variable" strokewidth="inherit">
     <!--连接点-->
    <connections>
      <constraint x="0.5" y="0" perimeter="1" name="A" />
      <constraint x="0.5" y="1" perimeter="1" name="B" />
    </connections>
    <!--背景-->
    <background>
    </background>
    <!--前景-->
    <foreground>
    </foreground>
 </shape>
</shapes>

Shapes

相当于一个集合,里面可以包含多个shape图形

shape

一个shape相当于一个图形,相关参数:

参数 解释 类型 值
name 图形名称 string 自定义
w 图形宽度 string 自定义,默认值为100
h 图形高度 string 自定义,默认值为100
aspect 图形比率 string 1.variable(默认值)表示调整大小图形会以比率缩放,缩放使图形宽高会改变 2.fixed(固定)图形默认比率大小不能改变,缩放不会改变图形的宽高
strokewidth 线条 string 默认值为1。"inherit"表示线条宽度仅在缩放时更改,而不是在调整大小时更改。如果使用数值,则在缩放和调整大小时都会改变线条宽度。这个参数要在后面每个线段前面加上strokewidth标签赋值配合使用才能有效果

注意:mxStencil中图形的坐标不是按照普通x,y相对于原点来的,这里x、y坐标是以整个页面的左上角为原点,到图形的左上角的距离。

connections

连接点是用于和其他图形建立连接的点,图下蓝色的小X

连接点的定义方式:

参数 解释 值
perimeter 周长 0表示由x、y指定的连接点,1表示使连接点的位置从形状左上角,通过x、y到与形状周长相交的点
x、y 连接点的位置 周长为0时,x、y是相对坐标最大值为1。周长为0时,由图形坐标点出发经过x、y坐标最后与边框相交
name 连接点的名称 形状上端口的唯一标识符

实验参数perimeter="0"的设置原理, 设置 两个连接点

<constraint x="0" y="0.5" perimeter="0" name="!" />
<constraint x="1" y="0.5" perimeter="0" name="!" />

以图形左上角为原点,(0,0)是左上角,(0,0.5)左边中心,(1,0.5)是右边界的中心,值可能小于0或大于1,以定位在形状外部。

实验参数perimeter="1"的设置原理,设置x=2.5,y=3.7,已知图形的宽为5.7,高为7.5,那么x、y所处的位置就是红色方框标记,也就是改图形的中心点,两点连成一条线,由图形坐标点出发经过x、y坐标最后与边框相交的就是连接点的位置。

background

背景通常可以说是图形的形状的线,也可以说是其他形状的图形。

背景只能包含:笔划(path)、封闭路径(自定义的形状)、矩形、圆矩形或椭圆元素。不能包括图像、文本或包含形状

Move:用于绘制一个起点

line:用于描述终点,Move和line表示连成一条线,line的一直使用表示笔划不中断

close:把起点和终点连起来闭合,常见于弧线,封闭图形里面

<path>
        <move x="0" y="10" />
        <line x="10" y="10" />
  </path>
<path>
        <move x="2.79999999999991" y="0.700000000000114" />
        <line x="0.699999999999886" y="6.81676937119846E-14" />
        <line x="0.7" y="6.81676937119846E-14" />
        <line x="0.7" y="1.40000000000016" />
        <line x="0.699999999999886" y="1.40000000000016" />
        <line x="2.79999999999991" y="0.700000000000114" />
        <close />
  </path>

arc:

<path>
        <move x="x1" y="y1" />
        <arc rx="0.7" ry="0.7" x-axis-rotation="" large-arc-flag="" sweep-flag="" x="x2" y="y2" />
</path>
  • x1、y1:起点
  • x2、y2:终点
  • rx、ry:中心点
  • x-axis-rotation:图形旋转角度(0~360)
  • sweep-flag:弧线方向 0表示逆时针 1表示顺时针
  • large-arc-flag:起点和终点连成一条线做这个线的中垂线,根据中心点,就有四段弧
    这里要讲的参数是large-arc-flag(角度大小) 和sweep-flag(弧线方向),large-arc-flag决定弧线是大于还是小于180度,0表示小角度弧,1表示大角度弧。sweep-flag表示弧线的方向,0表示从起点到终点沿逆时针画弧,1表示从起点到终点沿顺时针画弧。下面的例子展示了这四种情况。

  • quad:二次贝塞尔曲线
  • curve:三次贝塞尔曲线

foreground

前景可以包含:封闭路径(阴影)、文字等等,如果有填充可以放在这部分

文字:

  • str:需要显示的文本字符串。
  • x、y:文本元素的十进制位置(x,y),必需。
  • align:文本元素的水平对齐,可以是“left”、“center”或“right”。可选,默认为“左”。
  • valign:文本元素的垂直对齐,可以是“top”、“middle”或“bottom”。可选,默认为“top”。
  • localized:0或1,如果是1,“str”实际上包含一个键,用于从mxResources中获取值。可选,默认值为mxStencil.defaultLocalized。
  • vertical:0或1,如果为1,则垂直渲染标签(旋转90度)。可选,默认值为0。
  • rotation:单位为度(0到360)。旋转文本的角度。可选,默认值为0。
  • align-shape:或1,如果0,则在设置文本旋转时忽略形状的旋转。可选,默认值为1。

其他样式

在当前状态中更改颜色的元素都采用哈希前缀的十六进制颜色代码ffae80

  • strokecolor,这将设置在发出笔划或圆角命令时图形路径将呈现的颜色。即线条的颜色
<strokecolor color="#000000" />
 <path>
    <move x="0" y="3.5" />
    <line x="22" y="3.5" />
 </path>
<stroke/>
  • fillcolor,这将设置在发出“填充”或“圆角笔划”命令时,闭合路径内部将呈现的颜色。
<fillcolor color="#000000" />
      <path>
        <move x="0.61" y="1.69937846768753" />
        <arc rx="1.78" ry="1.7" x-axis-rotation="0" large-arc-flag="0" sweep-flag="1" x="4.19" y="1.6" />
      </path>
<fillstroke />
  • fillcolor要和fillstroke一起配合使用才有效
  • fontcolor,这设置了在绘制文本时字体渲染的颜色。
<fontcolor color="#000000" />
<text str="RTOP" x="22.18" y="9.42" rotation="0" />
  • alpha定义了在1.0和0.0之间使用的完全不透明透明度程度,而完全透明时使用的透明度等级。
  • fillalpha定义了在1.0和0.0之间使用的完全不透明填充透明度的程度,以及完全透明的0.0。
  • Strokeapha定义了在1.0之间使用的完全不透明笔划透明度的程度,以及为完全透明而使用的0.0之间的笔划透明度。
  • strokewidth定义通过抚摸渲染的图形图元的整数厚度。使用fixed=“1”按原样应用该值,而不必缩放。
  • dashed 为“1”,表示启用破折号,“0”表示禁用。启用虚线时,当前的划线图案(由dashpattern定义)将用于笔划。dashpattern是一个间隔的“开,关”长度序列,定义绘制笔划的距离, 就是指画短划线的时候,第一笔长3个单位,空白1个单位,接下来长2个单位,再空5个单位,如此循环,dashpattern可以设置虚线的样式。例如:
<strokewidth width="0"/>
<dashed dashpattern="3 1 2 5" />
  <path>
      <move x="22" y="7"/>
      <line x="0" y="7"/>
 </path>
<stroke />

字体样式:

  • fontsize,整数,
  • fontstyle,一个粗体(1)、斜体(2)和下划线(4)的ORed位模式,即粗体下划线为“5”
  • fontfamily是一个字符串,定义要使用的字体
<fontsize size="2" />
<fontcolor color="#000000" />
<text str="CONTROLNAME" x="1.63" y="6.03" rotation="0" />

新增标签

  1. 插入点标签
<insert x="1" y="0"></insert>
  1. 连接点状态
<constraint x="0" y="0" perimeter="1" name="!" Inflow="0" />

Inflow为0代表只能流入 1代表只能流出 2代表都可使用

图形内部颜色绘制

第一种:在foreground前景中绘制一段路径path,在path前面加上fillcolor用于表示封闭路径的颜色,最后在path后面加上fillstroke表示这是填充笔画。

<foreground>
     <!--渲染图形外部路径的颜色-->
     <strokecolor color="#000000" />
     <!--填充的颜色-->
      <fillcolor color="#00ffff" />
      <path>
        <move x="0.61" y="1.69937846768753" />
        <arc rx="1.78938082222999" ry="1.78938082222999" x-axis-rotation="0" large-arc-flag="0" sweep-flag="1" x="4.19" y="1.69937846768753" />
      </path>
      <fillstroke />
</foreground>

第二种:是绘制了一段封闭路径,然后给封闭路径加上了填充颜色。

<fillcolor color="#00ffff" />
      <path>
        <move x="2.4" y="1.08" />
        <line x="4.55186899564266" y="2.155934497821" />
        <line x="4.55186899564266" y="0.0040655021789977" />
        <line x="2.4" y="1.08" />
        <close />
      </path>
      <fill />

封闭线段绘制

<path>
        <move x="4.79999999999988" y="4.0998037856843" />
        <line x="2.4" y="5.26937846768752" />
        <line x="2.92038760422274" y="5.52297520382388" />
        <line x="4.79999999999988" y="6.43895314969076" />
        <line x="4.79999999999988" y="4.0998037856843" />
        <close />
  </path>

设置一条线的颜色大小

<background>
    <!--渲染图形路径的颜色-->
   <strokecolor color="#ffff00" />
     <!--渲染线的宽度-->
   <strokewidth width="0" />
     <!--设置线的样式-->
   <dashed dashed="0" />
    <path>
        <move x="1.04" y="2.1" />
        <line x="1.04" y="3.59" />
    </path>
    <stroke/>
</background>

样例

<shape aspect="variable" h="92" name="4 Point Star" strokewidth="inherit" w="92">
    <connections>
        <constraint name="N" perimeter="0" x="0.5" y="0"/>
        <constraint name="S" perimeter="0" x="0.5" y="1"/>
        <constraint name="W" perimeter="0" x="0" y="0.5"/>
        <constraint name="E" perimeter="0" x="1" y="0.5"/>
    </connections>
    <background>
        <path>
            <move x="46" y="0"/>
            <line x="56" y="36"/>
            <line x="92" y="46"/>
            <line x="56" y="56"/>
            <line x="46" y="92"/>
            <line x="36" y="56"/>
            <line x="0" y="46"/>
            <line x="36" y="36"/>
            <close/>
        </path>
    </background>
    <foreground>
        <fillstroke/>
    </foreground>
</shape>

<shape name="jcontvvncwd" h="6.46937846768753" w="4.8" aspect="variable" strokewidth="inherit">
    <connections>
      <constraint x="0" y="0.74" perimeter="1" name="!" />
      <constraint x="1" y="0.74" perimeter="1" name="!" />
    </connections>
    <background>
      <strokecolor color="#ffff00" />
      <strokewidth width="0" />
      <dashed dashed="0" />
      <path>
        <move x="0" y="6.46937846768753" />
        <line x="4.8" y="4.06937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0" />
      <dashed dashed="0" />
      <path>
        <move x="4.8" y="6.46937846768753" />
        <line x="0" y="4.06937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="0" y="4.06937846768753" />
        <line x="0" y="6.46937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="0" y="4.06937846768753" />
        <line x="4.8" y="4.06937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="4.8" y="4.06937846768753" />
        <line x="4.8" y="6.46937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="0" y="6.46937846768753" />
        <line x="4.8" y="6.46937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="0" y="6.46937846768753" />
        <line x="4.8" y="4.06937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="4.8" y="4.06937846768753" />
        <line x="4.8" y="6.46937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="4.8" y="6.46937846768753" />
        <line x="0" y="4.06937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="0.61" y="1.69937846768753" />
        <line x="2.4" y="1.69937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="2.4" y="1.69937846768753" />
        <line x="4.19" y="1.69937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0.0200080032012812" />
      <dashed dashed="0" />
      <path>
        <move x="2.4" y="1.69937846768753" />
        <line x="2.4" y="5.26937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0" />
      <dashed dashed="0" />
      <path>
        <move x="4.8" y="4.09937846768753" />
        <line x="4.8" y="6.43937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0" />
      <dashed dashed="0" />
      <path>
        <move x="0" y="4.09937846768753" />
        <line x="0" y="6.43937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0" />
      <dashed dashed="0" />
      <path>
        <move x="0" y="4.09937846768753" />
        <line x="2.92" y="5.51937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0" />
      <dashed dashed="0" />
      <path>
        <move x="4.8" y="4.09937846768753" />
        <line x="1.88" y="5.51937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0" />
      <dashed dashed="0" />
      <path>
        <move x="2.92" y="5.51937846768753" />
        <line x="4.8" y="6.43937846768753" />
      </path>
      <stroke />
      <strokecolor color="#ffff00" />
      <strokewidth width="0" />
      <dashed dashed="0" />
      <path>
        <move x="1.88" y="5.51937846768753" />
        <line x="0" y="6.43937846768753" />
      </path>
      <stroke />
    </background>
    <foreground>
      <strokecolor color="#ffffff" />
      <fillcolor color="#000000" />
      <path>
        <move x="0.61" y="1.69937846768753" />
        <arc rx="1.78938082222999" ry="1.78938082222999" x-axis-rotation="0" large-arc-flag="0" sweep-flag="1" x="4.19" y="1.69937846768753" />
      </path>
      <fillstroke />
      <insert x="2.4" y="5.26937846768753">
      </insert>
      <fillcolor color="#ffff00" />
      <path>
        <move x="1.17239551400417E-13" y="4.0998037856843" />
        <line x="1.17239551400417E-13" y="6.43895314969076" />
        <line x="1.87961239577726" y="5.52297520382388" />
        <line x="2.4" y="5.26937846768752" />
        <line x="1.17239551400417E-13" y="4.0998037856843" />
        <close />
      </path>
      <fill />
      <fillcolor color="#ffff00" />
      <path>
        <move x="4.79999999999988" y="4.0998037856843" />
        <line x="2.4" y="5.26937846768752" />
        <line x="2.92038760422274" y="5.52297520382388" />
        <line x="4.79999999999988" y="6.43895314969076" />
        <line x="4.79999999999988" y="4.0998037856843" />
        <close />
      </path>
      <fill />
    </foreground>
</shape>



相关文章
|
存储 SQL 分布式计算
|
SQL 关系型数据库 MySQL
一篇文章解析mysql的 行转列(7种方法) 和 列转行
一篇文章解析mysql的 行转列(7种方法) 和 列转行
4058 0
|
6月前
|
人工智能 前端开发 Java
让AI Coding 效率翻倍!AGENTS.md 标准化文档详解:兼容Claude/Cursor/Codex全方案
在多AI编程工具并行使用的团队开发场景中,长期存在一个普遍痛点:不同AI客户端各自使用独立规则配置文件,Claude依赖CLAUDE.md、Cursor使用.cursorrules、Codex采用AGENT.md,团队需要同步多份内容高度重合的文档,维护成本成倍上涨。同时AI无法完整理解项目整体架构,前后端代码上下文割裂、私有组件不识别、编码规范不清晰、无法自主构建自测等问题,导致AI生成代码返工率极高,大幅拉低开发效率。
1388 0
|
8月前
|
机器学习/深度学习 缓存 测试技术
DeepSeek-V4开源:百万上下文,Agent能力比肩顶级闭源模型
DeepSeek-V4正式开源!含V4-Pro(1.6T参数)与V4-Flash(284B参数)双版本,均支持百万token上下文。首创混合注意力架构,Agent能力、世界知识与推理性能全面领先开源模型,数学/代码评测比肩顶级闭源模型。
8675 12
|
8月前
|
人工智能 运维 安全
基于端侧 AI 与云边协同的电力智能巡检解决方案实践
电力巡检面临人工依赖高、效率低、安全风险大、数据难闭环等痛点。本方案基于云-边-端协同架构,融合端侧轻量化AI、多模态感知与弱网适配技术,打造工业级AR智能安全帽,实现变电站、输电线路等场景的智能识别、实时预警、全程追溯与安全管控,助力电网数字化升级。
1410 0
基于端侧 AI 与云边协同的电力智能巡检解决方案实践
|
SQL 人工智能 搜索推荐
MCP这个协议,如何让大模型从‘能说’迈向‘能做’?
本文由三桥君撰写,深入解析MCP(模型上下文协议)如何助力大模型从“能说”到“能做”,涵盖其核心原理、工作机制与多场景应用,为AI产品经理提供系统化知识与实践指导。
1670 61
|
Oracle 关系型数据库 数据库
oracle日期加减的三种方式
【8月更文挑战第15天】在Oracle数据库中,可通过三种方式对日期进行加减操作:一是利用`ADD_MONTHS`函数增减月份,如`ADD_MONTHS(SYSDATE, 2)`表示两个月后;二是运用算术运算符直接加减天数,如`SYSDATE + 7`表示七天后;三是采用`INTERVAL`表达式增加或减少特定时间间隔,如`SYSDATE + INTERVAL &#39;2&#39; YEAR`表示两年后。这些方法可根据实际需求灵活选用。
5450 5
|
前端开发 数据可视化 JavaScript
惊喜! Github 10k+ star 的国产流程图框架,LogicFlow 能解你的图编辑痛点?
LogicFlow 是一款高效、灵活的流程图编辑框架,支持可视化渲染、自定义节点、插件扩展及前端执行。适用于审批流、ER 图、低代码平台等多种场景,具备清晰架构与活跃社区,助力开发者快速实现专业流程图编辑与执行。
3066 1
Visual Studio 快速分析 .NET Dump 文件
【11月更文挑战第10天】.NET Dump 文件是在 .NET 应用程序崩溃或出现问题时生成的,记录了应用程序的状态,包括内存对象、线程栈和模块信息。通过分析这些文件,开发人员可以定位和解决内存泄漏、死锁等问题。在 Visual Studio 中,可以通过调试工具、内存分析工具和符号加载等功能来详细分析 Dump 文件。此外,还可以使用第三方工具如 WinDbg 进行更深入的分析。
1677 1
|
自然语言处理 前端开发 JavaScript
🔥3.4K star!阿里出品对话式UI神器,轻松打造专业级聊天界面!
ChatUI 是阿里巴巴团队开源的对话式 UI 设计语言与 React 组件库,专为构建智能对话界面而生。它凝聚了阿里在智能客服领域的最佳实践,提供“最新、最全、最优质”的开源项目和高效工作学习方法。核心功能亮点包括智能消息流处理、无障碍访问认证、企业级主题定制、多端自适应布局、国际化双引擎和智能输入辅助。技术架构基于 React 17 + TypeScript 4,支持 Less + CSS Variables 样式方案,Rollup + Babel 构建工具,以及 Jest + React Testing Library 测试体系。
4042 5