javascript - 如何通过 Jointjs 使用外部 json 文件中的动态数据

标签 javascript jquery

我正在使用 Jointjs 来实现我所需要的。

我需要知道的是,如何将来自 json 文件的数据放入组织结构图中。在 example of Jointjs ,一切都是硬编码的,显然这只是为了演示效果。

看代码

// here I call the json
$.getJSON( "test.json", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    console.log(key, val)
  });
});

json 返回:

[
    {   "Employee_id" : "345345",
        "name" : "Helga Martinez",
        "Contact_Info" : {
            "skype" : "mySkype",
            "email" : "myemail@email.com"
        },
        "Position" : "PM",
        "LOB" : "my lob",
        "Location" : "onshore",
        "Profile_Picture" : "URL",
        "Read_More" : "optional section"
    },
    {   "Employee_id" : "786",
        "name" : "jordan loaiza",
        "Contact_Info" : {
            "skype" : "the Skype",
            "email" : "theemail@email.com"
        },
        "Position" : "Dev",
        "LOB" : "the lob",
        "Location" : "offshore",
        "Profile_Picture" : "url/URL",
        "Read_More" : "optional section"
    },
    {   "Employee_id" : "23425",
        "name" : "marvin solano",
        "Contact_Info" : {
            "skype" : "3 Skype",
            "email" : "3email@email.com"
        },
        "Position" : "Front-End",
        "LOB" : "3 LOB",
        "Location" : "offshore",
        "Profile_Picture" : "url/URL/3",
        "Read_More" : "optional section"
    },
    {   "Employee_id" : "7657",
        "name" : "leroy bernard",
        "Contact_Info" : {
            "skype" : "5 Skype",
            "email" : "5email@email.com"
        },
        "Position" : "Sr Software Engineer",
        "LOB" : "5 LOB",
        "Location" : "onshore",
        "Profile_Picture" : "url/URL/5",
        "Read_More" : "optional section"
    },
    {   "Employee_id" : "78987",
        "name" : "diego porras",
        "Contact_Info" : {
            "skype" : "6 Skype",
            "email" : "6email@email.com"
        },
        "Position" : "Sr Front-End",
        "LOB" : "6 LOB",
        "Location" : "onshore",
        "Profile_Picture" : "url/URL/6",
        "Read_More" : "tons of things to say about me . . ."
    }
];

现在,根据职位属性"Position": "PM",PM 应该位于组织结构图的头部,然后位于其他人员的下方。

这是我尝试使用的组织结构图的源代码

var graph = new joint.dia.Graph();

var paper = new joint.dia.Paper({
    el: $('#paper'),
    width: 800,
    height: 600,
    gridSize: 1,
    model: graph,
    perpendicularLinks: true,
    restrictTranslate: true
});

var member = function(x, y, rank, name, image, background, textColor) {

    textColor = textColor || "#000";

    var cell = new joint.shapes.org.Member({
        position: { x: x, y: y },
        attrs: {
            '.card': { fill: background, stroke: 'none'},
              image: { 'xlink:href': '/images/demos/orgchart/'+ image, opacity: 0.7 },
            '.rank': { text: rank, fill: textColor, 'word-spacing': '-5px', 'letter-spacing': 0},
            '.name': { text: name, fill: textColor, 'font-size': 13, 'font-family': 'Arial', 'letter-spacing': 0 }
        }
    });
    graph.addCell(cell);
    return cell;
};

function link(source, target, breakpoints) {

    var cell = new joint.shapes.org.Arrow({
        source: { id: source.id },
        target: { id: target.id },
        vertices: breakpoints,
        attrs: {
            '.connection': {
                'fill': 'none',
                'stroke-linejoin': 'round',
                'stroke-width': '2',
                'stroke': '#4b4a67'
            }
        }

    });
    graph.addCell(cell);
    return cell;
}

var bart = member(300,70,'CEO', 'Bart Simpson', 'male.png', '#30d0c6');
var homer = member(90,200,'VP Marketing', 'Homer Simpson', 'male.png', '#7c68fd', '#f1f1f1');
var marge = member(300,200,'VP Sales', 'Marge Simpson', 'female.png', '#7c68fd', '#f1f1f1');
var lisa = member(500,200,'VP Production' , 'Lisa Simpson', 'female.png', '#7c68fd', '#f1f1f1');
var maggie = member(400,350,'Manager', 'Maggie Simpson', 'female.png', '#feb563');
var lenny = member(190,350,'Manager', 'Lenny Leonard', 'male.png', '#feb563');
var carl = member(190,500,'Manager', 'Carl Carlson', 'male.png', '#feb563');



link(bart, marge, [{x: 385, y: 180}]);
link(bart, homer, [{x: 385, y: 180}, {x: 175, y: 180}]);
link(bart, lisa, [{x: 385, y: 180}, {x: 585, y: 180}]);
link(homer, lenny, [{x:175 , y: 380}]);
link(homer, carl, [{x:175 , y: 530}]);
link(marge, maggie, [{x:385 , y: 380}]);

返回此 View

enter image description here

如您所见,该组织结构图的数据是硬编码的,我需要相同的数据,但使用从 JSON 接收的数据。

有什么建议吗?

最佳答案

此代码根据您的 json 数据和插件的限制提供自定义答案:

var graph = new joint.dia.Graph();

  var paper = new joint.dia.Paper({
      el: $('#paper'),
      width: 800,
      height: 600,
      gridSize: 1,
      model: graph,
      perpendicularLinks: true,
      restrictTranslate: true
  });

  var member = function(x, y, rank, name, image, background, textColor) {

      textColor = textColor || "#000";

      var cell = new joint.shapes.org.Member({
          position: { x: x, y: y },
          attrs: {
              '.card': { fill: background, stroke: 'none'},
                image: { 'xlink:href': '/images/demos/orgchart/'+ image, opacity: 0.7 },
              '.rank': { text: rank, fill: textColor, 'word-spacing': '-5px', 'letter-spacing': 0},
              '.name': { text: name, fill: textColor, 'font-size': 13, 'font-family': 'Arial', 'letter-spacing': 0 }
          }
      });
      graph.addCell(cell);
      return cell;
  };

  function link(source, target, breakpoints) {

      var cell = new joint.shapes.org.Arrow({
          source: { id: source.id },
          target: { id: target.id },
          vertices: breakpoints,
          attrs: {
              '.connection': {
                  'fill': 'none',
                  'stroke-linejoin': 'round',
                  'stroke-width': '2',
                  'stroke': '#4b4a67'
              }
          }

      });
      graph.addCell(cell);
      return cell;
  }


  var testJson = [
      {   "Employee_id" : "345345",
          "name" : "Helga Martinez",
          "Contact_Info" : {
              "skype" : "mySkype",
              "email" : "myemail@email.com"
          },
          "Position" : "PM",
          "LOB" : "my lob",
          "Location" : "onshore",
          "Profile_Picture" : "URL",
          "Read_More" : "optional section"
      },
      {   "Employee_id" : "786",
          "name" : "jordan loaiza",
          "Contact_Info" : {
              "skype" : "the Skype",
              "email" : "theemail@email.com"
          },
          "Position" : "Dev",
          "LOB" : "the lob",
          "Location" : "offshore",
          "Profile_Picture" : "url/URL",
          "Read_More" : "optional section"
      },
      {   "Employee_id" : "23425",
          "name" : "marvin solano",
          "Contact_Info" : {
              "skype" : "3 Skype",
              "email" : "3email@email.com"
          },
          "Position" : "Front-End",
          "LOB" : "3 LOB",
          "Location" : "offshore",
          "Profile_Picture" : "url/URL/3",
          "Read_More" : "optional section"
      },
      {   "Employee_id" : "7657",
          "name" : "leroy bernard",
          "Contact_Info" : {
              "skype" : "5 Skype",
              "email" : "5email@email.com"
          },
          "Position" : "Sr Software Engineer",
          "LOB" : "5 LOB",
          "Location" : "onshore",
          "Profile_Picture" : "url/URL/5",
          "Read_More" : "optional section"
      },
      {   "Employee_id" : "78987",
          "name" : "diego porras",
          "Contact_Info" : {
              "skype" : "6 Skype",
              "email" : "6email@email.com"
          },
          "Position" : "Sr Front-End",
          "LOB" : "6 LOB",
          "Location" : "onshore",
          "Profile_Picture" : "url/URL/6",
          "Read_More" : "tons of things to say about me . . ."
      }
  ];



  var levels = ['#30d0c6','#7c68fd','#feb563']
  var head = member(300,70,testJson[0].Position,testJson[0].name,testJson[0].Profile_Picture,levels[0]);
  var objUsers = {};
  var x = -200;
  var y = 200;
  $.each(testJson,function(i,item){
    if(i !== 0){
      x = x +200;
      objUsers[item.name] = member(x,y,item.Position,item.name,item.Profile_Picture,levels[1]);
      link(head,objUsers[item.name],[{x: 385, y: 180}, {x: x + 100, y: 180}]);
    }   
  });

工作 fiddle :https://jsfiddle.net/robertrozas/gfaL9ype/

关于javascript - 如何通过 Jointjs 使用外部 json 文件中的动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35182546/

相关文章:

javascript - 按下键后防止光标隐藏在浏览器中

javascript - 在 map 函数中 react onClick 处理程序

javascript - Backbone : Correct way of passing 'this' reference to anon functions in success/error callbacks

javascript - 通过 Ajax 下载生成的 CSV 文件

javascript - 谷歌地图显示多个位置的标记

jQuery/DataTables - 导出到 Excel

javascript - 如何禁用 Bootstrap 表单字段?

javascript - 同步相互依赖的 Dojo 小部件/值

javascript - <p :commandButton> not getting scrolled along with the <p:scrollPanel>

JavaScript - 如何知道定期运行的方法是否执行了超时?