AreaStack.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <echartsBase :option="setOption"/>
  3. </template>
  4. <script setup>
  5. import echartsBase from './index.vue'
  6. import { nextTick, onMounted, ref, watch } from 'vue'
  7. const props = defineProps({
  8. title: {
  9. type: String,
  10. default: '管理费已支出与剩余预算曲线图'
  11. },
  12. color: {
  13. type: Array,
  14. default: () => (['#4095E5', '#C25750'])
  15. },
  16. unit: {
  17. type: String,
  18. default: '元'
  19. },
  20. datas: {
  21. type: Array,
  22. default: () => ([])
  23. }
  24. })
  25. //初始变量
  26. const setOption = ref({})
  27. const titles = ref(props.title)
  28. const colors = ref(props.color)
  29. const units = ref(props.unit)
  30. const datas = ref(props.datas)
  31. //创建月份
  32. const monthData = ref([])
  33. for (let i = 1; i < 13; i++) {
  34. monthData.value = [...monthData.value, i + '月']
  35. }
  36. watch(() => [
  37. props.title,
  38. props.color,
  39. props.unit,
  40. ], ([title, color, unit]) => {
  41. titles.value = title
  42. colors.value = color
  43. units.value = unit
  44. setOptions()
  45. })
  46. //深度监听数据变化
  47. watch(() => [
  48. props.datas,
  49. ], ([data]) => {
  50. datas.value = data
  51. setDataFormat()
  52. }, { deep: true })
  53. //处理数据
  54. const setDataFormat = () => {
  55. const data = datas.value
  56. let legend = [], series = [];
  57. for (let i = 0; i < data.length; i++) {
  58. if (data[i].name) {
  59. legend.push(data[i].name)
  60. series.push({
  61. name: data[i].name,
  62. type: 'line',
  63. stack: 'Total',
  64. areaStyle: {},
  65. data: data[i].value
  66. })
  67. }
  68. }
  69. setOptions(legend, series)
  70. }
  71. //设置图表
  72. const setOptions = (legend = [], series = []) => {
  73. setOption.value = {
  74. color: colors.value,
  75. title: {
  76. text: titles.value,
  77. left: 'center',
  78. bottom: '0',
  79. textStyle: {
  80. color: '#B3B3B3',
  81. fontSize: 14,
  82. }
  83. },
  84. tooltip: {
  85. trigger: 'axis',
  86. axisPointer: {
  87. type: 'cross',
  88. }
  89. },
  90. legend: {
  91. data: legend
  92. },
  93. grid: {
  94. top: '40px',
  95. left: '0',
  96. right: '10px',
  97. bottom: titles.value ? '40px' : '0',
  98. containLabel: true
  99. },
  100. xAxis: [
  101. {
  102. type: 'category',
  103. boundaryGap: false,
  104. data: monthData.value
  105. }
  106. ],
  107. yAxis: [
  108. {
  109. type: 'value',
  110. axisLabel: {
  111. formatter: '{value}' + units.value
  112. },
  113. }
  114. ],
  115. series: series
  116. }
  117. }
  118. //渲染完成
  119. onMounted(() => {
  120. nextTick(() => {
  121. setDataFormat()
  122. })
  123. })
  124. </script>