createGraphFromNodeMatrix.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. var List = require("../../data/List");
  21. var Graph = require("../../data/Graph");
  22. var linkList = require("../../data/helper/linkList");
  23. var createDimensions = require("../../data/helper/createDimensions");
  24. var CoordinateSystem = require("../../CoordinateSystem");
  25. var createListFromArray = require("./createListFromArray");
  26. /*
  27. * Licensed to the Apache Software Foundation (ASF) under one
  28. * or more contributor license agreements. See the NOTICE file
  29. * distributed with this work for additional information
  30. * regarding copyright ownership. The ASF licenses this file
  31. * to you under the Apache License, Version 2.0 (the
  32. * "License"); you may not use this file except in compliance
  33. * with the License. You may obtain a copy of the License at
  34. *
  35. * http://www.apache.org/licenses/LICENSE-2.0
  36. *
  37. * Unless required by applicable law or agreed to in writing,
  38. * software distributed under the License is distributed on an
  39. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  40. * KIND, either express or implied. See the License for the
  41. * specific language governing permissions and limitations
  42. * under the License.
  43. */
  44. /**
  45. * 从邻接矩阵生成
  46. * ```
  47. * TARGET
  48. * -1--2--3--4--5-
  49. * 1| x x x x x
  50. * 2| x x x x x
  51. * 3| x x x x x SOURCE
  52. * 4| x x x x x
  53. * 5| x x x x x
  54. * ```
  55. *
  56. * @param {Array.<Object>} nodes 节点信息
  57. * @param {Array} matrix 邻接矩阵
  58. * @param {module:echarts/model/Series}
  59. * @param {boolean} directed 是否是有向图
  60. * @return {module:echarts/data/Graph}
  61. */
  62. function _default(nodes, matrix, hostModel, directed) {
  63. var graph = new Graph(directed);
  64. for (var i = 0; i < nodes.length; i++) {
  65. graph.addNode(zrUtil.retrieve( // Id, name, dataIndex
  66. nodes[i].id, nodes[i].name, i), i);
  67. }
  68. var size = matrix.length;
  69. var links = [];
  70. var linkCount = 0;
  71. for (var i = 0; i < size; i++) {
  72. for (var j = 0; j < size; j++) {
  73. var val = matrix[i][j];
  74. if (val === 0) {
  75. continue;
  76. }
  77. var n1 = graph.nodes[i];
  78. var n2 = graph.nodes[j];
  79. var edge = graph.addEdge(n1, n2, linkCount);
  80. if (edge) {
  81. linkCount++;
  82. links.push({
  83. value: val
  84. });
  85. }
  86. }
  87. }
  88. var coordSys = hostModel.get('coordinateSystem');
  89. var nodeData;
  90. if (coordSys === 'cartesian2d' || coordSys === 'polar') {
  91. nodeData = createListFromArray({
  92. data: nodes
  93. }, hostModel);
  94. } else {
  95. // FIXME
  96. var coordSysCtor = CoordinateSystem.get(coordSys); // FIXME
  97. var dimensionNames = createDimensions(nodes, {
  98. coordDimensions: (coordSysCtor && coordSysCtor.type !== 'view' ? coordSysCtor.dimensions || [] : []).concat(['value'])
  99. });
  100. nodeData = new List(dimensionNames, hostModel);
  101. nodeData.initData(nodes);
  102. }
  103. var edgeData = new List(['value'], hostModel);
  104. edgeData.initData(links);
  105. linkList({
  106. mainData: nodeData,
  107. struct: graph,
  108. structAttr: 'graph',
  109. datas: {
  110. node: nodeData,
  111. edge: edgeData
  112. },
  113. datasAttr: {
  114. node: 'data',
  115. edge: 'edgeData'
  116. }
  117. }); // Update dataIndex of nodes and edges because invalid edge may be removed
  118. graph.update();
  119. return graph;
  120. }
  121. module.exports = _default;