MiniBar.vue 1.3 KB
Newer Older
1
<template>
2 3 4 5 6
  <div :style="{'width':width==null?'auto':width+'px'}">
    <v-chart :forceFit="width==null" :height="height" :data="data" padding="0">
      <v-tooltip/>
      <v-bar position="x*y"/>
    </v-chart>
7 8 9 10 11
  </div>
</template>

<script>
  import moment from 'dayjs'
12 13

  const sourceData = []
14 15 16
  const beginDay = new Date().getTime()

  for (let i = 0; i < 10; i++) {
17
    sourceData.push({
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
      x: moment(new Date(beginDay + 1000 * 60 * 60 * 24 * i)).format('YYYY-MM-DD'),
      y: Math.round(Math.random() * 10)
    })
  }

  const tooltip = [
    'x*y',
    (x, y) => ({
      name: x,
      value: y
    })
  ]

  const scale = [{
    dataKey: 'x',
    min: 2
  }, {
    dataKey: 'y',
    title: '时间',
    min: 1,
    max: 30
  }]

  export default {
42 43 44
    name: 'MiniBar',
    props: {
      dataSource: {
45
        type: Array,
46 47 48 49 50 51 52 53 54
        default: () => []
      },
      width: {
        type: Number,
        default: null
      },
      height: {
        type: Number,
        default: 200
55 56
      }
    },
57 58 59 60 61
    created() {
      if (this.dataSource.length === 0) {
        this.data = sourceData
      } else {
        this.data = this.dataSource
62 63
      }
    },
64
    data() {
65 66
      return {
        tooltip,
67 68
        data: [],
        scale
69 70 71 72 73 74 75 76
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import "chart";
</style>