<template>
  <div :style="{ padding: '0 0 32px 20px' }">
    <h4 :style="{ marginBottom: '20px' }">{{ title }}</h4>
    <v-chart :forceFit="true" :height="height" :data="data" :scale="scale" :padding="padding">
      <v-tooltip/>
      <v-axis/>
      <v-bar position="x*y" :color="barColor"/>
    </v-chart>
  </div>
</template>

<script>
  import { triggerWindowResizeEvent } from '@/utils/util'

  const DataSet = require('@antv/data-set');

  export default {
    name: 'Bar',
    props: {
      dataSource: {
        type: Array,
        required: true
      },
      yaxisText: {
        type: String,
        default: 'y'
      },
      title: {
        type: String,
        default: ''
      },
      height: {
        type: Number,
        default: 254
      },
      barColor: {
        type: Array,
        default: []
      },
      padding: {
        type: Array,
        default: ['auto', 'auto', '40', '50']
      },
    },
    data() {
      return {

      }
    },
    computed: {
      scale() {
        return [{
          dataKey: 'y',
          alias: this.yaxisText,
          min: 0,
          minTickInterval: 1,
        }]
      },
      data() {
        const dv = new DataSet.View().source(this.dataSource)
        dv.transform({
          type: 'fold',
          fields: ['value'],
          key: 'city',
          value: 'y',
        })
        let rows =  dv.rows
        return rows
      }
    },
    mounted() {
      triggerWindowResizeEvent()
    }
  }
</script>