BarAndLineCustom.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. export default defineComponent({
  8. name: 'BarAndLine',
  9. props: {
  10. chartData: {
  11. type: Array,
  12. default: () => [],
  13. },
  14. option: {
  15. type: Object,
  16. default: () => ({}),
  17. },
  18. xAxisPropType: {
  19. type: String,
  20. required: true,
  21. },
  22. propTypeArr: {
  23. type: Array,
  24. default: () => [],
  25. required: true,
  26. },
  27. width: {
  28. type: String as PropType<string>,
  29. default: '100%',
  30. },
  31. height: {
  32. type: String as PropType<string>,
  33. default: '100%',
  34. },
  35. },
  36. setup(props) {
  37. const chartRef = ref<HTMLDivElement | null>(null);
  38. const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
  39. const option = reactive({
  40. tooltip: {
  41. trigger: 'axis',
  42. axisPointer: {
  43. type: 'shadow',
  44. label: {
  45. show: true,
  46. backgroundColor: '#333',
  47. },
  48. },
  49. },
  50. legend: {
  51. top: 10,
  52. textStyle: {
  53. color: '#ffffffee',
  54. },
  55. },
  56. xAxis: {
  57. type: 'category',
  58. splitLine: { show: true, lineStyle: { color: 'rgba(21,80,126,.5)' } },
  59. data: [],
  60. },
  61. yAxis: {
  62. type: 'value',
  63. splitLine: { show: true, lineStyle: { color: 'rgba(21,80,126,.5)' } },
  64. },
  65. series: [
  66. {
  67. name: 'bar',
  68. type: 'bar',
  69. data: [],
  70. },
  71. ],
  72. });
  73. watchEffect(() => {
  74. props.chartData && initCharts();
  75. });
  76. function initCharts() {
  77. if (props.option) {
  78. Object.assign(option, props.option);
  79. }
  80. //图例类型
  81. // let typeArr = Array.from(new Set(props.chartData.map((item) => item.type)));
  82. //轴数据
  83. let xAxisData = Array.from(new Set(props.chartData.map((item) => item[props.xAxisPropType])));
  84. [...props.propTypeArr].forEach((filed:string, index) => {
  85. option.series[index]['data'] = props.chartData.map(item => item[filed]);
  86. });
  87. option.xAxis.data = xAxisData;
  88. setOptions(option, false);
  89. }
  90. return { chartRef };
  91. },
  92. });
  93. </script>