rose.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <div id="rose">
  3. <div :id="gid"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import { mapState, createNamespacedHelpers } from 'vuex';
  8. import { Rose } from '@antv/g2plot';
  9. export default {
  10. name: 'rose',
  11. props: {
  12. data: { type: Array, default: () => [] },
  13. axis: { type: Object, default: () => {} }, //{x:x轴字段名; xAlias:x轴名称; y:y轴字段名; yAlias:y轴名称}
  14. gid: { type: String, default: `${new Date().getTime()}` },
  15. headTitle: { type: String, default: '' },
  16. },
  17. components: {},
  18. data: function() {
  19. return {
  20. chart: null,
  21. list: undefined,
  22. };
  23. },
  24. mounted() {
  25. this.init();
  26. },
  27. methods: {
  28. async init() {
  29. let e = document.getElementById(this.gid);
  30. if (!e) {
  31. console.warn('没有找到指定渲染容器');
  32. return;
  33. }
  34. if (!this.list) {
  35. //未获得数据
  36. return;
  37. }
  38. if (this.chart) {
  39. this.chart.updateConfig({ data: this.list });
  40. } else {
  41. let { x, y, xAlias, yAlias } = this.axis;
  42. let meta = {};
  43. meta[x] = { alias: xAlias };
  44. meta[y] = { alias: yAlias };
  45. this.chart = new Rose(this.gid, {
  46. title: { visible: true, text: this.headTitle },
  47. pixelRatio: 4,
  48. data: this.list,
  49. radiusField: y,
  50. categoryField: x,
  51. colorField: x,
  52. // angleField: y,
  53. meta: meta,
  54. point: {
  55. visible: true,
  56. },
  57. label: {
  58. visible: true,
  59. type: 'spider',
  60. formatter: (text, item) => `${item[x]}: ${item[y]}`,
  61. },
  62. //stackField + legend 一起使用,前者确定以哪个字段作为选项,后者是显示与否,位置
  63. stackField: x,
  64. legend: {
  65. visible: true,
  66. position: 'right-top',
  67. },
  68. events: {
  69. // onPieClick: e => this.gclick(e),
  70. },
  71. });
  72. }
  73. this.chart.render();
  74. },
  75. },
  76. watch: {
  77. data: {
  78. handler(val) {
  79. if (val.length > 0) this.$set(this, `list`, val);
  80. },
  81. immediate: true,
  82. deep: true,
  83. },
  84. list: {
  85. handler(val) {
  86. if (val) this.$nextTick(() => this.init());
  87. },
  88. },
  89. },
  90. computed: {
  91. ...mapState(['user']),
  92. pageTitle() {
  93. return `${this.$route.meta.title}`;
  94. },
  95. },
  96. metaInfo() {
  97. return { title: this.$route.meta.title };
  98. },
  99. };
  100. </script>
  101. <style lang="less" scoped></style>