BarChart.vue 3.5 KB
Newer Older
1
<!--  线 + 柱混合图 -->
2 3 4 5 6 7 8 9 10
<template>
  <div
      :id="id"
      :class="className"
      :style="{height, width}"
  />
</template>

<script setup lang="ts">
11
import {nextTick, onActivated, onBeforeUnmount, onDeactivated, onMounted} from "vue";
12
import {init, EChartsOption} from 'echarts'
13
import * as echarts from 'echarts';
14
import resize from '@/utils/resize'
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

const props = defineProps({
  id: {
    type: String,
    default: 'barChart'
  },
  className: {
    type: String,
    default: ''
  },
  width: {
    type: String,
    default: '200px',
    required: true
  },
  height: {
    type: String,
    default: '200px',
    required: true
  }
})

37 38 39 40 41 42 43 44
const {
  mounted,
  chart,
  beforeDestroy,
  activated,
  deactivated
} = resize()

45 46 47 48 49 50
function initChart() {
  const barChart = init(document.getElementById(props.id) as HTMLDivElement)

  barChart.setOption({
    title: {
      show: true,
51
      text: '业绩总览(2021年)',
52 53 54 55 56 57
      x: 'center',
      padding: 15,
      textStyle: {
        fontSize: 18,
        fontStyle: 'normal',
        fontWeight: 'bold',
58
        color: '#337ecc'
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
      }
    },
    grid: {
      left: '2%',
      right: '2%',
      bottom: '10%',
      containLabel: true
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    legend: {
      x: 'center',
      y: 'bottom',
      data: ['收入', '毛利润', '收入增长率', '利润增长率']
    },
    xAxis: [
      {
        type: 'category',
84
        data: ['上海', '北京', '浙江', '广东', '深圳', '四川', '湖北', '安徽'],
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        max: 10000,
        interval: 2000,
        axisLabel: {
          formatter: '{value} '
        }
      },
      {
        type: 'value',
        min: 0,
        max: 100,
        interval: 20,
        axisLabel: {
          formatter: '{value}%'
        }
      }
    ],
    series: [
      {
        name: '收入',
        type: 'bar',
        data: [
115
          8000, 8200, 7000, 6200, 6500, 5500, 4500, 4200, 3800,
116
        ],
117 118 119 120 121 122 123 124
        barWidth: 20,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#83bff6' },
            { offset: 0.5, color: '#188df0' },
            { offset: 1, color: '#188df0' }
          ])
        }
125 126 127 128 129
      },
      {
        name: '毛利润',
        type: 'bar',
        data: [
130
          6700, 6800, 6300, 5213, 4500, 4200, 4200, 3800
131
        ],
132 133 134 135 136 137 138 139
        barWidth: 20,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#25d73c' },
            { offset: 0.5, color: '#1bc23d' },
            { offset: 1, color: '#179e61' }
          ])
        }
140 141 142 143 144
      },
      {
        name: '收入增长率',
        type: 'line',
        yAxisIndex: 1,
145 146 147 148
        data: [65, 67, 65, 53, 47, 45, 43, 42, 41],
        itemStyle: {
          color: '#67C23A'
        }
149 150 151 152 153
      },
      {
        name: '利润增长率',
        type: 'line',
        yAxisIndex: 1,
154 155 156 157
        data: [80, 81, 78, 67, 65, 60, 56,51, 45 ],
        itemStyle: {
          color: '#409EFF'
        }
158 159
      }
    ]
160 161
  } as EChartsOption)
  chart.value = barChart
162 163
}

164 165 166 167 168 169 170 171 172 173 174 175
onBeforeUnmount(() => {
  beforeDestroy()
})

onActivated(() => {
  activated()
})

onDeactivated(() => {
  deactivated()
})

176
onMounted(() => {
177
  mounted()
178 179 180 181
  nextTick(() => {
    initChart()
  })
})
182

183 184 185 186 187
</script>

<style lang="scss" scoped>

</style>