bar.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div id="bar">
  3. <div :id="gid"></div>
  4. <download :gid="gid" :canvas="canvas"></download>
  5. </div>
  6. </template>
  7. <script>
  8. import download from '@frame/parts/statistics/to-download';
  9. import _ from 'lodash';
  10. import { Bar } from '@antv/g2plot';
  11. import { mapState, createNamespacedHelpers } from 'vuex';
  12. export default {
  13. name: 'bar',
  14. props: {
  15. data: { type: [Object, Array], default: () => [] },
  16. axis: { type: Object, default: () => {} },
  17. gid: { type: String, default: `${new Date().getTime()}` },
  18. },
  19. components: { download },
  20. data: function() {
  21. return {
  22. chart: null,
  23. list: [],
  24. canvas: undefined,
  25. };
  26. },
  27. created() {},
  28. mounted() {
  29. this.init();
  30. },
  31. methods: {
  32. async init() {
  33. let { x, y, xAlias, yAlias } = this.axis;
  34. let meta = {};
  35. meta[x] = { alias: xAlias };
  36. meta[y] = { alias: yAlias };
  37. this.chart = new Bar(this.gid, {
  38. pixelRatio: 1,
  39. data: this.list,
  40. //条形和柱状的x,y轴需要互换
  41. xField: y,
  42. yField: x,
  43. meta: meta,
  44. point: {
  45. visible: true,
  46. },
  47. label: {
  48. visible: true,
  49. },
  50. //stackField + legend 一起使用,前者确定以哪个字段作为选项,后者是显示与否,位置
  51. stackField: x,
  52. legend: {
  53. visible: true,
  54. position: 'right-top',
  55. },
  56. xAxis: {
  57. nice: true,
  58. visible: true,
  59. line: {
  60. visible: true,
  61. },
  62. grid: {
  63. visible: true,
  64. },
  65. max: _.max(this.list.map(i => i.value)),
  66. min: 0,
  67. tickLine: {
  68. visible: true,
  69. },
  70. },
  71. yAxis: {
  72. nice: true,
  73. visible: true,
  74. grid: {
  75. visible: true,
  76. },
  77. line: {
  78. visible: true,
  79. },
  80. },
  81. events: {
  82. onColumnClick: e => this.gclick(e),
  83. },
  84. });
  85. this.chart.render();
  86. this.prepaid();
  87. },
  88. gclick(data) {
  89. console.log('in function:gclick');
  90. console.log(data);
  91. },
  92. prepaid() {
  93. let doc = document.getElementById(this.gid);
  94. let cav = doc.getElementsByTagName('canvas');
  95. if (cav.length <= 0) {
  96. console.warn('没有找到图表');
  97. return;
  98. }
  99. this.$set(this, `canvas`, cav[0]);
  100. },
  101. },
  102. computed: {
  103. ...mapState(['user']),
  104. pageTitle() {
  105. return `${this.$route.meta.title}`;
  106. },
  107. },
  108. watch: {
  109. data: {
  110. immediate: true,
  111. deep: true,
  112. handler(val) {
  113. if (val) this.$set(this, `list`, val);
  114. },
  115. },
  116. },
  117. metaInfo() {
  118. return { title: this.$route.meta.title };
  119. },
  120. };
  121. </script>
  122. <style lang="less" scoped></style>