parser.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var responseObjects = p5WFS_GetFeature();
  2. var object = responseObjects[0];
  3. var nodes = {};
  4. var nodesArray = [];
  5. var links = {};
  6. var linksArray = [];
  7. var instanceTypes = {};
  8. var dragged = false;
  9. const hasTargetArray = node => {
  10. for (const prop in node)
  11. if (node[prop].constructor === Array)
  12. return {array: node[prop], name: prop.split(':').pop().replace(/BI_audit_/, '')};
  13. return false;
  14. }
  15. const getLinks = (array, expansion) => {
  16. if (array != undefined && expansion !== undefined) {
  17. array.forEach(element => {
  18. const rowObject = hasTargetArray(element);
  19. if (rowObject) {
  20. for (let i = rowObject.array.length - 2; i >= 0; i--) {
  21. const target = hasTargetArray(rowObject.array[i]);
  22. const source = hasTargetArray(rowObject.array[i + 1]);
  23. let sourceGroup = source.name;
  24. let targetGroup = target.name;
  25. if (expansion && expansion.type) {
  26. if (sourceGroup === expansion.type)
  27. sourceGroup = source.array[0][expansion.criteria];
  28. if (targetGroup === expansion.type)
  29. targetGroup = target.array[0][expansion.criteria];
  30. }
  31. if (!links.hasOwnProperty(sourceGroup))
  32. links[sourceGroup] = {};
  33. if (links[sourceGroup].hasOwnProperty(targetGroup)) {
  34. links[sourceGroup][targetGroup]++;
  35. } else if (targetGroup) {
  36. links[sourceGroup][targetGroup] = 1;
  37. }
  38. }
  39. }
  40. })
  41. }
  42. };
  43. const getNodes = (array, expansion) => {
  44. if (array != undefined && expansion !== undefined) {
  45. array.forEach(element => {
  46. const rowObject = hasTargetArray(element);
  47. if (rowObject)
  48. rowObject.array.forEach(objectElement => {
  49. const trackNode = hasTargetArray(objectElement);
  50. if (trackNode) {
  51. let group = trackNode.name;
  52. let expanded = false;
  53. if (expansion && expansion.type && group === expansion.type) {
  54. group = trackNode.array[0][expansion.criteria];
  55. expanded = true;
  56. }
  57. nodes[group] = {expanded: expanded, type: trackNode.name};
  58. instanceTypes[trackNode.array[0].ID] = group;
  59. }
  60. });
  61. });
  62. }
  63. };
  64. const parseNodes = () => {
  65. for (const node in nodes) {
  66. if (nodes.hasOwnProperty(node))
  67. nodesArray.push({name: node, expanded: nodes[node].expanded, type: nodes[node].type});
  68. }
  69. };
  70. const parseLinks = () => {
  71. for (const link in links) {
  72. if (links.hasOwnProperty(link)) {
  73. for (const target in links[link]) {
  74. if (links[link].hasOwnProperty(target)) {
  75. linksArray.push({
  76. source: link,
  77. target: target,
  78. value: links[link][target]
  79. });
  80. }
  81. }
  82. }
  83. }
  84. };
  85. const parseGraph = (expansion) => {
  86. nodes = {};
  87. links = {};
  88. nodesArray = [];
  89. linksArray = [];
  90. getNodes(responseObjects[0]["default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row"], expansion);
  91. parseNodes();
  92. getLinks(responseObjects[0]["default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row"], expansion);
  93. parseLinks();
  94. dataModel = data = {
  95. nodes: nodesArray,
  96. links: linksArray
  97. };
  98. };
  99. const redraw = (expansion) => {
  100. if (dragged)
  101. return dragged = false;
  102. parseGraph(expansion);
  103. draw();
  104. };
  105. parseGraph();