ChartIndicator.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React from 'react'
  2. import classnames from 'classnames'
  3. import { IChartInfo } from '../Widget'
  4. import { checkChartEnable } from '../util'
  5. import { Tooltip } from 'antd'
  6. const styles = require('./Workbench.less')
  7. interface IChartIndicatorProps {
  8. chartInfo: IChartInfo
  9. dimetionsCount: number
  10. metricsCount: number
  11. selectedCharts: IChartInfo[]
  12. onSelect: (chart: IChartInfo) => void
  13. }
  14. export function ChartIndicator (props: IChartIndicatorProps) {
  15. const { chartInfo, dimetionsCount, metricsCount, selectedCharts } = props
  16. const { title, icon, rules } = chartInfo
  17. const contents = rules.map(({ dimension, metric }, ruleIdx) => {
  18. const subContents = []
  19. if (Array.isArray(dimension)) {
  20. subContents.push(`${dimension[0]}个 到 ${dimension[1] === 9999 ? '多' : dimension[1]}个 维度`)
  21. } else {
  22. subContents.push(`${dimension}个 维度`)
  23. }
  24. if (Array.isArray(metric)) {
  25. subContents.push(`${metric[0]}个 到 ${metric[1] === 9999 ? '多' : metric[1]}个 指标`)
  26. } else {
  27. subContents.push(`${metric}个 指标`)
  28. }
  29. if (rules.length > 1) {
  30. return (<p key={ruleIdx}>{subContents.join(',')}</p>)
  31. }
  32. return (subContents.map((item, idx) => (<p key={`${ruleIdx}_${idx}`}>{item}</p>)))
  33. })
  34. const overlay = (
  35. <>
  36. <p>{title}</p>
  37. {contents}
  38. </>
  39. )
  40. const iconClass = classnames({
  41. iconfont: true,
  42. [icon]: true,
  43. [styles.enabled]: checkChartEnable(dimetionsCount, metricsCount, chartInfo),
  44. [styles.selected]: selectedCharts.filter((s) => s.id !== chartInfo.id).length === 0,
  45. [styles.multipleSelect]: selectedCharts.some((s) => s.id === chartInfo.id)
  46. })
  47. return (
  48. <Tooltip
  49. title={overlay}
  50. placement="bottom"
  51. mouseLeaveDelay={0}
  52. >
  53. <i className={iconClass} onClick={onSelect(props)} />
  54. </Tooltip>
  55. )
  56. }
  57. function onSelect (props) {
  58. return function () {
  59. const { chartInfo, onSelect, dimetionsCount, metricsCount } = props
  60. if (checkChartEnable(dimetionsCount, metricsCount, chartInfo)) {
  61. onSelect(chartInfo)
  62. }
  63. }
  64. }
  65. export default ChartIndicator