chart-base.vue 3.02 KB
<!--
 * @Description: 
 * @Date: 2021-06-15 19:55:05
-->
<!--
 * @Description: 
 * @Date: 2020-07-16 18:27:04
-->
<template>
  <div class="ebox" :id="uuid">
    <slot class="table" name="map" />
  </div>
</template>

<script>
// import { v4 } from 'uuid';
/**
 * echart 配置
 */
import { use, init, registerMap } from 'echarts/core';
import { SVGRenderer, CanvasRenderer } from 'echarts/renderers';
import {
  PieChart,
  BarChart,
  LineChart,
  MapChart,
  ScatterChart
} from 'echarts/charts';

import china from '../chart-type/json/china.json';
// import chinaMapOutline from '../chart-type/json/chinaMapOutline.json';
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent,
  DatasetComponent,
  PolarComponent,
  VisualMapComponent
} from 'echarts/components';
use([
  SVGRenderer,
  BarChart,
  PieChart,
  LineChart,
  MapChart,
  ScatterChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent,
  DatasetComponent,
  PolarComponent,
  VisualMapComponent
]);
export default {
  props: {
    option: {
      type: Object,
      default: () => ({})
    },
    type: [Number, String],
    autoresize: Boolean
  },
  data() {
    return {
      uuid: `${parseInt(Math.random())}-${new Date().getTime()}`,
      myChart: null,
      height: 0,
      imgUrl: ''
    };
  },
  watch: {
    option: {
      deep: true,
      immediate: true,
      handler(v) {
        if (v) {
          this.$nextTick(() => {
            this.init();
          });
        }
      }
    }
  },
  methods: {
    init() {
      if (!this.myChart) {
        if ([13, 14].includes(+this.type)) {
          registerMap('china', china);
          // registerMap('chinaMapOutline', chinaMapOutline);
        }
        this.myChart = init(document.getElementById(this.uuid));
      }
      const { option, myChart } = this;
      const maxLength = option.series[0].data.length;
      const cloneArr = [...option.series[0].data];
      if (maxLength > 200) {
        const setData = (_myChart, startIndex) => {
          const endIndex = startIndex + 200;
          if (startIndex == 0) {
            option.series[0].data = cloneArr.slice(startIndex, endIndex);
            _myChart.setOption(option);
          } else {
            _myChart.appendData({
              seriesIndex: '0',
              data: cloneArr.slice(startIndex, endIndex)
            });
          }
          if (maxLength > endIndex) {
            setTimeout(
              () => {
                setData(_myChart, endIndex);
              },
              startIndex == 0 ? 0 : 100
            );
          }
        };
        setData(myChart, 0);
      } else {
        myChart.setOption(option);
      }
      // this.imgUrl = this.myChart.getDataURL({
      //   type: 'png',
      //   pixelRatio: 1, //放大2倍
      //   backgroundColor: '#fff'
      // });
    }
  },
  mounted() {},
  created() {}
};
</script>
<style lang="scss">
.ebox.hide > div {
  opacity: 0;
}
</style>
<style scoped lang="scss">
.ebox {
  display: flex;
  & + .ebox {
    margin-top: 80px;
  }
}
</style>