wordCloud.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 { decodeMetricName } from '../../components/util'
  23. import { getFormattedValue } from '../../components/Config/Format'
  24. const defaultTheme = require('assets/json/echartsThemes/default.project.json')
  25. const defaultThemeColors = defaultTheme.theme.color
  26. export default function (chartProps: IChartProps) {
  27. const {
  28. width,
  29. height,
  30. data,
  31. cols,
  32. metrics,
  33. chartStyles
  34. } = chartProps
  35. const {
  36. spec
  37. } = chartStyles
  38. const {
  39. } = spec
  40. const title = cols[0].name
  41. const agg = metrics[0].agg
  42. const metricName = decodeMetricName(metrics[0].name)
  43. const tooltip: EChartOption.Tooltip = {
  44. formatter (params: EChartOption.Tooltip.Format) {
  45. const { name, value, color } = params
  46. const tooltipLabels = []
  47. if (color) {
  48. tooltipLabels.push(`<span class="widget-tooltip-circle" style="background: ${color}"></span>`)
  49. }
  50. tooltipLabels.push(name)
  51. if (data) {
  52. tooltipLabels.push(': ')
  53. tooltipLabels.push(getFormattedValue(value as number, metrics[0].format))
  54. }
  55. return tooltipLabels.join('')
  56. }
  57. }
  58. return {
  59. tooltip,
  60. series: [{
  61. type: 'wordCloud',
  62. sizeRange: [12, 72],
  63. textStyle: {
  64. normal: {
  65. color () {
  66. return defaultThemeColors[Math.floor(Math.random() * defaultThemeColors.length)]
  67. }
  68. }
  69. },
  70. rotationStep: 90,
  71. data: data
  72. .filter((d) => !!d[title])
  73. .map((d) => ({
  74. name: d[title],
  75. value: d[`${agg}(${metricName})`]
  76. }))
  77. }]
  78. }
  79. }