SankeySeries.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 SeriesModel = require("../../model/Series");
  20. var createGraphFromNodeEdge = require("../helper/createGraphFromNodeEdge");
  21. var _format = require("../../util/format");
  22. var encodeHTML = _format.encodeHTML;
  23. var Model = require("../../model/Model");
  24. var _config = require("../../config");
  25. var __DEV__ = _config.__DEV__;
  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. var SankeySeries = SeriesModel.extend({
  45. type: 'series.sankey',
  46. layoutInfo: null,
  47. levelModels: null,
  48. /**
  49. * Init a graph data structure from data in option series
  50. *
  51. * @param {Object} option the object used to config echarts view
  52. * @return {module:echarts/data/List} storage initial data
  53. */
  54. getInitialData: function (option, ecModel) {
  55. var links = option.edges || option.links;
  56. var nodes = option.data || option.nodes;
  57. var levels = option.levels;
  58. var levelModels = this.levelModels = {};
  59. for (var i = 0; i < levels.length; i++) {
  60. if (levels[i].depth != null && levels[i].depth >= 0) {
  61. levelModels[levels[i].depth] = new Model(levels[i], this, ecModel);
  62. } else {}
  63. }
  64. if (nodes && links) {
  65. var graph = createGraphFromNodeEdge(nodes, links, this, true, beforeLink);
  66. return graph.data;
  67. }
  68. function beforeLink(nodeData, edgeData) {
  69. nodeData.wrapMethod('getItemModel', function (model, idx) {
  70. model.customizeGetParent(function (path) {
  71. var parentModel = this.parentModel;
  72. var nodeDepth = parentModel.getData().getItemLayout(idx).depth;
  73. var levelModel = parentModel.levelModels[nodeDepth];
  74. return levelModel || this.parentModel;
  75. });
  76. return model;
  77. });
  78. edgeData.wrapMethod('getItemModel', function (model, idx) {
  79. model.customizeGetParent(function (path) {
  80. var parentModel = this.parentModel;
  81. var edge = parentModel.getGraph().getEdgeByIndex(idx);
  82. var depth = edge.node1.getLayout().depth;
  83. var levelModel = parentModel.levelModels[depth];
  84. return levelModel || this.parentModel;
  85. });
  86. return model;
  87. });
  88. }
  89. },
  90. setNodePosition: function (dataIndex, localPosition) {
  91. var dataItem = this.option.data[dataIndex];
  92. dataItem.localX = localPosition[0];
  93. dataItem.localY = localPosition[1];
  94. },
  95. /**
  96. * Return the graphic data structure
  97. *
  98. * @return {module:echarts/data/Graph} graphic data structure
  99. */
  100. getGraph: function () {
  101. return this.getData().graph;
  102. },
  103. /**
  104. * Get edge data of graphic data structure
  105. *
  106. * @return {module:echarts/data/List} data structure of list
  107. */
  108. getEdgeData: function () {
  109. return this.getGraph().edgeData;
  110. },
  111. /**
  112. * @override
  113. */
  114. formatTooltip: function (dataIndex, multipleSeries, dataType) {
  115. // dataType === 'node' or empty do not show tooltip by default
  116. if (dataType === 'edge') {
  117. var params = this.getDataParams(dataIndex, dataType);
  118. var rawDataOpt = params.data;
  119. var html = rawDataOpt.source + ' -- ' + rawDataOpt.target;
  120. if (params.value) {
  121. html += ' : ' + params.value;
  122. }
  123. return encodeHTML(html);
  124. } else if (dataType === 'node') {
  125. var node = this.getGraph().getNodeByIndex(dataIndex);
  126. var value = node.getLayout().value;
  127. var name = this.getDataParams(dataIndex, dataType).data.name;
  128. if (value) {
  129. var html = name + ' : ' + value;
  130. }
  131. return encodeHTML(html);
  132. }
  133. return SankeySeries.superCall(this, 'formatTooltip', dataIndex, multipleSeries);
  134. },
  135. optionUpdated: function () {
  136. var option = this.option;
  137. if (option.focusNodeAdjacency === true) {
  138. option.focusNodeAdjacency = 'allEdges';
  139. }
  140. },
  141. // Override Series.getDataParams()
  142. getDataParams: function (dataIndex, dataType) {
  143. var params = SankeySeries.superCall(this, 'getDataParams', dataIndex, dataType);
  144. if (params.value == null && dataType === 'node') {
  145. var node = this.getGraph().getNodeByIndex(dataIndex);
  146. var nodeValue = node.getLayout().value;
  147. params.value = nodeValue;
  148. }
  149. return params;
  150. },
  151. defaultOption: {
  152. zlevel: 0,
  153. z: 2,
  154. coordinateSystem: 'view',
  155. layout: null,
  156. // The position of the whole view
  157. left: '5%',
  158. top: '5%',
  159. right: '20%',
  160. bottom: '5%',
  161. // Value can be 'vertical'
  162. orient: 'horizontal',
  163. // The dx of the node
  164. nodeWidth: 20,
  165. // The vertical distance between two nodes
  166. nodeGap: 8,
  167. // Control if the node can move or not
  168. draggable: true,
  169. // Value can be 'inEdges', 'outEdges', 'allEdges', true (the same as 'allEdges').
  170. focusNodeAdjacency: false,
  171. // The number of iterations to change the position of the node
  172. layoutIterations: 32,
  173. label: {
  174. show: true,
  175. position: 'right',
  176. color: '#000',
  177. fontSize: 12
  178. },
  179. levels: [],
  180. // Value can be 'left' or 'right'
  181. nodeAlign: 'justify',
  182. itemStyle: {
  183. borderWidth: 1,
  184. borderColor: '#333'
  185. },
  186. lineStyle: {
  187. color: '#314656',
  188. opacity: 0.2,
  189. curveness: 0.5
  190. },
  191. emphasis: {
  192. label: {
  193. show: true
  194. },
  195. lineStyle: {
  196. opacity: 0.5
  197. }
  198. },
  199. animationEasing: 'linear',
  200. animationDuration: 1000
  201. }
  202. });
  203. var _default = SankeySeries;
  204. module.exports = _default;