【问题标题】:Creating/editing mxgraph models using Python使用 Python 创建/编辑 mxgraph 模型
【发布时间】:2020-06-16 09:05:57
【问题描述】:

是否可以使用 Python 创建、编辑或将 JSON 或 XML 数据转换为 mxgraph 格式的 mxgraph 模型?如果是这样怎么做?,我在各个站点上进行了搜索,但找不到解决方案(示例 JSON 数据);

  {
    name: 'Globals',    
    parentObjects: []
  },

  {
    name: 'Customer',
    included: true,    
    parentObjects: [
      {
        name: 'Globals',
      }
    ],
  },

  {
    name: 'Product',
    included: true,    
    parentObjects: [
      {
        name: 'Globals',
      }
    ],
  },

  {
    name: 'Transaction',
    included: true,    
    parentObjects: [
      {
        name: 'Customer',
      },
      {
        name: 'Product',
      }
    ],
  }

【问题讨论】:

    标签: javascript python python-3.x mxgraph


    【解决方案1】:

    使用 mxgraph 没有“统一”的方式...您可以创建各种形状的各种类型的节点... MxGraph 允许您以 JSON 和 XML 格式导入和导出图形,但首先您需要以与要读取的 MxGraph 兼容的 JSON 或 XML 格式创建一个。

    您可以在此处找到导入和导出 JSON 的代码示例: Example

    正如您在 JSON 中所见,您需要指定节点的各个方面(几何、父级、边、可连接性等)。与 XML 类似...

    我不是 Python 方面的专家,但您应该做的是将您的 JSON 转换为 MxGraph 可以实际读取的内容...

    示例 JSON:

    {
        "graph": [
            {
                "value": {
                    "name": "Daffy Duck"
                },
                "geometry": {
                    "x": 90,
                    "y": 60,
                    "width": 120,
                    "height": 30,
                    "relative": false,
                    "TRANSLATE_CONTROL_POINTS": true,
                    "alternateBounds": null,
                    "sourcePoint": null,
                    "targetPoint": null,
                    "points": null,
                    "offset": null
                },
                "id": "2",
                "vertex": true,
                "connectable": true,
                "parent": "1",
                "source": null,
                "target": null,
                "edges": [
                    {
                        "value": "Edge",
                        "geometry": {
                            "x": 0,
                            "y": 0,
                            "width": 0,
                            "height": 0,
                            "relative": true
                        },
                        "id": "4",
                        "edge": true,
                        "parent": "1",
                        "source": "2",
                        "target": "3",
                        "mxObjectId": "mxCell#7"
                    }
                ],
                "mxObjectId": "mxCell#6"
            },
            {
                "value": {
                    "name": "Bugs Bunny"
                },
                "geometry": {
                    "x": 1020,
                    "y": 60,
                    "width": 120,
                    "height": 30,
                    "relative": false,
                    "TRANSLATE_CONTROL_POINTS": true,
                    "alternateBounds": null,
                    "sourcePoint": null,
                    "targetPoint": null,
                    "points": null,
                    "offset": null
                },
                "id": "3",
                "vertex": true,
                "connectable": true,
                "parent": "1",
                "source": null,
                "target": null,
                "edges": [
                    {
                        "value": "Edge",
                        "geometry": {
                            "x": 0,
                            "y": 0,
                            "width": 0,
                            "height": 0,
                            "relative": true
                        },
                        "id": "4",
                        "edge": true,
                        "parent": "1",
                        "source": "2",
                        "target": "3",
                        "mxObjectId": "mxCell#7"
                    }
                ],
                "mxObjectId": "mxCell#8"
            },
            {
                "value": "Edge",
                "geometry": {
                    "x": 0,
                    "y": 0,
                    "width": 0,
                    "height": 0,
                    "relative": true
                },
                "id": "4",
                "edge": true,
                "parent": "1",
                "source": "2",
                "target": "3",
                "mxObjectId": "mxCell#7"
            },
            {
                "value": {
                    "name": "Elmer Fudd"
                },
                "geometry": {
                    "x": 90,
                    "y": 220,
                    "width": 120,
                    "height": 30,
                    "relative": false,
                    "TRANSLATE_CONTROL_POINTS": true,
                    "alternateBounds": null,
                    "sourcePoint": null,
                    "targetPoint": null,
                    "points": null,
                    "offset": null
                },
                "id": "5",
                "vertex": true,
                "connectable": true,
                "parent": "1",
                "source": null,
                "target": null,
                "edges": [
                    {
                        "value": "Edge",
                        "geometry": {
                            "x": 0,
                            "y": 0,
                            "width": 0,
                            "height": 0,
                            "relative": true
                        },
                        "id": "7",
                        "edge": true,
                        "parent": "1",
                        "source": "5",
                        "target": "6",
                        "mxObjectId": "mxCell#10"
                    }
                ],
                "mxObjectId": "mxCell#9"
            },
            {
                "value": {
                    "name": "Tasmanian Devil"
                },
                "geometry": {
                    "x": 1020,
                    "y": 220,
                    "width": 120,
                    "height": 30,
                    "relative": false,
                    "TRANSLATE_CONTROL_POINTS": true,
                    "alternateBounds": null,
                    "sourcePoint": null,
                    "targetPoint": null,
                    "points": null,
                    "offset": null
                },
                "id": "6",
                "vertex": true,
                "connectable": true,
                "parent": "1",
                "source": null,
                "target": null,
                "edges": [
                    {
                        "value": "Edge",
                        "geometry": {
                            "x": 0,
                            "y": 0,
                            "width": 0,
                            "height": 0,
                            "relative": true
                        },
                        "id": "7",
                        "edge": true,
                        "parent": "1",
                        "source": "5",
                        "target": "6",
                        "mxObjectId": "mxCell#10"
                    }
                ],
                "mxObjectId": "mxCell#11"
            },
            {
                "value": "Edge",
                "geometry": {
                    "x": 0,
                    "y": 0,
                    "width": 0,
                    "height": 0,
                    "relative": true
                },
                "id": "7",
                "edge": true,
                "parent": "1",
                "source": "5",
                "target": "6",
                "mxObjectId": "mxCell#10"
            }
        ]
    }
    

    【讨论】:

    • 感谢您的回复!然而,我正在从事的项目需要一种 Pythonic 的方式来完成它。如果有人可以提供有关如何做到这一点的线索,那就太好了。是否有现有的方法可以做到这一点,还是我们必须在 Python 中实现一个方法?
    • 很有趣,我目前正在搜索相同的内容。认为 mxGraph 在前端,所以从 DJANGO 或 Tornado 中的 API 生成 JSON 应该很容易@marco 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多