<template>
    <div style="min-height: 100px;">
            <div class="carTypeTime" >
                <canvas v-if="!carTypeTimePath" class="canvas" canvas-id="carTypeTime" id="carTypeTime" @tap="handleTap"/>
                <image v-if="carTypeTimePath" class="canvas" :src="carTypeTimePath"></image>
            </div>
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
import NoData from "../noData.vue";
import Analyse from "../analyse.vue";
var uChartsInstance = {};
export default {
    name: "carTypeTime",
    components: {Analyse, NoData},
    data() {
        return {
            width:0,
            isShowData:false,
            info:{},
            carTypeTimePath:'',//图片路径
            analyseInfo:{
                analysisins_type: 1104,
                analysisins_format: 2000
            },
            num:0
        }
    },
    props: {
        data: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        data: {
            handler(value) {
                this.info = value
                this.carTypeTimePath=''
                this.handleCarData(value)
            },
            immediate:true,
            deep:true
        }
    },
    methods: {
        //点击事件
        handleTap(e){
          uChartsInstance[e.target.id].showToolTip(e,{
            formatter: (item, category, index, opts) =>{
              if (this.num===0){
                this.num++
                return item.name + ":" + opts.series[0].valueList[index] + '辆';
              }else{
                this.num=0
                return item.name + ":" + item.data + '分';
              }
            }
          });
          uChartsInstance[e.target.id].touchLegend(e);
        },
        // 处理传入的数据
        handleCarData(value) {
            let res = {
                categories: value.categories,
                series: value.series
            }
            this.drawCharts('carTypeTime', res)
        },
        drawCharts(id, data) {
            const ctx = uni.createCanvasContext(id, this);
            let phoneInfo = uni.getStorageSync('phoneInfo')
            this.width =  phoneInfo.screenWidth-16
            uChartsInstance[id] = new uCharts({
                type: "column",
                context: ctx,
                width: this.width,
                height: 265,
                categories:data.categories,
                series: data.series,
                animation: false,
                canvas2d: true,
                rotate: false,
                rotateLock: false,
                background: "#FFFFFF",
                color: ["#1E80FF","#00B6FF"],
                padding: [30,30,0,0],
                dataLabel: false,
                enableScroll: false,
                legend: {
                    show: true,
                    position: "bottom",
                    lineHeight: 25,
                    float: 'center'

                },
                xAxis:{
                    disableGrid: true,
                  axisLineColor:"#F2F2F5"
                },
                yAxis:{
                    showTitle:true,
                  gridColor:'#F2F2F5',
                    data: [
                        {
                            title:'单位: %',
                            min: 0,
                            max: 100,
                            titleOffsetY: -5,
                            axisLineColor:"#F2F2F5"
                        },
                        {
                            title:'单位: 分钟',
                            min: 0,
                            max: 150,
                            position: 'right',
                            titleOffsetY: -5,
                            titleOffsetX: 5,
                            axisLineColor:"#F2F2F5"
                        },
                    ]
                },
                extra: {
                    column: {
                        type: "group",
                        width: 12,
                        seriesGap:2,
                        categoryGap: 24,
                        activeBgColor: "#000000",
                        activeBgOpacity: 0.08,
                    }
                }
            });
            setTimeout( ()=>{
                // this.canvasToTempImage('carTypeTime')
            },1000)
        },
        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.carTypeTimePath = base64
                            }
                        })
                    }
                }
            },this)
        },
    }
}
</script>

<style scoped lang="scss">
.carTypeTime{
    width: 100%;
    height: 252px;
    .canvas{
        width: 100%;
        height: 100%;
    }
}
</style>
