<template>
    <div class="carTypeTime">
        <canvas v-if="!carTypeTimePath" class="canvas" canvas-id="carTypeTime" id="carTypeTime"/>
        <image v-if="carTypeTimePath" class="canvas" :src="carTypeTimePath"></image>
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
var uChartsInstance = {};
export default {
    name: "carTypeTime",
    data() {
        return {
            width:0,
            carTypeTimePath:''//图片路径
        }
    },
    props: {
        data: {
            type: Object,
            default: () => {}
        }
    },
    watch: {
        data: {
            handler(value) {
                this.carTypeTimePath=''
                this.handleCarData(value)
            },
            deep:true
        }
    },
    methods: {
        // 处理传入的数据
        handleCarData(value) {
            console.log('carTypeTime',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: 252,
                categories:data.categories,
                series: data.series,
                animation: false,
                canvas2d: true,
                rotate: false,
                rotateLock: false,
                background: "#FFFFFF",
                color: ["#00C2FF","#CAD0DA"],
                padding: [30,30,0,0],
                dataLabel: false,
                enableScroll: false,
                legend: {
                    show: true,
                    position: "bottom",
                    lineHeight: 25,
                    float: 'center'

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

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