mxGraph简单操作。

工作需要使用mxGraph。依着葫芦画瓢慢慢学,不足之处还望大家指点。

function main(container) {
    if (!mxClient.isBrowserSupported()) {
        mxUtils.error('Browser is not supported!', 200, false);
    } else {
        var graph = new mxGraph(container);//创建视图
        graph.setHtmlLabels(true);
        graph.setTolerance(20);
        graph.setEnabled(false); // 禁用选择和单元格处理

        var style = graph.getStylesheet().getDefaultVertexStyle();
        // style[mxConstants.STYLE_VERTICAL_ALIGN] = mxConstants.ALIGN_BOTTOM;//文字对齐方式
        // style[mxConstants.STYLE_FILLCOLOR] = 'rgb(251, 148, 79)'; //填充色 
        style[mxConstants.STYLE_FONTSIZE] = 14; //文字大小
        style[mxConstants.STYLE_FONTCOLOR] = "#fff"; //文字颜色
        style[mxConstants.STYLE_WHITE_SPACE] = 'wrap'; //自动换行
        delete style[mxConstants.STYLE_STROKECOLOR]; //去掉边框
        //鼠标拖动
        graph.setAutoSizeCells(true);
        graph.setPanning(true);
        graph.panningHandler.useLeftButtonForPanning = true;

        graph.setCellsResizable(false); // 禁止改变元素大小
        mxEvent.disableContextMenu(container); // 禁用浏览器默认的右键菜单栏

        // 缩放
        mxEvent.addMouseWheelListener(function(evt, up) {
            if (up) {
                graph.zoomIn();
            } else {
                graph.zoomOut();
            }
            mxEvent.consume(evt);
        });

        style = graph.getStylesheet().getDefaultEdgeStyle();
        style[mxConstants.STYLE_EDGE] = mxEdgeStyle.TopToBottom;
        style[mxConstants.STYLE_STROKECOLOR] = 'rgb(115, 121, 133)'; //连接线颜色
        delete graph.getStylesheet().getDefaultEdgeStyle()['endArrow']; //去掉箭头

        graph.addListener(mxEvent.DOUBLE_CLICK, function(sender, evt) {
            var cell = evt.getProperty('cell');
            console.log(cell)
        }); //双击事件

        var parent = graph.getDefaultParent();
        graph.getModel().beginUpdate();
        try {
            var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30); //生成模块
            var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
            graph.insertEdge(parent, null, ' ', v2, v1); //连接两个模块
        } finally {
            graph.getModel().endUpdate();
        }
    }
}
20170113201156.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,653评论 25 709
  • 一不小心,2017年就过去一半了。回顾年初制定的雄心勃勃的计划,相信不少人又要伤感了。 时间是最公平的,每个人一天...
    职优易阅读 1,703评论 0 1
  • 火车就要来了,爸爸坐的火车…… 秋天开始的时候,我和妈妈般到了这座小城,从那以后我一直都没有见过爸爸。 不过,今天...
    善于Harbin阅读 3,680评论 5 1
  • 现在你应该还没有睡觉呢。我猜你已经到了上海的机场。 今天有点特殊。我好像又失眠了。失眠的声音就像一段缓慢的河流。我...
    一只特立独行的isabe猪阅读 3,860评论 3 0
  • 闲也似忙~~老了没瞌睡,只把天亮盼,早起喝茶吃馒头,假装要去忙。提把遮阳伞 ,夹个公文包,走到护城河边站,抽烟发个呆。
    瘦人碗子阅读 2,118评论 0 1