数据可视化大屏
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

173 Zeilen
4.1KB

  1. import { Component, OnInit, AfterViewInit, OnDestroy, OnChanges, SimpleChanges, Input, ViewChild, ElementRef } from '@angular/core';
  2. import * as echarts from 'echarts';
  3. @Component({
  4. selector: 'app-chart-component',
  5. standalone: true,
  6. templateUrl: './chart-component.component.html',
  7. styleUrls: ['./chart-component.component.css']
  8. })
  9. export class ChartComponentComponent implements OnInit, OnDestroy, OnChanges {
  10. @ViewChild('chart', { static: true })
  11. chartContainer!: ElementRef;
  12. private chartInstance: echarts.ECharts | null = null;
  13. // 接收外部传入的配置项(例如:柱状图、折线图等配置)
  14. @Input() options: echarts.EChartsOption = {};
  15. /** 默认样式 */
  16. defaultTooltipOptions = {
  17. tooltip: {
  18. trigger: 'item',
  19. borderColor: '#00eff8',
  20. backgroundColor: '#12192C',
  21. padding: [6, 14, 6, 14],
  22. textStyle: {
  23. fontSize: 14,
  24. color: '#74FAFB',
  25. },
  26. position: 'top',
  27. axisPointer: {
  28. type: 'shadow'
  29. }
  30. },
  31. series: [
  32. {
  33. type: 'gauge',
  34. startAngle: 180,
  35. endAngle: 0,
  36. center: ['50%', '75%'],
  37. radius: '90%',
  38. min: 0,
  39. max: 1,
  40. splitNumber: 8,
  41. axisLine: {
  42. lineStyle: {
  43. width: 6,
  44. color: [
  45. [0.25, '#74FAFB'],
  46. [0.5, '#74FAFB'],
  47. [0.75, '#74FAFB'],
  48. [1, '#74FAFB']
  49. ]
  50. }
  51. },
  52. pointer: {
  53. icon: 'path://M12.8,0.7l12,40.1H0.7L12.8,0.7z',
  54. length: '12%',
  55. width: 20,
  56. offsetCenter: [0, '-60%'],
  57. itemStyle: {
  58. color: 'auto'
  59. }
  60. },
  61. axisTick: {
  62. length: 12,
  63. lineStyle: {
  64. color: 'auto',
  65. width: 2
  66. }
  67. },
  68. splitLine: {
  69. length: 20,
  70. lineStyle: {
  71. color: 'auto',
  72. width: 5
  73. }
  74. },
  75. axisLabel: {
  76. color: '#464646',
  77. fontSize: 20,
  78. distance: -60,
  79. rotate: 'tangential',
  80. formatter: function (value: any) {
  81. if (value === 0.875) {
  82. return '';
  83. } else if (value === 0.625) {
  84. return '';
  85. } else if (value === 0.375) {
  86. return '';
  87. } else if (value === 0.125) {
  88. return '';
  89. }
  90. return '';
  91. }
  92. },
  93. title: {
  94. offsetCenter: [0, '-10%'],
  95. fontSize: 20
  96. },
  97. tooltip: {
  98. trigger: 'item',
  99. borderColor: '#00eff8',
  100. backgroundColor: '#12192C',
  101. padding: [6, 14, 6, 14],
  102. textStyle: {
  103. fontSize: 14,
  104. color: '#74FAFB',
  105. },
  106. position: 'top',
  107. axisPointer: {
  108. type: 'shadow'
  109. }
  110. },
  111. detail: {
  112. fontSize: 30,
  113. offsetCenter: [0, '-5%'],
  114. valueAnimation: true,
  115. formatter: function (value: any) {
  116. return Math.round(value * 100) + '';
  117. },
  118. color: 'inherit'
  119. },
  120. }
  121. ]
  122. };
  123. constructor() { }
  124. ngOnInit(): void {
  125. console.log('ChartComponentComponent ngOnInit');
  126. this.initEcharts();
  127. }
  128. ngOnChanges(changes: SimpleChanges): void {
  129. if (changes['options'] && !changes['options'].isFirstChange()) {
  130. console.log('ChartComponentComponent ngOnChanges');
  131. this.updateChartOptions();
  132. }
  133. }
  134. ngOnDestroy(): void {
  135. if (this.chartInstance) {
  136. console.log('ChartComponentComponent ngOnInit');
  137. this.chartInstance.dispose();
  138. }
  139. }
  140. initEcharts(): void {
  141. const chartDom = this.chartContainer.nativeElement;
  142. this.chartInstance = echarts.init(chartDom, 'dark');
  143. // 设置默认的tooltip配置,如果外部传入了tooltip则会覆盖这里的设置
  144. // 合并默认和外部传入的配置项
  145. const mergedOptions = {
  146. ...this.defaultTooltipOptions,
  147. ...this.options
  148. };
  149. this.chartInstance.setOption(mergedOptions);
  150. }
  151. updateChartOptions(): void {
  152. if (this.chartInstance) {
  153. this.chartInstance.setOption(this.options);
  154. }
  155. }
  156. }