最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - mxGraph convert JSON data to graph - Stack Overflow

programmeradmin0浏览0评论

I have a set of JSON format data which needs to be converted into a mxGraph diagram.

here's how it should look like:

This is the structure of my JSON data

[
  {
    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',
      }
    ],
  },
]

I am very new to mxGraph and I do not have much experience with it so any help would be much appreciated. Thank you.

I have a set of JSON format data which needs to be converted into a mxGraph diagram.

here's how it should look like:

This is the structure of my JSON data

[
  {
    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',
      }
    ],
  },
]

I am very new to mxGraph and I do not have much experience with it so any help would be much appreciated. Thank you.

Share Improve this question edited Sep 26, 2017 at 11:22 Ankur 1,9144 gold badges33 silver badges59 bronze badges asked Sep 26, 2017 at 11:07 PrajwalPrajwal 1981 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

I am also new to mxGraph and wanted a little learning session. So I tried it with your requirements.

I have created a fiddle from the example projects of mxGraph-js.

Here is the primary code that makes the magic:

var root = undefined;

...

var dict = {};
// run through each element in json
data.forEach(function (element) {
    var name = element.name;
    // create graph element
    var graphElement = graph.insertVertex(parent, null, name, 20, 20, 80, 30);
    // check if any parent element
    if (element.parentObjects.length > 0) {
        // run through each parent element
        element.parentObjects.forEach(function (parentObj) {
            var parentGraphElement = dict[parentObj.name];
            // add line between current element and parent
            graph.insertEdge(parent, null, '', parentGraphElement, graphElement);          
        });
    } else {
        // set root for layouting
        root = graphElement;
    }
    // add element to dictionary. this is needed to find object later(parent)
    dict[name] = graphElement;
});

...

// Creates a layout algorithm to be used with the graph
var layout = new mxHierarchicalLayout(graph, mxConstants.DIRECTION_NORTH);

// Moves stuff wider apart than usual
layout.forceConstant = 140;
if (root) {
    layout.execute(parent, root);
}

Happy Coding, Kalasch

发布评论

评论列表(0)

  1. 暂无评论