createGraphFromNodeEdge.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import * as zrUtil from 'zrender/lib/core/util.js';
  41. import SeriesData from '../../data/SeriesData.js';
  42. import Graph from '../../data/Graph.js';
  43. import linkSeriesData from '../../data/helper/linkSeriesData.js';
  44. import prepareSeriesDataSchema from '../../data/helper/createDimensions.js';
  45. import CoordinateSystem from '../../core/CoordinateSystem.js';
  46. import createSeriesData from './createSeriesData.js';
  47. import { convertOptionIdName } from '../../util/model.js';
  48. export default function createGraphFromNodeEdge(nodes, edges, seriesModel, directed, beforeLink) {
  49. // ??? TODO
  50. // support dataset?
  51. var graph = new Graph(directed);
  52. for (var i = 0; i < nodes.length; i++) {
  53. graph.addNode(zrUtil.retrieve( // Id, name, dataIndex
  54. nodes[i].id, nodes[i].name, i), i);
  55. }
  56. var linkNameList = [];
  57. var validEdges = [];
  58. var linkCount = 0;
  59. for (var i = 0; i < edges.length; i++) {
  60. var link = edges[i];
  61. var source = link.source;
  62. var target = link.target; // addEdge may fail when source or target not exists
  63. if (graph.addEdge(source, target, linkCount)) {
  64. validEdges.push(link);
  65. linkNameList.push(zrUtil.retrieve(convertOptionIdName(link.id, null), source + ' > ' + target));
  66. linkCount++;
  67. }
  68. }
  69. var coordSys = seriesModel.get('coordinateSystem');
  70. var nodeData;
  71. if (coordSys === 'cartesian2d' || coordSys === 'polar') {
  72. nodeData = createSeriesData(nodes, seriesModel);
  73. } else {
  74. var coordSysCtor = CoordinateSystem.get(coordSys);
  75. var coordDimensions = coordSysCtor ? coordSysCtor.dimensions || [] : []; // FIXME: Some geo do not need `value` dimenson, whereas `calendar` needs
  76. // `value` dimension, but graph need `value` dimension. It's better to
  77. // uniform this behavior.
  78. if (zrUtil.indexOf(coordDimensions, 'value') < 0) {
  79. coordDimensions.concat(['value']);
  80. }
  81. var dimensions = prepareSeriesDataSchema(nodes, {
  82. coordDimensions: coordDimensions,
  83. encodeDefine: seriesModel.getEncode()
  84. }).dimensions;
  85. nodeData = new SeriesData(dimensions, seriesModel);
  86. nodeData.initData(nodes);
  87. }
  88. var edgeData = new SeriesData(['value'], seriesModel);
  89. edgeData.initData(validEdges, linkNameList);
  90. beforeLink && beforeLink(nodeData, edgeData);
  91. linkSeriesData({
  92. mainData: nodeData,
  93. struct: graph,
  94. structAttr: 'graph',
  95. datas: {
  96. node: nodeData,
  97. edge: edgeData
  98. },
  99. datasAttr: {
  100. node: 'data',
  101. edge: 'edgeData'
  102. }
  103. }); // Update dataIndex of nodes and edges because invalid edge may be removed
  104. graph.update();
  105. return graph;
  106. }