radar.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 { IChartProps } from '../../components/Chart'
  21. import { EChartOption } from 'echarts'
  22. import {
  23. decodeMetricName,
  24. getSizeValue,
  25. getSizeRate
  26. } from '../../components/util'
  27. import { getFieldAlias } from '../../components/Config/Field'
  28. import { getFormattedValue } from '../../components/Config/Format'
  29. import {
  30. getMetricAxisOption,
  31. getLabelOption,
  32. getLegendOption,
  33. getSymbolSize
  34. } from './util'
  35. export default function (chartProps: IChartProps) {
  36. const {
  37. width,
  38. height,
  39. data,
  40. cols,
  41. metrics,
  42. chartStyles,
  43. color,
  44. tip
  45. } = chartProps
  46. const { label, legend, radar, toolbox } = chartStyles
  47. const { legendPosition, fontSize } = legend
  48. const labelOption = {
  49. label: getLabelOption('radar', label, metrics)
  50. }
  51. let dimensions = []
  52. if (cols.length) {
  53. dimensions = dimensions.concat(cols)
  54. }
  55. const metricsNames = metrics.map((m) => decodeMetricName(m.name))
  56. let seriesData
  57. let indicator
  58. let indicatorMax = -Infinity
  59. let legendData
  60. if (!dimensions.length) {
  61. if (color.items.length) {
  62. dimensions = dimensions.concat(color.items.map((c) => c.name))
  63. }
  64. const metricsData = !data.length
  65. ? []
  66. : metrics.map((m) => data[0][`${m.agg}(${decodeMetricName(m.name)})`])
  67. seriesData = !data.length ? [] : [{ value: metricsData }]
  68. indicatorMax = Math.max(...metricsData)
  69. indicatorMax = indicatorMax + Math.round(indicatorMax * 0.1)
  70. indicator = metrics.map((m) => ({
  71. name: decodeMetricName(m.name),
  72. max: indicatorMax
  73. }))
  74. } else {
  75. legendData = metricsNames
  76. const dimension = dimensions[0]
  77. const indicatorData = {}
  78. const dimensionData = metricsNames.reduce(
  79. (acc, name) => ({
  80. ...acc,
  81. [name]: {}
  82. }),
  83. {}
  84. )
  85. data.forEach((row) => {
  86. if (!indicatorData[row[dimension.name]]) {
  87. indicatorData[row[dimension.name]] = -Infinity
  88. }
  89. metrics.forEach((m) => {
  90. const name = decodeMetricName(m.name)
  91. const cellVal = row[`${m.agg}(${name})`]
  92. indicatorMax = Math.max(indicatorMax, cellVal)
  93. if (!dimensionData[name][row[dimension.name]]) {
  94. dimensionData[name][row[dimension.name]] = 0
  95. }
  96. dimensionData[name][row[dimension.name]] += cellVal
  97. })
  98. })
  99. indicator = Object.keys(indicatorData).map((name: string) => ({
  100. name,
  101. max: indicatorMax + Math.round(indicatorMax * 0.1)
  102. }))
  103. seriesData =
  104. data.length > 0
  105. ? Object.entries(dimensionData).map(([name, value]) => ({
  106. name,
  107. value: Object.values(value)
  108. }))
  109. : []
  110. }
  111. const tooltip: EChartOption.Tooltip = {
  112. formatter(params: EChartOption.Tooltip.Format) {
  113. const { dataIndex, data, color } = params
  114. let tooltipLabels = []
  115. if (dimensions.length) {
  116. const metric = metrics[dataIndex]
  117. tooltipLabels.push(
  118. getFieldAlias(metric.field, {}) || decodeMetricName(metric.name)
  119. )
  120. tooltipLabels = tooltipLabels.concat(
  121. indicator.map(
  122. ({ name }, idx) =>
  123. `${name}: ${getFormattedValue(data.value[idx], metric.format)}`
  124. )
  125. )
  126. if (color) {
  127. tooltipLabels[0] =
  128. `<span class="widget-tooltip-circle" style="background: ${color}"></span>` +
  129. tooltipLabels[0]
  130. }
  131. } else {
  132. tooltipLabels = tooltipLabels.concat(
  133. indicator.map(
  134. ({ name }, idx) =>
  135. `${name}: ${getFormattedValue(
  136. data.value[idx],
  137. metrics[idx].format
  138. )}`
  139. )
  140. )
  141. }
  142. return tooltipLabels.join('<br/>')
  143. }
  144. }
  145. return {
  146. tooltip,
  147. legend: legendData && getLegendOption(legend, legendData),
  148. radar: {
  149. // type: 'log',
  150. indicator,
  151. ...radar
  152. },
  153. series: [
  154. {
  155. name: '',
  156. type: 'radar',
  157. data: seriesData,
  158. ...labelOption
  159. }
  160. ]
  161. }
  162. }