Charts.vue 2.61 KB
<!--
 * @Description: 
 * @Author: huyue
 * @Date: 2023-07-04 17:23:09
 * @LastEditors: huyue
 * @LastEditTime: 2023-07-10 17:07:39
-->
<template>
  <div class="mapChart">
    <h2 class="title-item">{{ title }}</h2>
    <div id="demo" class="midtop"></div>
  </div>
</template>
<script>
import * as echarts from 'echarts'
import '@/utils/china.js'
export default {
  name: 'Charts',
  // mixins: [resize],
  props: {
    id: {
      type: String,
      default: ''
      // required: true
    },
    chartData: {
      type: Array,
      default: () => [],
      // required: true
    },
    title: {
      type: String,
      default: '',
      // required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById('demo'))
      this.setOptions(this.chartData)
      this.chart.on('click', function (params) {
      })
    },
    setOptions(data = []) {
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{b}<br/>{c} (人)'
        },
        toolbox: {
          show: true,
          orient: 'vertical',
          left: 'right',
          top: 'center',
          feature: {
            dataView: { readOnly: false },
            restore: {},
            saveAsImage: {}
          }
        },
        visualMap: {
          min: 800,
          max: 50000,
          text: ['High', 'Low'],
          realtime: false,
          calculable: true,
          left: '5%',
          bottom: '8%',
          textStyle: {
            color: '#fff'
          },
          color: '#fff',
          inRange: {
            color: ['lightskyblue', 'yellow', 'orangered']
          }
        },
        series: [
          {
            type: 'map',
            map: 'china',
            label: {
              show: true,
              color: 'yellow'
            },
            data
          }
        ]
      })
    }
  }
}
</script>
<style scoped lang="scss">
.mapChart {
    text-align: center;
    width: 100%;
    font-size: 12px;
    font-weight: normal;
    letter-spacing: 2px;
    font-weight: normal;
    overflow: hidden;
  }
  .midtop {
    width: 99%;
    height: 430px;
  }

</style>