首页 > 解决方案 > 用 XML 在 mxGraph 中画线?

问题描述

我使用这个 mxGraph 编辑器的例子

我正在尝试使用该库来制作梯形图编辑器,所以我想绘制起点和终点线,但我需要它们作为图表区域从左到右的边界(每次都从编辑器开始) ,我该怎么做?如何让它们连接到元素?

有人可以帮忙吗?

解释图像:我想在图片中做什么

编辑

这段代码画了一条线,然后我用几何属性定位它。

var xml = "<root>"+
                "<MyObject id=\"2\">"+
                "<mxCell style=\"strokeColor=black;fillColor=black\" parent=\"1\" vertex=\"1\" type = 'startLine'>"+
                "<mxGeometry x=\"0\" y=\"0\" width=\"2\" height=\"119\" as=\"geometry\"/>"+
                "</mxCell></MyObject></root>";
        doc = mxUtils.parseXml(xml),
        codec = new mxCodec(doc),
        elt = doc.documentElement.firstChild,
        cells = [];

        while (elt != null){                
            cells.push(codec.decodeCell(elt));
            elt.setAttribute('attribute1', 'value1');
            $this.editor.graph.refresh();
            elt = elt.nextSibling;
        }
        $this.editor.graph.addCells(cells);

标签: javascriptmxgraphjgraphdraw.io

解决方案


为了将问题标记为“已回答”,以便其他人可以看到:

做这个 :

var xml = "<root>"+
                "<MyObject id=\"2\">"+
                "<mxCell style=\"strokeColor=black;fillColor=black\" parent=\"1\" vertex=\"1\" type = 'startLine'>"+
                "<mxGeometry x=\"0\" y=\"0\" width=\"2\" height=\"119\" as=\"geometry\"/>"+
                "</mxCell></MyObject></root>";
        doc = mxUtils.parseXml(xml),
        codec = new mxCodec(doc),
        elt = doc.documentElement.firstChild,
        cells = [];

        while (elt != null){                
            cells.push(codec.decodeCell(elt));
            elt.setAttribute('attribute1', 'value1');
            $this.editor.graph.refresh();
            elt = elt.nextSibling;
        }
        $this.editor.graph.addCells(cells);

推荐阅读