BarMultid.vue 2.2 KB
Newer Older
1 2 3
<template>
  <div :style="{ padding: '0 0 32px 32px' }">
    <h4 :style="{ marginBottom: '20px' }">{{ title }}</h4>
4 5 6 7 8
    <v-chart :data="data" :height="height" :force-fit="true" :onClick="handleClick">
      <v-tooltip/>
      <v-axis/>
      <v-legend/>
      <v-bar position="x*y" color="type" :adjust="adjust"/>
9 10 11 12 13 14
    </v-chart>
  </div>
</template>

<script>
  import { DataSet } from '@antv/data-set'
15
  import { ChartEventMixins } from './mixins/ChartMixins'
16 17 18

  export default {
    name: 'BarMultid',
19
    mixins: [ChartEventMixins],
20 21 22 23 24
    props: {
      title: {
        type: String,
        default: ''
      },
25 26 27 28 29 30
      dataSource: {
        type: Array,
        default: () => [
          { type: 'Jeecg', 'Jan.': 18.9, 'Feb.': 28.8, 'Mar.': 39.3, 'Apr.': 81.4, 'May': 47, 'Jun.': 20.3, 'Jul.': 24, 'Aug.': 35.6 },
          { type: 'Jeebt', 'Jan.': 12.4, 'Feb.': 23.2, 'Mar.': 34.5, 'Apr.': 99.7, 'May': 52.6, 'Jun.': 35.5, 'Jul.': 37.4, 'Aug.': 42.4 }
        ]
31
      },
32 33 34 35 36 37 38 39 40 41 42 43
      fields: {
        type: Array,
        default: () => ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.']
      },
      // 别名,需要的格式:[{field:'name',alias:'姓名'}, {field:'sex',alias:'性别'}]
      aliases: {
        type: Array,
        default: () => []
      },
      height: {
        type: Number,
        default: 254
44 45 46 47 48 49
      }
    },
    data() {
      return {
        adjust: [{
          type: 'dodge',
50 51
          marginRatio: 1 / 32
        }]
52 53
      }
    },
54 55 56
    computed: {
      data() {
        const dv = new DataSet.View().source(this.dataSource)
57 58
        dv.transform({
          type: 'fold',
59
          fields: this.fields,
60
          key: 'x',
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
          value: 'y'
        })

        // bar 使用不了 - 和 / 所以替换下
        let rows = dv.rows.map(row => {
          if (typeof row.x === 'string') {
            row.x = row.x.replace(/[-/]/g, '_')
          }
          return row
        })
        // 替换别名
        rows.forEach(row => {
          for (let item of this.aliases) {
            if (item.field === row.type) {
              row.type = item.alias
              break
            }
          }
        })
        return rows
81 82 83 84 85 86 87 88
      }
    }
  }
</script>

<style scoped>

</style>