|
@@ -0,0 +1,127 @@
|
|
|
+<template>
|
|
|
+ <div id="bar">
|
|
|
+ <div :id="gid"></div>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+import _ from 'lodash';
|
|
|
+import { Bar, Column } from '@antv/g2plot';
|
|
|
+import { mapState, createNamespacedHelpers } from 'vuex';
|
|
|
+export default {
|
|
|
+ name: 'bar',
|
|
|
+ props: {
|
|
|
+ data: { type: [Object, Array], default: () => [] },
|
|
|
+ axis: { type: Object, default: () => {} },
|
|
|
+ gid: { type: String, default: `${new Date().getTime()}` },
|
|
|
+ horizontal: { type: Boolean, default: true },
|
|
|
+ },
|
|
|
+ components: {},
|
|
|
+ data: function() {
|
|
|
+ return {
|
|
|
+ chart: null,
|
|
|
+ list: [],
|
|
|
+ };
|
|
|
+ },
|
|
|
+ created() {},
|
|
|
+ mounted() {
|
|
|
+ this.init();
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ async init() {
|
|
|
+ let e = document.getElementById(this.gid);
|
|
|
+ if (!e) {
|
|
|
+ console.warn('没有找到指定渲染容器');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!this.list) {
|
|
|
+ //未获得数据
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (this.chart) {
|
|
|
+ this.chart.updateConfig({ data: this.list });
|
|
|
+ } else {
|
|
|
+ let { x, y, xAlias, yAlias } = this.axis;
|
|
|
+ let meta = {};
|
|
|
+ meta[x] = { alias: xAlias };
|
|
|
+ meta[y] = { alias: yAlias };
|
|
|
+ let options = {
|
|
|
+ pixelRatio: 1,
|
|
|
+ data: this.list,
|
|
|
+ //条形和柱状的x,y轴需要互换
|
|
|
+ xField: this.horizontal ? y : x,
|
|
|
+ yField: this.horizontal ? x : y,
|
|
|
+ meta: meta,
|
|
|
+ point: {
|
|
|
+ visible: true,
|
|
|
+ },
|
|
|
+ label: {
|
|
|
+ visible: true,
|
|
|
+ },
|
|
|
+ //stackField + legend 一起使用,前者确定以哪个字段作为选项,后者是显示与否,位置
|
|
|
+ stackField: x,
|
|
|
+ legend: {
|
|
|
+ visible: true,
|
|
|
+ position: 'right-top',
|
|
|
+ },
|
|
|
+ xAxis: {
|
|
|
+ nice: true,
|
|
|
+ visible: true,
|
|
|
+ line: {
|
|
|
+ visible: true,
|
|
|
+ },
|
|
|
+ grid: {
|
|
|
+ visible: true,
|
|
|
+ },
|
|
|
+ tickLine: {
|
|
|
+ visible: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ yAxis: {
|
|
|
+ nice: true,
|
|
|
+ visible: true,
|
|
|
+ grid: {
|
|
|
+ visible: true,
|
|
|
+ },
|
|
|
+ line: {
|
|
|
+ visible: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
+
|
|
|
+ events: {
|
|
|
+ // onColumnClick: e => this.gclick(e),
|
|
|
+ },
|
|
|
+ };
|
|
|
+ if (this.horizontal) this.chart = new Bar(this.gid, options);
|
|
|
+ else this.chart = new Column(this.gid, options);
|
|
|
+ }
|
|
|
+ this.chart.render();
|
|
|
+ },
|
|
|
+ },
|
|
|
+ computed: {
|
|
|
+ ...mapState(['user']),
|
|
|
+ pageTitle() {
|
|
|
+ return `${this.$route.meta.title}`;
|
|
|
+ },
|
|
|
+ },
|
|
|
+ watch: {
|
|
|
+ data: {
|
|
|
+ handler(val) {
|
|
|
+ if (val.length > 0) this.$set(this, `list`, val);
|
|
|
+ },
|
|
|
+ immediate: true,
|
|
|
+ deep: true,
|
|
|
+ },
|
|
|
+ list: {
|
|
|
+ handler(val) {
|
|
|
+ if (val) this.$nextTick(() => this.init());
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ metaInfo() {
|
|
|
+ return { title: this.$route.meta.title };
|
|
|
+ },
|
|
|
+};
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="less" scoped></style>
|