<template>
    <div class="businessType">
      <QiunDataCharts
          type="ring"
          :opts="opts"
          :chartData="res"
          :animation="false"
          :canvas2d="true"
          :inScrollView="true"
          tooltipFormat="businessType"
      />
<!--        <canvas class="business" style=" width: 100vw;height: 170px;position: fixed;left: 100vw" canvas-id="businessType" id="businessType" @tap="handleTap"/>-->
<!--      <image v-if="preferPath" style="width: 100vw;height: 170px" :src="preferPath"></image>-->

    </div>
</template>

<script>
import QiunDataCharts from "../qiun-data-charts/components/qiun-data-charts/qiun-data-charts.vue";
import uCharts from '@/components/u-charts.js';
var uChartsInstance = {};
export default {
    name: "businessType",
  components:{QiunDataCharts},
    data() {
        return {
            width:0,
          preferPath:'',
          opts:{},
          res:{}
        }
    },
    props: {
        data: {
            type: Array,
            default: () => []
        }
    },
    watch: {
        data: {
            handler(value) {
                this.handleCarData(value)
            },
            immediate: true
        }
    },
    methods: {
      //点击事件
      handleTap(e){
        uChartsInstance[e.target.id].showToolTip(e,{
          formatter: (item, category, index, opts) =>{
            return item.name;
          }
        });
      },
        // 处理传入的数据
        handleCarData(value) {
            let res = {
                series:value
            }
            this.res = res
          this.opts = {
            series: res.series,
            animation: false,
            rotate: false,
            rotateLock: false,
            background: "#FFFFFF",
            color: ["#1E80FF", "#00C2FF","#6B6FFF","#38C275","#74839D","#B8C7DE"],
            padding: [5, 5, 5, 5],
            dataLabel: false,
            enableScroll: false,
            title:{
              name:''
            },
            subtitle:{
              name:''
            },
            legend: {
              show: true,
              position: "right",
              lineHeight: 25,
              float: 'center'

            },
            extra: {
              ring: {
                ringWidth: 20,
                activeOpacity: 0.5,
                activeRadius: 10,
                offsetAngle: -45,
                labelWidth: 15,
                border: false,
                borderWidth: 3,
                borderColor: "#FFFFFF"
              }
            }
          }
            // this.drawCharts('businessType', res)
        },
        drawCharts(id, data) {
            const ctx = uni.createCanvasContext(id, this);
            let _this = this
            let phoneInfo = uni.getStorageSync('phoneInfo')
            this.width =  phoneInfo.screenWidth - 32
            uChartsInstance[id] = new uCharts({
                type: "ring",
                context: ctx,
                width: _this.width,
                height: 180,
                series: data.series,
                animation: false,
                rotate: false,
                rotateLock: false,
                background: "#FFFFFF",
                color: ["#1E80FF", "#00C2FF","#6B6FFF","#38C275","#74839D","#B8C7DE"],
                padding: [5, 5, 5, 5],
                dataLabel: false,
                enableScroll: false,
                legend: {
                    show: true,
                    position: "right",
                    lineHeight: 25,
                    float: 'center'

                },

                extra: {
                    ring: {
                        ringWidth: 20,
                        activeOpacity: 0.5,
                        activeRadius: 10,
                        offsetAngle: -45,
                        labelWidth: 15,
                        border: false,
                        borderWidth: 3,
                        borderColor: "#FFFFFF"
                    }
                }
            });
          setTimeout( ()=>{
            _this.canvasToTempImage('businessType')
          },500)
        },
      canvasToTempImage(id){
        uni.canvasToTempFilePath({
          canvasId:id,
          complete:(res)=>{
            if (res.tempFilePath){
              uni.getFileSystemManager().readFile({
                filePath: res.tempFilePath,
                encoding: 'base64',
                success: res => {
                  let base64 = 'data:image/png;base64,' + res.data;
                  this.preferPath = base64
                }
              })
            }
          }
        },this)
      },
    }
}
</script>

<style scoped lang="scss">
.businessType{
  width: calc(100vw - 32px);
  height: 190px;
    .business{
        width: 100%;
        height: 190px;
    }
}
</style>
