如何将(SVG)图像添加到mxGraph

时间:2018-06-26 09:47:00

标签: svg mxgraph

我想将图像添加到mxGraph。 在我的尝试中,我看到gui处的框架,并且框架内部是图像文本而不是图像。 如何将图像添加到mxGraph?

我的代码如下:

    const newDiv = document.createElement('div');
    const parent = drawIoUi.editor.graph.getDefaultParent();
    drawIoUi.editor.graph.getModel().beginUpdate();
    const decodetSvg1 = btoa(this._svgTest);
    let newChart: any;
    try {
        newChart = drawIoUi.editor.graph.insertVertex(parent, null, newDiv, 40, 140, 300, 200);
    } finally {
        const fullImg1 =
            `<img alt="" style="width:100%; height:100%;" src="data:image/svg+xml;base64,` +
            decodetSvg1 +
            `">`;
        newChart.setAttribute('label', fullImg1);
        drawIoUi.editor.graph.getModel().endUpdate();
        drawIoUi.editor.graph.refresh();
        drawIoUi.editor.graph.container.focus();
    }

结果看起来像这样: enter image description here

1 个答案:

答案 0 :(得分:0)

在具有图像形状的单元格样式中使用缩短的数据URI,例如

graph.insertVertex(parent, null, '', 20, 20, 80, 80,
                            'shape=image;image=data:image/svg+xml,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIGlkPSJFYmVuZV8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iMCAwIDI1MCAyNTAiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDI1MCAyNTA7IiB4bWw6c3BhY2U9InByZXNlcnZlIj4mI3hhOzxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyI+JiN4YTsJLnN0MHtmaWxsOiNhMmEyYTI7fSYjeGE7CS5zdDF7ZmlsbDojOGU4ZThlO30mI3hhOwkuc3Qye2ZpbGw6I0ZGRkZGRjt9JiN4YTs8L3N0eWxlPiYjeGE7PHRpdGxlPlplaWNoZW5mbMOkY2hlIDE8L3RpdGxlPiYjeGE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