<template>
    <div class="levelTop">
      <QiunDataCharts
          type="column"
          :opts="opts"
          :chartData="res"
          :animation="false"
          :inScrollView="true"
          :canvas2d="true"
          tooltipFormat="levelTop"
      />
<!--        <canvas style="width: 100vw;height: 252px;position: fixed;left: 100%"  class="level" canvas-id="levelTop" id="levelTop" @tap="tap"/>-->
<!--        <image style="width: 100%;height: 252px" v-if="levelTopPath" class="level" :src="levelTopPath"></image>-->
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
import QiunDataCharts from "../qiun-data-charts/components/qiun-data-charts/qiun-data-charts.vue";
var uChartsInstance = {};
export default {
    name: "levelTop",
  components: {QiunDataCharts},
    data() {
        return {
            width:0,
            levelTopPath:'',
          res:{},
          opts:{}
        }
    },
    props: {
        data: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        data: {
            handler(value) {
                this.handleCarData(value)
            },
            immediate: true
        }
    },
    methods: {
        tap(e){
            uChartsInstance[e.target.id].showToolTip(e,{
                formatter: (item, category, index, opts) =>{
                    return item.name + ' '+ item.data + '%';
                }
            });
            uChartsInstance[e.target.id].touchLegend(e);
        },
        // 处理传入的数据
        handleCarData(value) {
            let res = value
          this.res = res
          this.opts = {
            categories: res.categories,
            series: res.series,
            animation: false,
            rotate: false,
            rotateLock: false,
            background: "#FFFFFF",
            color: ["#CAD0DA","#ACB9CD","#1E80FF", ],
            padding: [15, 5, 5, 5],
            dataLabel: false,
            enableScroll: false,
            legend: {
              show: true,
              position: "bottom",
              lineHeight: 25,
              float: 'center',
              fontSize:10

            },
            xAxis: {
              disableGrid: true,
              axisLineColor:'#F2F2F5',
              fontSize:10,
              titleOffsetY:5
            },
            yAxis: {
              splitNumber:5,
              gridColor:'#F2F2F5',
              data: [
                {
                  min: 0,
                  max: 100,
                  unit:'%',
                  axisLineColor:'#F2F2F5'
                }
              ]
            },
            extra: {
              column: {
                type: "stack",
                width: 20,
                activeBgColor: "#000000",
                activeBgOpacity: 0.08,
                labelPosition: "center"
              }
            }
          }
            // this.drawCharts('levelTop', 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: "column",
                context: ctx,
                width: _this.width,
                height: 272,
                categories: data.categories,
                series: data.series,
                animation: false,
                rotate: false,
                rotateLock: false,
                background: "#FFFFFF",
                color: ["#CAD0DA","#ACB9CD","#1E80FF", ],
                padding: [15, 5, 5, 5],
                dataLabel: false,
                enableScroll: false,
                legend: {
                    show: true,
                    position: "bottom",
                    lineHeight: 25,
                    float: 'center',
                    fontSize:10

                },
                xAxis: {
                    disableGrid: true,
                    axisLineColor:'#F2F2F5',
                    fontSize:10,
                    titleOffsetY:5
                },
                yAxis: {
                    splitNumber:5,
                    gridColor:'#F2F2F5',
                    data: [
                        {
                            min: 0,
                            max: 100,
                            unit:'%',
                            axisLineColor:'#F2F2F5'
                        }
                    ]
                },
                extra: {
                    column: {
                        type: "stack",
                        width: 20,
                        activeBgColor: "#000000",
                        activeBgOpacity: 0.08,
                        labelPosition: "center"
                    }
                }
            });
            setTimeout( ()=>{
                this.canvasToTempImage('levelTop')
            },2000)
        },
        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.levelTopPath = base64
                            }
                        })
                    }
                }
            },this)
        },
    }
}
</script>

<style scoped lang="scss">
.levelTop{
    width:100%;
    height: 252px;
    margin-top: 12px;
    .level{
        width: 100%;
        height: 252px;
    }
}
</style>
