GraphSeries.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 echarts = require("../../echarts");
  20. var List = require("../../data/List");
  21. var zrUtil = require("zrender/lib/core/util");
  22. var _model = require("../../util/model");
  23. var defaultEmphasis = _model.defaultEmphasis;
  24. var Model = require("../../model/Model");
  25. var _format = require("../../util/format");
  26. var encodeHTML = _format.encodeHTML;
  27. var createGraphFromNodeEdge = require("../helper/createGraphFromNodeEdge");
  28. var LegendVisualProvider = require("../../visual/LegendVisualProvider");
  29. var _multipleGraphEdgeHelper = require("../helper/multipleGraphEdgeHelper");
  30. var initCurvenessList = _multipleGraphEdgeHelper.initCurvenessList;
  31. var createEdgeMapForCurveness = _multipleGraphEdgeHelper.createEdgeMapForCurveness;
  32. /*
  33. * Licensed to the Apache Software Foundation (ASF) under one
  34. * or more contributor license agreements. See the NOTICE file
  35. * distributed with this work for additional information
  36. * regarding copyright ownership. The ASF licenses this file
  37. * to you under the Apache License, Version 2.0 (the
  38. * "License"); you may not use this file except in compliance
  39. * with the License. You may obtain a copy of the License at
  40. *
  41. * http://www.apache.org/licenses/LICENSE-2.0
  42. *
  43. * Unless required by applicable law or agreed to in writing,
  44. * software distributed under the License is distributed on an
  45. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  46. * KIND, either express or implied. See the License for the
  47. * specific language governing permissions and limitations
  48. * under the License.
  49. */
  50. var GraphSeries = echarts.extendSeriesModel({
  51. type: 'series.graph',
  52. init: function (option) {
  53. GraphSeries.superApply(this, 'init', arguments);
  54. var self = this;
  55. function getCategoriesData() {
  56. return self._categoriesData;
  57. } // Provide data for legend select
  58. this.legendVisualProvider = new LegendVisualProvider(getCategoriesData, getCategoriesData);
  59. this.fillDataTextStyle(option.edges || option.links);
  60. this._updateCategoriesData();
  61. },
  62. mergeOption: function (option) {
  63. GraphSeries.superApply(this, 'mergeOption', arguments);
  64. this.fillDataTextStyle(option.edges || option.links);
  65. this._updateCategoriesData();
  66. },
  67. mergeDefaultAndTheme: function (option) {
  68. GraphSeries.superApply(this, 'mergeDefaultAndTheme', arguments);
  69. defaultEmphasis(option, ['edgeLabel'], ['show']);
  70. },
  71. getInitialData: function (option, ecModel) {
  72. var edges = option.edges || option.links || [];
  73. var nodes = option.data || option.nodes || [];
  74. var self = this;
  75. if (nodes && edges) {
  76. // auto curveness
  77. initCurvenessList(this);
  78. var graph = createGraphFromNodeEdge(nodes, edges, this, true, beforeLink);
  79. zrUtil.each(graph.edges, function (edge) {
  80. createEdgeMapForCurveness(edge.node1, edge.node2, this, edge.dataIndex);
  81. }, this);
  82. return graph.data;
  83. }
  84. function beforeLink(nodeData, edgeData) {
  85. // Overwrite nodeData.getItemModel to
  86. nodeData.wrapMethod('getItemModel', function (model) {
  87. var categoriesModels = self._categoriesModels;
  88. var categoryIdx = model.getShallow('category');
  89. var categoryModel = categoriesModels[categoryIdx];
  90. if (categoryModel) {
  91. categoryModel.parentModel = model.parentModel;
  92. model.parentModel = categoryModel;
  93. }
  94. return model;
  95. });
  96. var edgeLabelModel = self.getModel('edgeLabel'); // For option `edgeLabel` can be found by label.xxx.xxx on item mode.
  97. var fakeSeriesModel = new Model({
  98. label: edgeLabelModel.option
  99. }, edgeLabelModel.parentModel, ecModel);
  100. var emphasisEdgeLabelModel = self.getModel('emphasis.edgeLabel');
  101. var emphasisFakeSeriesModel = new Model({
  102. emphasis: {
  103. label: emphasisEdgeLabelModel.option
  104. }
  105. }, emphasisEdgeLabelModel.parentModel, ecModel);
  106. edgeData.wrapMethod('getItemModel', function (model) {
  107. model.customizeGetParent(edgeGetParent);
  108. return model;
  109. });
  110. function edgeGetParent(path) {
  111. path = this.parsePath(path);
  112. return path && path[0] === 'label' ? fakeSeriesModel : path && path[0] === 'emphasis' && path[1] === 'label' ? emphasisFakeSeriesModel : this.parentModel;
  113. }
  114. }
  115. },
  116. /**
  117. * @return {module:echarts/data/Graph}
  118. */
  119. getGraph: function () {
  120. return this.getData().graph;
  121. },
  122. /**
  123. * @return {module:echarts/data/List}
  124. */
  125. getEdgeData: function () {
  126. return this.getGraph().edgeData;
  127. },
  128. /**
  129. * @return {module:echarts/data/List}
  130. */
  131. getCategoriesData: function () {
  132. return this._categoriesData;
  133. },
  134. /**
  135. * @override
  136. */
  137. formatTooltip: function (dataIndex, multipleSeries, dataType) {
  138. if (dataType === 'edge') {
  139. var nodeData = this.getData();
  140. var params = this.getDataParams(dataIndex, dataType);
  141. var edge = nodeData.graph.getEdgeByIndex(dataIndex);
  142. var sourceName = nodeData.getName(edge.node1.dataIndex);
  143. var targetName = nodeData.getName(edge.node2.dataIndex);
  144. var html = [];
  145. sourceName != null && html.push(sourceName);
  146. targetName != null && html.push(targetName);
  147. html = encodeHTML(html.join(' > '));
  148. if (params.value) {
  149. html += ' : ' + encodeHTML(params.value);
  150. }
  151. return html;
  152. } else {
  153. // dataType === 'node' or empty
  154. return GraphSeries.superApply(this, 'formatTooltip', arguments);
  155. }
  156. },
  157. _updateCategoriesData: function () {
  158. var categories = zrUtil.map(this.option.categories || [], function (category) {
  159. // Data must has value
  160. return category.value != null ? category : zrUtil.extend({
  161. value: 0
  162. }, category);
  163. });
  164. var categoriesData = new List(['value'], this);
  165. categoriesData.initData(categories);
  166. this._categoriesData = categoriesData;
  167. this._categoriesModels = categoriesData.mapArray(function (idx) {
  168. return categoriesData.getItemModel(idx, true);
  169. });
  170. },
  171. setZoom: function (zoom) {
  172. this.option.zoom = zoom;
  173. },
  174. setCenter: function (center) {
  175. this.option.center = center;
  176. },
  177. isAnimationEnabled: function () {
  178. return GraphSeries.superCall(this, 'isAnimationEnabled') // Not enable animation when do force layout
  179. && !(this.get('layout') === 'force' && this.get('force.layoutAnimation'));
  180. },
  181. defaultOption: {
  182. zlevel: 0,
  183. z: 2,
  184. coordinateSystem: 'view',
  185. // Default option for all coordinate systems
  186. // xAxisIndex: 0,
  187. // yAxisIndex: 0,
  188. // polarIndex: 0,
  189. // geoIndex: 0,
  190. legendHoverLink: true,
  191. hoverAnimation: true,
  192. layout: null,
  193. focusNodeAdjacency: false,
  194. // Configuration of circular layout
  195. circular: {
  196. rotateLabel: false
  197. },
  198. // Configuration of force directed layout
  199. force: {
  200. initLayout: null,
  201. // Node repulsion. Can be an array to represent range.
  202. repulsion: [0, 50],
  203. gravity: 0.1,
  204. // Initial friction
  205. friction: 0.6,
  206. // Edge length. Can be an array to represent range.
  207. edgeLength: 30,
  208. layoutAnimation: true
  209. },
  210. left: 'center',
  211. top: 'center',
  212. // right: null,
  213. // bottom: null,
  214. // width: '80%',
  215. // height: '80%',
  216. symbol: 'circle',
  217. symbolSize: 10,
  218. edgeSymbol: ['none', 'none'],
  219. edgeSymbolSize: 10,
  220. edgeLabel: {
  221. position: 'middle',
  222. distance: 5
  223. },
  224. draggable: false,
  225. roam: false,
  226. // Default on center of graph
  227. center: null,
  228. zoom: 1,
  229. // Symbol size scale ratio in roam
  230. nodeScaleRatio: 0.6,
  231. // cursor: null,
  232. // categories: [],
  233. // data: []
  234. // Or
  235. // nodes: []
  236. //
  237. // links: []
  238. // Or
  239. // edges: []
  240. label: {
  241. show: false,
  242. formatter: '{b}'
  243. },
  244. itemStyle: {},
  245. lineStyle: {
  246. color: '#aaa',
  247. width: 1,
  248. opacity: 0.5
  249. },
  250. emphasis: {
  251. label: {
  252. show: true
  253. }
  254. }
  255. }
  256. });
  257. var _default = GraphSeries;
  258. module.exports = _default;