createGraphFromNodeEdge.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. function _default(nodes, edges, seriesModel, directed, beforeLink) {
  45. // ??? TODO
  46. // support dataset?
  47. var graph = new Graph(directed);
  48. for (var i = 0; i < nodes.length; i++) {
  49. graph.addNode(zrUtil.retrieve( // Id, name, dataIndex
  50. nodes[i].id, nodes[i].name, i), i);
  51. }
  52. var linkNameList = [];
  53. var validEdges = [];
  54. var linkCount = 0;
  55. for (var i = 0; i < edges.length; i++) {
  56. var link = edges[i];
  57. var source = link.source;
  58. var target = link.target; // addEdge may fail when source or target not exists
  59. if (graph.addEdge(source, target, linkCount)) {
  60. validEdges.push(link);
  61. linkNameList.push(zrUtil.retrieve(link.id, source + ' > ' + target));
  62. linkCount++;
  63. }
  64. }
  65. var coordSys = seriesModel.get('coordinateSystem');
  66. var nodeData;
  67. if (coordSys === 'cartesian2d' || coordSys === 'polar') {
  68. nodeData = createListFromArray(nodes, seriesModel);
  69. } else {
  70. var coordSysCtor = CoordinateSystem.get(coordSys);
  71. var coordDimensions = coordSysCtor && coordSysCtor.type !== 'view' ? coordSysCtor.dimensions || [] : []; // FIXME: Some geo do not need `value` dimenson, whereas `calendar` needs
  72. // `value` dimension, but graph need `value` dimension. It's better to
  73. // uniform this behavior.
  74. if (zrUtil.indexOf(coordDimensions, 'value') < 0) {
  75. coordDimensions.concat(['value']);
  76. }
  77. var dimensionNames = createDimensions(nodes, {
  78. coordDimensions: coordDimensions
  79. });
  80. nodeData = new List(dimensionNames, seriesModel);
  81. nodeData.initData(nodes);
  82. }
  83. var edgeData = new List(['value'], seriesModel);
  84. edgeData.initData(validEdges, linkNameList);
  85. beforeLink && beforeLink(nodeData, edgeData);
  86. linkList({
  87. mainData: nodeData,
  88. struct: graph,
  89. structAttr: 'graph',
  90. datas: {
  91. node: nodeData,
  92. edge: edgeData
  93. },
  94. datasAttr: {
  95. node: 'data',
  96. edge: 'edgeData'
  97. }
  98. }); // Update dataIndex of nodes and edges because invalid edge may be removed
  99. graph.update();
  100. return graph;
  101. }
  102. module.exports = _default;