SingLineArea.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <div ref="chartRef" :style="{ height, width }"></div>
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  6. import { useECharts } from '/@/hooks/web/useECharts';
  7. import { cloneDeep } from 'lodash-es';
  8. export default defineComponent({
  9. name: 'single-line',
  10. props: {
  11. chartData: {
  12. type: Array,
  13. default: () => [],
  14. },
  15. option: {
  16. type: Object,
  17. default: () => ({}),
  18. },
  19. width: {
  20. type: String as PropType<string>,
  21. default: '100%',
  22. },
  23. height: {
  24. type: String as PropType<string>,
  25. default: '100%',
  26. },
  27. },
  28. setup(props) {
  29. const chartRef = ref<HTMLDivElement | null>(null);
  30. const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
  31. const option = reactive({
  32. tooltip: {
  33. trigger: 'axis',
  34. backgroundColor: 'rgba(0, 0, 0, .6)',
  35. textStyle: {
  36. color: '#fff',
  37. fontSize: 12,
  38. },
  39. },
  40. legend: {
  41. // right: 'center',
  42. top: '4%',
  43. type: 'plain',
  44. textStyle: {
  45. color: '#fff',
  46. fontSize: 12,
  47. },
  48. // icon:'rect',
  49. itemGap: 15,
  50. itemWidth: 10,
  51. icon: 'path://M0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z',
  52. data: [],
  53. },
  54. xAxis: {
  55. type: 'category',
  56. data: [],
  57. },
  58. yAxis: {
  59. type: 'value',
  60. },
  61. series: [],
  62. });
  63. watchEffect(() => {
  64. initCharts();
  65. // props.chartData && initCharts();
  66. });
  67. function initCharts() {
  68. if (props.option) {
  69. Object.assign(option, cloneDeep(props.option));
  70. }
  71. let seriesData = props.chartData.map((item, index) => {
  72. return item.val;
  73. });
  74. let seriesData1 = props.chartData.map((item, index) => {
  75. return item.val1;
  76. });
  77. let seriesData2 = props.chartData.map((item, index) => {
  78. return item.val2;
  79. });
  80. let xAxisData = props.chartData.map((item) => {
  81. return item.name;
  82. });
  83. let legendData = option.series.map(item => {
  84. return {
  85. name: item.name
  86. }
  87. })
  88. option.series.forEach((el: any, index) => {
  89. if (el.data == 'val') {
  90. option.series[index].data = seriesData;
  91. } else if (el.data == 'val1') {
  92. option.series[1].data = seriesData1;
  93. } else if (el.data == 'val2') {
  94. option.series[2].data = seriesData2;
  95. }
  96. });
  97. option.xAxis.data = xAxisData;
  98. option.legend.data = legendData
  99. setOptions(option);
  100. // window.onresize = function () {
  101. // myChart.resize();
  102. // };
  103. }
  104. return { chartRef };
  105. },
  106. });
  107. </script>