echarts 桑基图案例 https://echarts.apache.org/examples/zh/editor.html?c=sankey-levels

  1. import React, { useEffect, useState } from 'react';
  2. import { array, bool, number } from 'prop-types';
  3. import { TooltipComponent } from 'echarts/components';
  4. import { SankeyChart } from 'echarts/charts';
  5. import { UniversalTransition } from 'echarts/features';
  6. import Echarts, { chartHeight, sanKeyColor } from '@/components/Echarts';
  7. import { nodes, calls } from './nodes.json';
  8. const sankeyColor = [
  9. '#3fe1da', '#6be6c1', '#3fcfdc', '#626c91', '#3fbcde',
  10. '#a0a7e6', '#3fa9e1', '#96dee8', '#bf99f8'
  11. ];
  12. Chart.propTypes = {
  13. dataSource: array,
  14. height: number,
  15. loading: bool,
  16. };
  17. Chart.defaultProps = {
  18. dataSource: [],
  19. height: chartHeight,
  20. loading: false,
  21. };
  22. function Chart({ dataSource, height, loading }) {
  23. const [options, setOptions] = useState({});
  24. useEffect(update, [loading]);
  25. function update() {
  26. const OPTIONS = getOptions({ nodes, calls });
  27. setOptions(OPTIONS);
  28. }
  29. function getOptions({ nodes, calls }) {
  30. return {
  31. tooltip: {
  32. trigger: 'item',
  33. },
  34. series: {
  35. type: 'sankey',
  36. left: 40,
  37. top: 20,
  38. right: 300,
  39. bottom: 40,
  40. emphasis: { focus: 'adjacency' },
  41. data: nodes,
  42. links: calls,
  43. label: {
  44. color: '#fff',
  45. formatter: (param) => param.data.name,
  46. },
  47. color: sankeyColor,
  48. itemStyle: {
  49. borderWidth: 0,
  50. },
  51. lineStyle: {
  52. color: 'source',
  53. opacity: 0.12,
  54. },
  55. tooltip: {
  56. position: 'bottom',
  57. formatter: (param) => {
  58. if (param.dataType === 'edge') {
  59. return `${param.data.serviceName} -> ${param.data.destServiceName}`;
  60. }
  61. return param.data.serviceName;
  62. },
  63. },
  64. },
  65. };
  66. }
  67. const attrs = {
  68. options,
  69. height,
  70. loading,
  71. components: [
  72. TooltipComponent,
  73. SankeyChart,
  74. UniversalTransition,
  75. ],
  76. // renderType: 'svg'
  77. };
  78. return (
  79. <Echarts {...attrs} />
  80. );
  81. }
  82. export default Chart;

桑基图数据

数据来源:http://demo.skywalking.apache.org/topology

  1. {
  2. "nodes": [
  3. {
  4. "id": "VXNlcg==.0_VXNlcg==",
  5. "name": "User",
  6. "serviceId": "VXNlcg==.0",
  7. "serviceName": "User",
  8. "type": "",
  9. "isReal": false
  10. },
  11. {
  12. "id": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1_R0VUOi9wcm9qZWN0Qi97dmFsdWV9",
  13. "name": "GET:/projectB/{value}",
  14. "serviceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1",
  15. "serviceName": "business-zone::projectB",
  16. "type": "",
  17. "isReal": true
  18. },
  19. {
  20. "id": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1_R0VUOi9wcm9qZWN0QS97bmFtZX0=",
  21. "name": "GET:/projectA/{name}",
  22. "serviceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1",
  23. "serviceName": "business-zone::projectA",
  24. "type": "",
  25. "isReal": true
  26. },
  27. {
  28. "id": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1_R0VUOi9wcm9qZWN0Qy97dmFsdWV9",
  29. "name": "GET:/projectC/{value}",
  30. "serviceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1",
  31. "serviceName": "business-zone::projectC",
  32. "type": "",
  33. "isReal": true
  34. },
  35. {
  36. "id": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1_L3Byb2plY3RBL3Rlc3Q=",
  37. "name": "/projectA/test",
  38. "serviceId": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1",
  39. "serviceName": "system::load balancer1",
  40. "type": "",
  41. "isReal": true
  42. },
  43. {
  44. "id": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMg==.1_L3Byb2plY3RBL3Rlc3Q=",
  45. "name": "/projectA/test",
  46. "serviceId": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMg==.1",
  47. "serviceName": "system::load balancer2",
  48. "type": "",
  49. "isReal": true
  50. }
  51. ],
  52. "calls": [
  53. {
  54. "id": "VXNlcg==.0-VXNlcg==-YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1-R0VUOi9wcm9qZWN0Qi97dmFsdWV9",
  55. "source": "VXNlcg==.0_VXNlcg==",
  56. "target": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1_R0VUOi9wcm9qZWN0Qi97dmFsdWV9",
  57. "detectPoints": [
  58. "SERVER"
  59. ],
  60. "serviceName": "User",
  61. "endpointName": "User",
  62. "serviceId": "VXNlcg==.0",
  63. "endpointId": "VXNlcg==.0_VXNlcg==",
  64. "type": "",
  65. "destServiceName": "business-zone::projectB",
  66. "destServiceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1",
  67. "destEndpointName": "GET:/projectB/{value}",
  68. "destEndpointId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1_R0VUOi9wcm9qZWN0Qi97dmFsdWV9",
  69. "value": 0.01
  70. },
  71. {
  72. "id": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1-R0VUOi9wcm9qZWN0QS97bmFtZX0=-YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1-R0VUOi9wcm9qZWN0Qi97dmFsdWV9",
  73. "source": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1_R0VUOi9wcm9qZWN0QS97bmFtZX0=",
  74. "target": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1_R0VUOi9wcm9qZWN0Qi97dmFsdWV9",
  75. "detectPoints": [
  76. "SERVER"
  77. ],
  78. "destServiceName": "business-zone::projectB",
  79. "destServiceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1",
  80. "destEndpointName": "GET:/projectB/{value}",
  81. "destEndpointId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEI=.1_R0VUOi9wcm9qZWN0Qi97dmFsdWV9",
  82. "serviceName": "business-zone::projectA",
  83. "endpointName": "GET:/projectA/{name}",
  84. "serviceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1",
  85. "endpointId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1_R0VUOi9wcm9qZWN0QS97bmFtZX0=",
  86. "type": "",
  87. "value": 36
  88. },
  89. {
  90. "id": "VXNlcg==.0-VXNlcg==-YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1-R0VUOi9wcm9qZWN0Qy97dmFsdWV9",
  91. "source": "VXNlcg==.0_VXNlcg==",
  92. "target": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1_R0VUOi9wcm9qZWN0Qy97dmFsdWV9",
  93. "detectPoints": [
  94. "SERVER"
  95. ],
  96. "serviceName": "User",
  97. "endpointName": "User",
  98. "serviceId": "VXNlcg==.0",
  99. "endpointId": "VXNlcg==.0_VXNlcg==",
  100. "type": "",
  101. "destServiceName": "business-zone::projectC",
  102. "destServiceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1",
  103. "destEndpointName": "GET:/projectC/{value}",
  104. "destEndpointId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1_R0VUOi9wcm9qZWN0Qy97dmFsdWV9",
  105. "value": 0.01
  106. },
  107. {
  108. "id": "VXNlcg==.0-VXNlcg==-c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1-L3Byb2plY3RBL3Rlc3Q=",
  109. "source": "VXNlcg==.0_VXNlcg==",
  110. "target": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1_L3Byb2plY3RBL3Rlc3Q=",
  111. "detectPoints": [
  112. "SERVER"
  113. ],
  114. "serviceName": "User",
  115. "endpointName": "User",
  116. "serviceId": "VXNlcg==.0",
  117. "endpointId": "VXNlcg==.0_VXNlcg==",
  118. "type": "",
  119. "destServiceName": "system::load balancer1",
  120. "destServiceId": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1",
  121. "destEndpointName": "/projectA/test",
  122. "destEndpointId": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1_L3Byb2plY3RBL3Rlc3Q=",
  123. "value": 0.01
  124. },
  125. {
  126. "id": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1-R0VUOi9wcm9qZWN0QS97bmFtZX0=-YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1-R0VUOi9wcm9qZWN0Qy97dmFsdWV9",
  127. "source": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1_R0VUOi9wcm9qZWN0QS97bmFtZX0=",
  128. "target": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1_R0VUOi9wcm9qZWN0Qy97dmFsdWV9",
  129. "detectPoints": [
  130. "SERVER"
  131. ],
  132. "destServiceName": "business-zone::projectC",
  133. "destServiceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1",
  134. "destEndpointName": "GET:/projectC/{value}",
  135. "destEndpointId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1_R0VUOi9wcm9qZWN0Qy97dmFsdWV9",
  136. "serviceName": "business-zone::projectA",
  137. "endpointName": "GET:/projectA/{name}",
  138. "serviceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1",
  139. "endpointId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1_R0VUOi9wcm9qZWN0QS97bmFtZX0=",
  140. "type": "",
  141. "value": 4
  142. },
  143. {
  144. "id": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMg==.1-L3Byb2plY3RBL3Rlc3Q=-YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1-R0VUOi9wcm9qZWN0QS97bmFtZX0=",
  145. "source": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMg==.1_L3Byb2plY3RBL3Rlc3Q=",
  146. "target": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1_R0VUOi9wcm9qZWN0QS97bmFtZX0=",
  147. "detectPoints": [
  148. "SERVER"
  149. ],
  150. "destServiceName": "business-zone::projectA",
  151. "destServiceId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1",
  152. "destEndpointName": "GET:/projectA/{name}",
  153. "destEndpointId": "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1_R0VUOi9wcm9qZWN0QS97bmFtZX0=",
  154. "serviceName": "system::load balancer2",
  155. "endpointName": "/projectA/test",
  156. "serviceId": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMg==.1",
  157. "endpointId": "c3lzdGVtOjpsb2FkIGJhbGFuY2VyMg==.1_L3Byb2plY3RBL3Rlc3Q=",
  158. "type": "",
  159. "value": 72
  160. }
  161. ]
  162. }