echart-bar-row.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!-- 柱图 -->
  2. <template>
  3. <div style="height:100%;min-height: 300px;" :id="props.id" class="myChart"></div>
  4. </template>
  5. <script setup lang="ts">
  6. import { onMounted, inject, watch, reactive } from "vue";
  7. import { findBarRowFun } from "@/plugins/findData";
  8. // 接受父组件参数,配置默认值
  9. const props = defineProps({
  10. id: {
  11. type: String,
  12. default: () => '',
  13. },
  14. lineData: {
  15. type: Object,
  16. default: () => { },
  17. },
  18. });
  19. let echart: any = null
  20. let myChart: any = null
  21. let echartData: any = reactive({})
  22. watch(() => props.lineData, async () => {
  23. Object.assign(echartData,findBarRowFun(props.lineData))
  24. myChart.clear();
  25. myChart.setOption(initEcharts());
  26. })
  27. onMounted(() => {
  28. echart = inject('echart')
  29. myChart = echart.init(document.getElementById(props.id));
  30. setEchartFun();
  31. })
  32. //函数
  33. const setEchartFun = async () => {
  34. myChart.setOption(initEcharts());
  35. window.onresize = function () {
  36. myChart.resize();
  37. };
  38. }
  39. const initEcharts = () => {
  40. // 绘制图表
  41. return {
  42. title: {},
  43. tooltip: {
  44. // trigger: 'axis',
  45. // axisPointer: {
  46. // type: 'shadow'
  47. // }
  48. },
  49. legend: {},
  50. grid: {
  51. top:'0%',
  52. left: '0%',
  53. right: '4%',
  54. bottom: '3%',
  55. containLabel: true
  56. },
  57. xAxis: {
  58. type: 'value',
  59. boundaryGap: [0, 0.01]
  60. },
  61. yAxis: {
  62. type: 'category',
  63. data: echartData.xAxisArr
  64. },
  65. series: [
  66. {
  67. type: 'bar',
  68. data: echartData.dataServe,
  69. barMaxWidth:'20%',//宽度
  70. itemStyle: { //面积图颜色设置
  71. color: {
  72. type: 'linear',
  73. x: 0,
  74. y: 0,
  75. x2: 0,
  76. y2: 1,
  77. colorStops: [
  78. { offset: 0, color: 'rgb(51,205,184)' },
  79. { offset: 0.5, color: 'rgb(51,205,184)' },
  80. { offset: 1, color: 'rgb(51,205,184)' }
  81. ],
  82. globalCoord: false // 缺省为 false
  83. },
  84. borderRadius: [0,20,20,0] //圓角
  85. },
  86. },
  87. ]
  88. }
  89. }
  90. </script>