2017-02-17 110 views
1

在mxGraph-js中,我使用以下代码向顶点添加覆盖图。mxGraph js不编码覆盖顶点

graph.addCellOverlay(cell, overlay); 

和编码图形为XML的

var graph = new mxGraph(container); 
var xml = encoder.encode(graph.getModel()); 

然后,使用下面的方法对其进行解码回来。

var doc = mxUtils.parseXml(xml); 
var codec = new mxCodec(doc); 
codec.decode(doc.documentElement, graph.getModel()); 

我的问题是,当解码编码图回来绘制没有覆盖图的图形。看起来,编码重叠不会被编码到xml中。我如何编码与覆盖图的图形,然后正确解码回来?

回答

0

我跑过同一个问题。没有深入挖掘mxGraph js代码,我怀疑这是一个缺失的功能,或者可能是功能受损。

下面是我看到的一些可能的选项。

  1. 将图像嵌入到单元格样式中。 查看源示例fixedicon.html。其经由父属性到主要细胞相关

    <mxGraphModel> 
        <root> 
         <mxCell id="0"/> 
         <mxCell id="1" parent="0"/> 
         <mxCell id="2" value="Fixed icon" style="shape=label;image=images/plus.png;imageWidth=16;imageHeight=16;spacingBottom=10;fillColor=#adc5ff;gradientColor=#7d85df;glass=1;rounded=1;shadow=1;" vertex="1" parent="1"> 
          <mxGeometry x="20" y="20" width="80" height="50" as="geometry"/> 
         </mxCell> 
        </root> 
    </mxGraphModel> 
    
  2. 端口类型的细胞(在式中定义)。 查看源码示例ports.html。

    <mxGraphModel> 
        <root> 
        <mxCell id="0"/> 
        <mxCell id="1" parent="0"/> 
        <mxCell id="2" value="<h1 style="margin:0px;">Website</h1><br><img src="images/icons48/earth.png" width="48" height="48"><br> 
         <a href="http://www.jgraph.com" target="_blank">Browse</a>" vertex="1" connectable="0" parent="1"> 
         <mxGeometry x="280" y="200" width="120" height="120" as="geometry"> 
         <mxRectangle width="120" height="40" as="alternateBounds"/> 
         </mxGeometry> 
        </mxCell> 
        <mxCell id="3" value="Trigger" style="port;image=editors/images/overlays/flash.png;align=right;imageAlign=right;spacingRight=18" vertex="1" parent="2"> 
         <mxGeometry y="0.25" width="16" height="16" relative="1" as="geometry"> 
         <mxPoint x="-6" y="-8" as="offset"/> 
         </mxGeometry> 
        </mxCell> 
        <mxCell id="4" value="Input" style="port;image=editors/images/overlays/check.png;align=right;imageAlign=right;spacingRight=18" vertex="1" parent="2"> 
         <mxGeometry y="0.75" width="16" height="16" relative="1" as="geometry"> 
         <mxPoint x="-6" y="-4" as="offset"/> 
         </mxGeometry> 
        </mxCell> 
        <mxCell id="5" value="Error" style="port;image=editors/images/overlays/error.png;spacingLeft=18" vertex="1" parent="2"> 
         <mxGeometry x="1" y="0.25" width="16" height="16" relative="1" as="geometry"> 
         <mxPoint x="-8" y="-8" as="offset"/> 
         </mxGeometry> 
        </mxCell> 
        <mxCell id="6" value="Result" style="port;image=editors/images/overlays/information.png;spacingLeft=18" vertex="1" parent="2"> 
         <mxGeometry x="1" y="0.75" width="16" height="16" relative="1" as="geometry"> 
         <mxPoint x="-8" y="-4" as="offset"/> 
         </mxGeometry> 
        </mxCell> 
        </root> 
    </mxGraphModel> 
    
  3. 式样细胞和细胞图像组合在一起。

    <mxGraphModel> 
        <root> 
        <mxCell id="0"/> 
        <mxCell id="1" parent="0"/> 
        <mxCell id="6" value="" style="group" parent="1" vertex="1" connectable="0"> 
         <mxGeometry x="70" y="70" width="120" height="85" as="geometry"/> 
        </mxCell> 
        <mxCell id="4" value="" style="shape=image;imageAspect=0;aspect=fixed;verticalLabelPosition=bottom;verticalAlign=top;image=images/close.png;" parent="6" vertex="1"> 
         <mxGeometry x="5" y="76" width="9" height="9" as="geometry"/> 
        </mxCell> 
        <UserObject label="shape" id="3"> 
         <mxCell style="whiteSpace=wrap;html=1;" parent="6" vertex="1"> 
         <mxGeometry width="120" height="60" as="geometry"/> 
         </mxCell> 
        </UserObject> 
        </root> 
    </mxGraphModel> 
    
+0

我倾向于选项#2,因为我需要将点击事件分配给图像。 –

0

正在探索添加子细胞中的选项#2,从最初的答案,但发现没有办法添加单击事件处理程序。最后深入挖掘源代码,发现在mxCellCodec类中它实例化了一个mxObjectCodec,该mxObjectCodec具有包含覆盖的默认排除列表。

var codec = new mxObjectCodec(new mxCell(), 
['children', 'edges', 'overlays', 'mxTransient'], 
['parent', 'source', 'target']); 

解决方法是从编解码器排除列表中删除“覆盖”。我还必须将allowEval属性设置为true以允许事件侦听器函数也被编码为&已解码。

// remove overlays from exclude list for mxCellCodec so that overlays are encoded into XML 
var cellCodec = mxCodecRegistry.getCodec(mxCell); 
var excludes = cellCodec.exclude; 
excludes.splice(excludes.indexOf('overlays'), 1); 

// set flag to allow expressions (function) to be encoded 
cellCodec.allowEval = true; 

// set flag to allow expressions (function) to be decoded 
mxObjectCodec.allowEval = true;