数据可视化大屏
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.8KB

  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. title: {
  18. textStyle: {
  19. color: "#ffffff"
  20. },
  21. },
  22. backgroundColor: "#100c2A",
  23. legend: {
  24. textStyle: {
  25. color: '#ffffff'
  26. },
  27. },
  28. tooltip: {
  29. trigger: 'item',
  30. borderColor: '#00eff8',
  31. backgroundColor: '#12192C',
  32. padding: [6, 14, 6, 14],
  33. textStyle: {
  34. fontSize: 14,
  35. color: '#74FAFB',
  36. },
  37. position: 'top',
  38. axisPointer: {
  39. type: 'shadow'
  40. }
  41. },
  42. grid: {
  43. left: '3%',
  44. right: '3%',
  45. bottom: '3%',
  46. containLabel: true
  47. },
  48. toolbox: {
  49. show: false,
  50. feature: {
  51. dataView: { show: true, readOnly: false },
  52. magicType: { show: true, type: ['line', 'bar'] },
  53. restore: { show: true },
  54. saveAsImage: { show: true }
  55. }
  56. },
  57. calculable: true,
  58. xAxis: [
  59. {
  60. type: 'category',
  61. // prettier-ignore
  62. data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
  63. }
  64. ],
  65. yAxis: [
  66. {
  67. type: 'value'
  68. }
  69. ],
  70. }
  71. constructor() { }
  72. ngOnInit(): void {
  73. console.log('ChartComponentComponent ngOnInit');
  74. this.initEcharts();
  75. }
  76. ngOnChanges(changes: SimpleChanges): void {
  77. if (changes['options'] && !changes['options'].isFirstChange()) {
  78. console.log('ChartComponentComponent ngOnChanges');
  79. this.updateChartOptions();
  80. }
  81. }
  82. ngOnDestroy(): void {
  83. if (this.chartInstance) {
  84. console.log('ChartComponentComponent ngOnInit');
  85. this.chartInstance.dispose();
  86. }
  87. }
  88. initEcharts(): void {
  89. const chartDom = this.chartContainer.nativeElement;
  90. this.chartInstance = echarts.init(chartDom);
  91. // 设置默认的tooltip配置,如果外部传入了tooltip则会覆盖这里的设置
  92. // 合并默认和外部传入的配置项
  93. const mergedOptions = {
  94. ...this.defaultTooltipOptions,
  95. ...this.options
  96. };
  97. this.chartInstance.setOption(mergedOptions);
  98. }
  99. updateChartOptions(): void {
  100. if (this.chartInstance) {
  101. this.chartInstance.setOption(this.options);
  102. }
  103. }
  104. }