+-
java-如何将图形导出到svg / graphml
我对如何将图形导出到svg或graphml有点困惑.到目前为止,api,example.com上的示例或线程都没有帮助我.

我需要将图形导出到svg和graphml.我得到了svg,即使布局正确也可以显示节点和边缘,但是我缺少诸如节点名称和分配的颜色之类的信息.

使用graphml,我还不知道如何获取正确的xml代码甚至显示功能正常的图形.

是否有任何指南/工作流程可以帮助我在JGraphX中进行导出?

预先感谢您的帮助,

克里斯

最佳答案
为了保存图形,您必须调用mxCellRendered将图形渲染到mxicanvas,从中获取文档(dom文档).
在渲染器中,它是这样的:
mxGraph.drawCell()-> mxGraph.drawState()-> mxICanvas.drawCell()-> mxICanvas.drawShape()
mxICanvas仅了解单元的几何形状和样式.

我也希望在svg文件中添加cell id属性,所以我做了以下工作

>扩展了mxGraph以覆盖drawCell()以便以单元格样式添加单元格ID,并且
>扩展了mxSvgCanvas,为我感兴趣的形状添加了id属性

另存为svg图的功能如下:

// use the extended svg canvas, where the cell id is added as attribute
public void createSVG(mxGraphExtended g) {
  String filename = "\home\koula\graph.svg";
  mxSvgCanvasExtended canvas = (mxSvgCanvasExtended) mxCellRenderer.drawCells(
    g, null, 1, null, new CanvasFactory() {
    public mxICanvas createCanvas(int width, int height) {
        mxSvgCanvasExtended canvas = new mxSvgCanvasExtended(mxDomUtils
            .createSvgDocument(width, height));
            canvas.setEmbedded(true);
            return canvas;
        } 
    });
  try {
    mxUtils.writeFile(mxXmlUtils.getXml(canvas.getDocument()), filename);
  } catch (IOException e) {
    e.printStackTrace();
  } 
}

重写的drawCell():

public class mxGraphExtended extends mxGraph {

    @Override
    public void drawCell(mxICanvas canvas, Object cell) {
        // add the cell's id as a style attribute
        // cause canvas only get the style and geometry
        mxCellState state = this.getView().getState(cell);
        state.getStyle().put("cellid", ((mxCell)cell).getId());

        super.drawCell(canvas, cell);
    }
}

重写的drawShape()如下所示:

public class mxSvgCanvasExtended extends mxSvgCanvas {

    //... have coppied only the related code

    @Override
    public Element drawShape(int x, int y, int w, int h, 
        Map<String, Object> style)
    {
         //... 

         // Draws the shape
         String shape = mxUtils.getString(style, mxConstants.STYLE_SHAPE, "");
         String cellid = mxUtils.getString(style, "cellid", "");
         Element elem = null;
         // ... 

        // e.g. if image, add the cell id 
        if (shape.equals(mxConstants.SHAPE_IMAGE)) {
            String img = getImageForStyle(style);
            if (img != null) {
                // Vertical and horizontal image flipping
                boolean flipH = mxUtils.isTrue(style, 
                    mxConstants.STYLE_IMAGE_FLIPH, false);
                boolean flipV = mxUtils.isTrue(style, 
                    mxConstants.STYLE_IMAGE_FLIPV, false);
                elem = createImageElement(x, y, w, h, img,
                    PRESERVE_IMAGE_ASPECT, flipH, flipV, isEmbedded());
                /* so here we are */ 
                // add the cell id atribute 
                if(!cellid.equals("")) {
                    elem.setAttribute("id", cellid);
                }
            }
        } else if (shape.equals(mxConstants.SHAPE_LINE))
            // ... 
        }// end drawShape

     } // end class

希望对您有所帮助.

点击查看更多相关文章

转载注明原文:java-如何将图形导出到svg / graphml - 乐贴网