java - how to export a graph to svg/graphml -
i little bit stuck on how export graphs svg or graphml. neither api, examples or threads on forum.jgraph.com did me until now.
i need export graphs both svg , graphml. got svg display nodes , edges correct layout, i'm missing information names of nodes , assigned colors.
with graphml have no clue yet how correct xml code display functioning graph.
is there guideline/workflow somewhere might me export in jgraphx?
thanks in advance help,
chris
in order save graph have call mxcellrendered render graph mxicanvas document (dom document). renderer goes this: mxgraph.drawcell() -> mxgraph.drawstate() -> mxicanvas.drawcell() -> mxicanvas.drawshape() mxicanvas knows cell's geometry , style.
i wanted cell id attribute added in svg file well, did following
- extended mxgraph override drawcell() in order add cell id in style of cell, ,
- extended mxsvgcanvas adding id attribute shapes interested me
the function save svg graph goes like:
// use extended svg canvas, cell id added 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(); } }
the overriden drawcell():
public class mxgraphextended extends mxgraph { @override public void drawcell(mxicanvas canvas, object cell) { // add cell's id style attribute // cause canvas style , geometry mxcellstate state = this.getview().getstate(cell); state.getstyle().put("cellid", ((mxcell)cell).getid()); super.drawcell(canvas, cell); } }
the overridden drawshape() goes like:
public class mxsvgcanvasextended extends mxsvgcanvas { //... have coppied related code @override public element drawshape(int x, int y, int w, int h, map<string, object> style) { //... // draws shape string shape = mxutils.getstring(style, mxconstants.style_shape, ""); string cellid = mxutils.getstring(style, "cellid", ""); element elem = null; // ... // e.g. if image, add cell id if (shape.equals(mxconstants.shape_image)) { string img = getimageforstyle(style); if (img != null) { // vertical , 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()); /* here */ // add cell id atribute if(!cellid.equals("")) { elem.setattribute("id", cellid); } } } else if (shape.equals(mxconstants.shape_line)) // ... }// end drawshape } // end class
hope help.
Comments
Post a Comment