<template>
    <div class="timePeriodAnalysis">
        <canvas v-if="!timePeriodPath" class="timePeriod" canvas-id="timePeriod" id="timePeriod"/>
        <image class="timePeriod" v-if="timePeriodPath" :src="timePeriodPath"></image>
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
var uChartsInstance = {};
export default {
    name: "timePeriodAnalysis",
    data() {
        return {
            width:0,
            timePeriodPath:''
        }
    },
    props: {
        data: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        data: {
            handler(value) {
                console.log('value1111',value)
                this.handleCarData(value)
            },
            immediate: true
        }
    },
    methods: {
        // 处理传入的数据
        handleCarData(value) {
            let res = value
            this.drawCharts('timePeriod', 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: "bubble",
                context: ctx,
                width: _this.width,
                height: 200,
                series: data.series,
                animation: false,
                rotate: false,
                rotateLock: false,
                background: "#FFFFFF",
                color: ["#2483FF"],
                padding: [15, 10, 5, 5],
                dataLabel: false,
                enableScroll: false,
                xAxis: {
                    disableGrid: true,
                    gridType: "solid",
                    splitNumber: 6,
                    boundaryGap: "justify",
                    min: 0,
                    max: 24,
                  axisLineColor:'#F2F2F5'
                },
                yAxis: {
                    disableGrid: false,
                    gridType: "solid",
                    gridColor:'#F2F2F5',
                    data: [
                        {
                            min: 0,
                            max: 50,
                          unit:'%',
                          axisLineColor:'#F2F2F5'
                        }
                    ]
                },
                legend: {
                    show: false,
                    position: "bottom",
                    lineHeight: 25,
                    float: 'center',
                },
                extra: {
                    bubble: {
                        border: 1,
                        opacity: 0.25
                    }
                }
            });
            setTimeout( ()=>{
                this.canvasToTempImage('timePeriod')
            },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.timePeriodPath = base64
                            }
                        })
                    }
                }
            },this)
        },
    }
}
</script>

<style scoped lang="scss">
.timePeriodAnalysis{
    width: 100%;
    .timePeriod{
        width: 100%;
        height: 200px;
    }
}
</style>
