LineChart.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div ref="chartDom" style="width: 100%;height: 100%;" />
  3. </template>
  4. <script setup>
  5. import * as echarts from 'echarts'
  6. const props = defineProps({
  7. lineData: {
  8. type: Object,
  9. default: {}
  10. }
  11. })
  12. const chartDom = ref(null);
  13. let chartInstance = null;
  14. // 初始化图表
  15. const initChart = () => {
  16. chartInstance = echarts.init(chartDom.value);
  17. chartInstance.setOption({
  18. tooltip: {
  19. trigger: 'axis',
  20. textStyle: {
  21. color: '#fafafa',
  22. },
  23. borderColor: 'transparent',
  24. backgroundColor: 'rgba(0, 0, 0, 0.5)',
  25. extraCssText: 'backdrop-filter: blur(6px);',
  26. },
  27. grid: {
  28. left: '4%',
  29. right: '4%',
  30. bottom: '10%',
  31. top: '16%',
  32. containLabel: true,
  33. },
  34. xAxis: {
  35. data: Object.keys(props.lineData),
  36. type: 'category',
  37. boundaryGap: false,
  38. axisLine: {
  39. symbol: 'none',
  40. lineStyle: {
  41. color: '#50637A',
  42. },
  43. },
  44. axisTick: {
  45. show: false,
  46. },
  47. axisLabel: {
  48. interval: 0,
  49. color: '#ffffff',
  50. fontSize: 12,
  51. padding: [10, 0, 0, 0],
  52. },
  53. },
  54. yAxis: {
  55. type: 'value',
  56. axisLabel: {
  57. color: '#ffffff',
  58. fontSize: 12,
  59. padding: [0, 10, 0, 0],
  60. },
  61. splitLine: {
  62. show: false,
  63. },
  64. },
  65. series: [
  66. {
  67. name: '增加值',
  68. data: Object.entries(props.lineData),
  69. type: 'line',
  70. color: 'rgb(49, 143, 247)',
  71. lineStyle: {
  72. width: 2,
  73. },
  74. areaStyle: {
  75. color: new echarts.graphic.LinearGradient(
  76. 0,
  77. 0,
  78. 0,
  79. 1,
  80. [{
  81. offset: 0,
  82. color: 'rgba(49, 143, 247, .6)',
  83. },
  84. {
  85. offset: 0.8,
  86. color: 'rgba(49, 143, 247, .2)',
  87. },
  88. ],
  89. false
  90. ),
  91. shadowColor: 'rgba(0, 0, 0, 0.1)',
  92. shadowBlur: 10,
  93. },
  94. symbol: 'circle',
  95. symbolSize: 6,
  96. },
  97. ],
  98. });
  99. };
  100. watch(() => props.lineData, (newVal) => {
  101. if (chartInstance) {
  102. chartInstance.setOption({
  103. xAxis: {
  104. data: Object.keys(props.lineData),
  105. },
  106. series: [{
  107. data: Object.entries(props.lineData),
  108. }],
  109. })
  110. }
  111. }, { deep: true, immediate: true } // 开启深度监听
  112. )
  113. // 生命周期
  114. onMounted(initChart);
  115. // 窗口自适应
  116. window.addEventListener('resize', () => {
  117. chartInstance?.resize();
  118. });
  119. </script>
  120. <style lang="scss"></style>