Yaxis.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import React from 'react'
  2. import * as echarts from 'echarts/lib/echarts'
  3. import { IMetricAxisConfig } from './Pivot'
  4. import { IWidgetMetric, DimetionType, IChartStyles } from '../Widget'
  5. import { IChartLine, IChartUnit } from './Chart'
  6. import { metricAxisLabelFormatter, decodeMetricName } from '../util'
  7. import { PIVOT_DEFAULT_AXIS_LINE_COLOR } from 'app/globalConstants'
  8. const styles = require('./Pivot.less')
  9. interface IYaxisProps {
  10. height: number
  11. metrics: IWidgetMetric[]
  12. data: any[]
  13. chartStyles: IChartStyles
  14. dimetionAxis: DimetionType
  15. metricAxisConfig?: IMetricAxisConfig
  16. }
  17. export class Yaxis extends React.PureComponent<IYaxisProps, {}> {
  18. private container: HTMLDivElement = null
  19. public componentDidMount () {
  20. this.renderAxis()
  21. }
  22. public componentDidUpdate () {
  23. this.renderAxis()
  24. }
  25. private renderAxis = () => {
  26. const { metrics, data, chartStyles, dimetionAxis, metricAxisConfig } = this.props
  27. const {
  28. showLine = false,
  29. lineStyle = '',
  30. lineSize = '',
  31. lineColor = '',
  32. showLabel = false,
  33. labelFontFamily = '',
  34. labelFontSize = '',
  35. labelColor = '',
  36. showTitleAndUnit = false,
  37. titleFontFamily = '',
  38. titleFontSize = '',
  39. titleColor = ''
  40. } = dimetionAxis === 'col' ? (chartStyles.yAxis || {}) : chartStyles.xAxis
  41. const doms = this.container.children as HTMLCollectionOf<HTMLDivElement>
  42. data.forEach((block, i) => {
  43. let instance = echarts.getInstanceByDom(doms[i])
  44. if (!instance) {
  45. instance = echarts.init(doms[i], 'default')
  46. } else {
  47. instance.clear()
  48. }
  49. const grid = []
  50. const xAxis = []
  51. const yAxis = []
  52. let xSum = 0
  53. let ySum = 0
  54. let index = 0
  55. block.data.forEach((line: IChartLine) => {
  56. const { data: lineData } = line
  57. lineData.forEach((unit: IChartUnit) => {
  58. const { width, records } = unit
  59. metrics.forEach((m, l) => {
  60. const metricAxisStyle = m.chart.coordinate === 'polar'
  61. ? {
  62. axisLabel: {
  63. show: false
  64. },
  65. axisLine: {
  66. lineStyle: {
  67. color: PIVOT_DEFAULT_AXIS_LINE_COLOR
  68. }
  69. },
  70. axisTick: {
  71. show: false
  72. }
  73. }
  74. : {
  75. axisLabel: {
  76. show: showLabel,
  77. color: labelColor,
  78. fontFamily: labelFontFamily,
  79. fontSize: labelFontSize,
  80. padding: 2,
  81. formatter: metricAxisLabelFormatter,
  82. showMaxLabel: false,
  83. showMinLabel: false,
  84. verticalAlign: 'top'
  85. },
  86. axisLine: {
  87. show: showLine,
  88. lineStyle: {
  89. color: lineColor,
  90. width: lineSize,
  91. type: lineStyle
  92. }
  93. },
  94. axisTick: {
  95. show: showLine,
  96. lineStyle: {
  97. color: lineColor
  98. }
  99. }
  100. }
  101. const axisTitle = showTitleAndUnit && {
  102. name: decodeMetricName(m.name),
  103. nameLocation: 'middle',
  104. nameGap: 45,
  105. nameTextStyle: {
  106. color: titleColor,
  107. fontFamily: titleFontFamily,
  108. fontSize: titleFontSize
  109. }
  110. }
  111. grid.push({
  112. top: dimetionAxis === 'col' ? (xSum + l * width) : ySum,
  113. left: dimetionAxis === 'col' ? ySum + 63 : xSum + 63, // splitLine 对齐
  114. width: 64,
  115. height: width
  116. })
  117. if (dimetionAxis === 'col') {
  118. xAxis.push({
  119. gridIndex: index,
  120. type: 'category',
  121. show: false
  122. })
  123. yAxis.push({
  124. gridIndex: index,
  125. type: 'value',
  126. ...axisTitle,
  127. ...metricAxisStyle,
  128. ...metricAxisConfig[m.name].yAxis
  129. })
  130. } else {
  131. xAxis.push({
  132. gridIndex: index,
  133. type: 'value',
  134. show: false
  135. })
  136. yAxis.push({
  137. gridIndex: index,
  138. type: 'category',
  139. data: records.map((r) => r.key),
  140. axisLabel: {
  141. interval: 0,
  142. show: showLabel,
  143. color: labelColor,
  144. fontFamily: labelFontFamily,
  145. fontSize: labelFontSize
  146. },
  147. axisLine: {
  148. show: showLine,
  149. lineStyle: {
  150. color: lineColor,
  151. width: lineSize,
  152. type: lineStyle
  153. }
  154. },
  155. axisTick: {
  156. show: showLine,
  157. lineStyle: {
  158. color: lineColor
  159. }
  160. }
  161. })
  162. }
  163. index += 1
  164. })
  165. if (dimetionAxis === 'col') {
  166. xSum += width * metrics.length
  167. } else {
  168. ySum += width
  169. }
  170. })
  171. if (dimetionAxis === 'col') {
  172. ySum = 0
  173. } else {
  174. xSum = 0
  175. }
  176. })
  177. instance.setOption({
  178. grid,
  179. xAxis,
  180. yAxis
  181. })
  182. instance.resize()
  183. })
  184. }
  185. public render () {
  186. const { data } = this.props
  187. const blocks = data.map((block) => (
  188. <div key={block.key} style={{height: block.length}} />
  189. ))
  190. return (
  191. <div
  192. className={styles.yAxis}
  193. ref={(f) => this.container = f}
  194. >
  195. {blocks}
  196. </div>
  197. )
  198. }
  199. }
  200. export default Yaxis