createGraphFromNodeEdge.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(
  54. // Id, name, dataIndex
  55. nodes[i].id, nodes[i].name, i), i);
  56. }
  57. var linkNameList = [];
  58. var validEdges = [];
  59. var linkCount = 0;
  60. for (var i = 0; i < edges.length; i++) {
  61. var link = edges[i];
  62. var source = link.source;
  63. var target = link.target;
  64. // addEdge may fail when source or target not exists
  65. if (graph.addEdge(source, target, linkCount)) {
  66. validEdges.push(link);
  67. linkNameList.push(zrUtil.retrieve(convertOptionIdName(link.id, null), source + ' > ' + target));
  68. linkCount++;
  69. }
  70. }
  71. var coordSys = seriesModel.get('coordinateSystem');
  72. var nodeData;
  73. if (coordSys === 'cartesian2d' || coordSys === 'polar') {
  74. nodeData = createSeriesData(nodes, seriesModel);
  75. } else {
  76. var coordSysCtor = CoordinateSystem.get(coordSys);
  77. var coordDimensions = coordSysCtor ? coordSysCtor.dimensions || [] : [];
  78. // FIXME: Some geo do not need `value` dimenson, whereas `calendar` needs
  79. // `value` dimension, but graph need `value` dimension. It's better to
  80. // uniform this behavior.
  81. if (zrUtil.indexOf(coordDimensions, 'value') < 0) {
  82. coordDimensions.concat(['value']);
  83. }
  84. var dimensions = prepareSeriesDataSchema(nodes, {
  85. coordDimensions: coordDimensions,
  86. encodeDefine: seriesModel.getEncode()
  87. }).dimensions;
  88. nodeData = new SeriesData(dimensions, seriesModel);
  89. nodeData.initData(nodes);
  90. }
  91. var edgeData = new SeriesData(['value'], seriesModel);
  92. edgeData.initData(validEdges, linkNameList);
  93. beforeLink && beforeLink(nodeData, edgeData);
  94. linkSeriesData({
  95. mainData: nodeData,
  96. struct: graph,
  97. structAttr: 'graph',
  98. datas: {
  99. node: nodeData,
  100. edge: edgeData
  101. },
  102. datasAttr: {
  103. node: 'data',
  104. edge: 'edgeData'
  105. }
  106. });
  107. // Update dataIndex of nodes and edges because invalid edge may be removed
  108. graph.update();
  109. return graph;
  110. }