EchartsBar.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <div ref="myChartBar" :style="{ width: width, height: height }"></div>
  3. </template>
  4. <script>
  5. import { mapState } from 'vuex';
  6. // 引入基本模板
  7. let echarts = require('echarts/lib/echarts');
  8. // 引入柱状图组件
  9. require('echarts/lib/chart/bar');
  10. // 引入提示框和title组件
  11. require('echarts/lib/component/tooltip');
  12. require('echarts/lib/component/title');
  13. import { LegendComponent } from 'echarts/components';
  14. import { GridComponent } from 'echarts/components';
  15. echarts.use([GridComponent]);
  16. echarts.use([LegendComponent]);
  17. export default {
  18. props: {
  19. // 数据
  20. barData: {
  21. type: Object,
  22. default() {
  23. return {
  24. title: '统计图标题', // 标题
  25. xAxis: {
  26. data: [],
  27. }, // x轴数据
  28. yAxis: {
  29. min: null,
  30. max: null,
  31. }, // y轴数据
  32. series: {
  33. data: [],
  34. }, // 移入提示数据
  35. };
  36. },
  37. },
  38. // 宽度
  39. width: {
  40. type: String,
  41. default: '100%',
  42. },
  43. // 高度
  44. height: {
  45. type: String,
  46. default: '400px',
  47. },
  48. },
  49. components: {},
  50. data() {
  51. return {};
  52. },
  53. created() {},
  54. mounted() {},
  55. beforeDestroy() {},
  56. watch: {
  57. // 监听浏览器可视区域宽度是否变化
  58. screenWidth() {
  59. this.resizeByWindowFun(); // 初始化echarts图表大小
  60. },
  61. },
  62. computed: {
  63. ...mapState({
  64. screenWidth: (state) => state.common.screenWidth,
  65. }),
  66. },
  67. methods: {
  68. // 初始化数据
  69. initDataFun(barData) {
  70. // 基于准备好的dom,初始化echarts实例
  71. let myChart = echarts.init(this.$refs.myChartBar);
  72. // 绘制图表
  73. if (!barData) {
  74. myChart.setOption({
  75. title: {
  76. text: '暂无数据',
  77. x: 'center',
  78. y: 'center',
  79. textStyle: {
  80. color: '#999',
  81. fontWeight: 'normal',
  82. fontSize: 24,
  83. },
  84. },
  85. });
  86. } else {
  87. myChart.setOption({
  88. tooltip: {},
  89. xAxis: {
  90. type: 'category',
  91. data: barData.xAxis || [],
  92. },
  93. yAxis: {
  94. type: 'value',
  95. // min: barData.yAxis.min,
  96. // max: barData.yAxis.max,
  97. },
  98. series: [
  99. {
  100. type: 'bar',
  101. data: barData.series || [],
  102. showBackground: true,
  103. backgroundStyle: {
  104. color: 'rgba(180, 180, 180, 0.2)',
  105. },
  106. itemStyle: {
  107. color: '#1890FF',
  108. },
  109. emphasis: {
  110. itemStyle: {
  111. color: '#1890FF',
  112. },
  113. },
  114. },
  115. ],
  116. });
  117. }
  118. },
  119. // 根据窗口大小 初始化echarts图表大小
  120. resizeByWindowFun() {
  121. echarts.init(this.$refs.myChartBar).resize();
  122. },
  123. },
  124. };
  125. </script>
  126. <style lang="less"></style>
  127. <style lang="less" scoped></style>