sankey.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import {
  21. IChartProps
  22. } from '../../components/Chart'
  23. import {
  24. decodeMetricName,
  25. getSizeValue,
  26. getSizeRate
  27. } from '../../components/util'
  28. import {
  29. getMetricAxisOption,
  30. getLabelOption,
  31. getLegendOption,
  32. getSymbolSize
  33. } from './util'
  34. import { EChartOption } from 'echarts'
  35. import { getFormattedValue } from '../../components/Config/Format'
  36. export default function (chartProps: IChartProps) {
  37. const {
  38. width,
  39. height,
  40. data,
  41. cols,
  42. metrics,
  43. chartStyles,
  44. tip
  45. } = chartProps
  46. const {
  47. label,
  48. spec,
  49. toolbox
  50. } = chartStyles
  51. const {
  52. nodeWidth,
  53. nodeGap,
  54. orient,
  55. draggable
  56. } = spec
  57. const labelOption = {
  58. label: getLabelOption('sankey', label, metrics)
  59. }
  60. let dimensions = []
  61. if (cols.length) {
  62. dimensions = dimensions.concat(cols)
  63. }
  64. const metricsName = decodeMetricName(metrics[0].name)
  65. const agg = metrics[0].agg
  66. const nodesValues = []
  67. const links = []
  68. data.forEach((row) => {
  69. dimensions.forEach(({ name }, idx) => {
  70. if (!nodesValues.includes(row[name])) {
  71. nodesValues.push(row[name])
  72. }
  73. if (dimensions[idx - 1]) {
  74. const source = row[dimensions[idx - 1].name]
  75. const target = row[dimensions[idx].name]
  76. const value = +row[`${agg}(${metricsName})`]
  77. if (isNaN(value)) { return }
  78. const existedLink = links.length && links.find((lnk) => lnk.source === source && lnk.target === target)
  79. if (!existedLink) {
  80. links.push({
  81. source,
  82. target,
  83. value
  84. })
  85. } else {
  86. existedLink.value += value
  87. }
  88. }
  89. })
  90. })
  91. const tooltip: EChartOption.Tooltip = {
  92. trigger: 'item',
  93. triggerOn: 'mousemove',
  94. formatter (params: EChartOption.Tooltip.Format) {
  95. const { name, value, color } = params
  96. const tooltipLabels = []
  97. if (color) {
  98. tooltipLabels.push(`<span class="widget-tooltip-circle" style="background: ${color}"></span>`)
  99. }
  100. tooltipLabels.push(name)
  101. if (value) {
  102. tooltipLabels.push(': ')
  103. tooltipLabels.push(getFormattedValue(value as number, metrics[0].format))
  104. }
  105. return tooltipLabels.join('')
  106. }
  107. }
  108. return {
  109. tooltip,
  110. series: [{
  111. type: 'sankey',
  112. layout: 'none',
  113. ...labelOption,
  114. data: nodesValues.map((val) => ({
  115. name: val
  116. })),
  117. links,
  118. orient,
  119. draggable,
  120. nodeWidth,
  121. nodeGap,
  122. focusNodeAdjacency: true,
  123. itemStyle: {
  124. normal: {
  125. borderWidth: 1,
  126. borderColor: '#aaa'
  127. }
  128. },
  129. lineStyle: {
  130. normal: {
  131. color: 'source',
  132. curveness: 0.5
  133. }
  134. }
  135. }]
  136. }
  137. }