column.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div id="column">
  3. <div :id="gid"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import { Column } from '@antv/g2plot';
  8. import { mapState, createNamespacedHelpers } from 'vuex';
  9. export default {
  10. name: 'column',
  11. props: {
  12. data: { type: [Object, Array], default: () => [] },
  13. axis: { type: Object, default: () => {} },
  14. gid: { type: String, default: `${new Date().getTime()}` },
  15. },
  16. components: {},
  17. data: function() {
  18. return {
  19. chart: null,
  20. list: [],
  21. };
  22. },
  23. created() {},
  24. mounted() {
  25. this.init();
  26. },
  27. methods: {
  28. async init() {
  29. let { x, y, xAlias, yAlias } = this.axis;
  30. let meta = {};
  31. meta[x] = { alias: xAlias };
  32. meta[y] = { alias: yAlias };
  33. this.chart = new Column(this.gid, {
  34. pixelRatio: 4,
  35. data: this.list,
  36. xField: x,
  37. yField: y,
  38. meta: meta,
  39. point: {
  40. visible: true,
  41. },
  42. label: {
  43. visible: true,
  44. },
  45. //stackField + legend 一起使用,前者确定以哪个字段作为选项,后者是显示与否,位置
  46. stackField: x,
  47. legend: {
  48. visible: true,
  49. position: 'right-top',
  50. },
  51. events: {
  52. onColumnClick: e => this.gclick(e),
  53. },
  54. });
  55. this.chart.render();
  56. },
  57. gclick(data) {
  58. console.log('in function:gclick');
  59. console.log(data);
  60. },
  61. },
  62. watch: {
  63. data: {
  64. immediate: true,
  65. deep: true,
  66. handler(val) {
  67. if (val) this.$set(this, `list`, val);
  68. },
  69. },
  70. },
  71. computed: {
  72. ...mapState(['user']),
  73. pageTitle() {
  74. return `${this.$route.meta.title}`;
  75. },
  76. },
  77. metaInfo() {
  78. return { title: this.$route.meta.title };
  79. },
  80. };
  81. </script>
  82. <style lang="less" scoped></style>