当前位置: 首页 > news >正文

mxgraph编辑器的使用

前端JS如何使用mxgraph编辑器

说明:此项目是JS项目,目前还不支持TS

引入资源

可以直接从官网上拿下来,或者从其他地方获取

官网编辑器

如果只是展示图形的话只引入 mxClient.js就可以了

一个免费在线编辑器

自己用它做了一个在线编辑器,可以参考一下

当前项目源码

图形资源展示

const graph = new mxGraph(document.getElementById('graph'))
graph.model.clear()
let xmlDoc = mxUtils.parseXml(xml);
let codec = new mxCodec(xmlDoc);
codec.decode(xmlDoc.documentElement, graph.getModel());
graph.setEnabled(false);
// graph.fit(); // 自适应缩放

图形展示图

编辑器

// 编辑器
(function () {
    // 语言资源
    let bundle = 'https://jgraph.github.io/mxgraph/javascript/examples/grapheditor/www/resources/grapheditor.txt';
    // 默认样式
    let defaultThemes='https://jgraph.github.io/mxgraph/javascript/examples/grapheditor/www/styles/default.xml'
    mxUtils.getAll([bundle,defaultThemes ], function (xhr) {
        // 添加语言配置
        mxResources.parse(xhr[0].getText());

        // 配置默认样式
        let themes = {};
        themes[Graph.prototype.defaultThemeName] = xhr[1].getDocumentElement();
        const editor = new EditorUi(new Editor(false, themes), document.getElementById('editor'))
        //初始化完成后可以再进一步配置编辑器,比如添加右键菜单,或者添加工具栏按钮等等
    }, function () {
        document.body.innerHTML = '<center style="margin-top:10%;">Error loading resource files. Please check browser console.</center>';
    });
})()

编辑器图

其他配置

  • 添加一个爪子箭头样式在箭头cell中设置style=endArrow= claw;startArrow= claw;
// 添加爪子箭头, style=endArrow=claw;startArrow=claw;
function createClaw(widthFactor) {
    widthFactor = (widthFactor != null) ? widthFactor : 2;
    return function (canvas, shape, type, pe, unitX, unitY, size, source, sw, filled) {
        let endOffsetX = unitX * sw * 1.118;
        let endOffsetY = unitY * sw * 1.118;
        unitX = unitX * (size + sw);
        unitY = unitY * (size + sw);
        let pt = pe.clone();
        pt.x -= endOffsetX;
        pt.y -= endOffsetY;
        let f = (type != mxConstants.ARROW_CLASSIC && type != mxConstants.ARROW_CLASSIC_THIN) ? 1 : 3 / 4;
        pe.x += -unitX * f - endOffsetX;
        pe.y += -unitY * f - endOffsetY;
        const y = (pt.y === pe.y ? pt.x > pe.x ? -unitX : unitX : 0) / widthFactor;
        const x = (pt.x === pe.x ? pt.y < pe.y ? unitY : -unitY : 0) / widthFactor;
        return function () {
            canvas.begin();
            canvas.moveTo(pe.x, pe.y);
            canvas.lineTo(pt.x, pt.y);
            canvas.moveTo(pe.x, pe.y);
            canvas.lineTo(pt.x + x, pt.y + y);
            canvas.moveTo(pe.x, pe.y);
            canvas.lineTo(pt.x - x, pt.y - y);
            canvas.close();
            canvas.stroke();
        };
    }
}

mxMarker.addMarker('claw', createClaw(1.5));
  • 工具栏中添加保存按钮
const toolbarInit = Toolbar.prototype.init;
Toolbar.prototype.init = function () {
    toolbarInit.apply(this, arguments);
    this.editorUi.menus.put('save', new Menu(() => this.editorUi.actions.get('save').funct(), true))
    // geSprite-save 这是一个样式,官网中没有save图标,可以自定义
    this.addMenu('geSprite-save', mxResources.get('save'), false, 'save', null, true);
}

相关文章:

  • 使用python爬取网络资源
  • 基于数据挖掘从经验方和医案探析岭南名医治疗妇科疾病的诊疗和用药规律
  • NO.57十六届蓝桥杯备战|基础算法-高精度|加减乘除|模拟竖式计算(C++)
  • Live555+Windows+MSys2 编译Androidso库和运行使用(三,实战篇)
  • LINUX基础 [三] - 进程创建
  • vue h5实现车牌号输入框
  • 【CH32V307】教程 + IDE配置
  • Java基于SpringBoot的宽带业务管理系统,附源码+文档说明
  • VSCode中使用Markdown以及Mermaid实现流程图和甘特图等效果
  • 基于数据挖掘的网络入侵检测关键技术研究
  • 设计模式-结构型模式-组合模式
  • 比利时政府网站再遭黑客攻击,我们应当如何应对DDoS?
  • 论文解读:《Word embedding factor based multi-head attention》——基于词嵌入因子的多头注意力
  • hyperf中关于时间的设定
  • 【设计模式】代理模式(Proxy Pattern)详解
  • C++:类和对象(一)
  • css—— object-fit 属性
  • ENSP学习day10
  • 建立虚拟用户的账号数据库并为vsftpd服务器添加虚拟用户支持的脚本
  • vcd波形转仿真激励
  • wordpress主页不显示/吉安seo网站快速排名
  • 免费的黄金网站有哪些/关键词快速上首页排名
  • 建设电子商务网站的规划书/搜狗收录提交
  • 网站建设与维护案列/广东seo推广贵不贵
  • 网站开发架构师/网页生成器
  • 在线修图网站/淮安网站seo