e-bar.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <div id="e-bar">
  3. <div :id="gid" class="" style="height:450px;"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import { mapState, createNamespacedHelpers } from 'vuex';
  8. import echarts from 'echarts/lib/echarts';
  9. import 'echarts/lib/chart/bar';
  10. import 'echarts/lib/component/title';
  11. import 'echarts/lib/component/legend';
  12. import 'echarts/lib/component/toolbox';
  13. import 'echarts/lib/component/markPoint';
  14. import 'echarts/lib/component/tooltip';
  15. export default {
  16. name: 'e-bar',
  17. props: {
  18. data: { type: [Object, Array], default: () => [] },
  19. axis: { type: Object, default: () => {} },
  20. gid: { type: String, default: `${new Date().getTime()}` },
  21. horizontal: { type: Boolean, default: true },
  22. },
  23. components: {},
  24. data: function() {
  25. return {
  26. chart: null,
  27. list: undefined,
  28. };
  29. },
  30. created() {},
  31. mounted() {
  32. this.init();
  33. },
  34. methods: {
  35. async init() {
  36. let e = document.getElementById(this.gid);
  37. if (!e) {
  38. console.warn('没有找到指定渲染容器');
  39. return;
  40. }
  41. if (!this.list) {
  42. //未获得数据
  43. return;
  44. }
  45. let { x, y, xAlias, yAlias } = this.axis;
  46. let legend = this.list.map(i => i[x]);
  47. this.chart = echarts.init(document.getElementById(this.gid));
  48. let xAxis = {};
  49. let yAxis = {};
  50. let data = this.list.map(i => i[y]);
  51. if (this.horizontal) {
  52. xAxis = { data: legend };
  53. yAxis = { type: y };
  54. } else {
  55. yAxis = { data: legend };
  56. xAxis = { type: y };
  57. }
  58. const option = {
  59. title: {},
  60. tooltip: {
  61. trigger: 'item',
  62. formatter: '{a}<br/>{b} : {c} ',
  63. axisPointer: {
  64. type: 'shadow',
  65. },
  66. },
  67. legend: {
  68. data: [yAlias],
  69. },
  70. xAxis: xAxis,
  71. yAxis: yAxis,
  72. barMaxWidth: '30%',
  73. series: [
  74. {
  75. name: yAlias,
  76. type: 'bar',
  77. data: data,
  78. label: {
  79. show: true,
  80. position: 'top',
  81. textStyle: {
  82. color: '#939393',
  83. },
  84. },
  85. },
  86. ],
  87. };
  88. this.chart.setOption(option);
  89. },
  90. },
  91. watch: {
  92. data: {
  93. handler(val) {
  94. if (val.length > 0) this.$set(this, `list`, val);
  95. },
  96. immediate: true,
  97. deep: true,
  98. },
  99. list: {
  100. handler(val) {
  101. if (val) this.$nextTick(() => this.init());
  102. },
  103. },
  104. },
  105. computed: {
  106. ...mapState(['user']),
  107. pageTitle() {
  108. return `${this.$route.meta.title}`;
  109. },
  110. },
  111. metaInfo() {
  112. return { title: this.$route.meta.title };
  113. },
  114. };
  115. </script>
  116. <style lang="less" scoped></style>